Java 如何创建一个POJO?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3527264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a POJO?
提问by
Recently I've started hearing about "POJOs" (Plain Old Java Objects). I googled it, but still don't understand the concept well. Can anyone give me a clear description of a POJO?
最近我开始听说“POJO”(Plain Old Java Objects)。我用谷歌搜索了它,但仍然不太理解这个概念。谁能给我一个 POJO 的清晰描述?
Consider a class "Person" with variables "id, name, address, salary" -- how would I create a POJO for this scenario? Is the code below a POJO?
考虑一个带有变量“id、name、address、salary”的类“Person”——我将如何为这种情况创建一个 POJO?下面的代码是 POJO 吗?
public class Person {
//variables
People people = new People();
private int id;
private String name;
private String address;
private int salary;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getSalary() {
return salary;
}
public void setId() {
this.id = id;
}
public void setName() {
this.name = name;
}
public void setAddress() {
this.address = address;
}
public void setSalary() {
this.salary = salary;
}
}
采纳答案by duffymo
A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:
POJO 只是删除了限制的普通旧 Java Bean。Java Bean 必须满足以下要求:
- Default no-arg constructor
- Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
- Must implement java.io.Serializable
- 默认无参数构造函数
- 对于名为 foo 的可变属性,遵循 getFoo 的 Bean 约定(或 isFoo 为布尔值)和 setFoo 方法;如果 foo 是不可变的,请不要使用 setFoo。
- 必须实现 java.io.Serializable
POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.
POJO 不强制执行任何这些操作。顾名思义:在JDK下编译的对象可以被认为是Plain Old Java Object。没有应用服务器,没有基类,不需要使用接口。
The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.
首字母缩写词 POJO 是对 EJB 2.0 的一种反应,EJB 2.0 需要几个接口、扩展基类和许多方法来做简单的事情。有些人,其中包括 Rod Johnson 和 Martin Fowler,反对复杂性,并寻求一种无需编写 EJB 即可实现企业级解决方案的方法。
Martin Fowler coined a new acronym.
Martin Fowler 创造了一个新的缩写词。
Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.
Rod Johnson 写了“没有 EJB 的 J2EE”,写了 Spring,对 EJB 产生了足够的影响,所以 3.1 版看起来很像 Spring 和 Hibernate,并从 VMWare 那里获得了甜蜜的 IPO。
Here's an example that you can wrap your head around:
这是一个您可以绕过去的示例:
public class MyFirstPojo
{
private String name;
public static void main(String [] args)
{
for (String arg : args)
{
MyFirstPojo pojo = new MyFirstPojo(arg); // Here's how you create a POJO
System.out.println(pojo);
}
}
public MyFirstPojo(String name)
{
this.name = name;
}
public String getName() { return this.name; }
public String toString() { return this.name; }
}
回答by Blair Conrad
A POJO is a Plain Old Java Object.
POJO 是一个普通的旧 Java 对象。
From the wikipedia article I linked to:
从我链接到的维基百科文章:
In computing software, POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean
在计算软件中,POJO 是Plain Old Java Object 的首字母缩写词。该名称用于强调给定的对象是普通的 Java 对象,而不是特殊的对象,尤其不是Enterprise JavaBean
Your class appears to already be a POJO.
您的课程似乎已经是 POJO。
回答by Jon Hanna
When you aren't doing anything to make your class particularly designed to work with a given framework, ORM, or other system that needs a special sort of class, you have a Plain Old Java Object, or POJO.
当您没有做任何事情来使您的类专门设计为与给定的框架、ORM 或其他需要特殊类的系统一起工作时,您就有了一个普通的旧 Java 对象或 POJO。
Ironically, one of the reasons for coining the term is that people were avoiding them in cases where they were sensible and some people concluded that this was because they didn't have a fancy name. Ironic, because your question demonstrates that the approach worked.
具有讽刺意味的是,创造这个词的原因之一是人们在明智的情况下会避开它们,有些人得出结论,这是因为他们没有一个花哨的名字。具有讽刺意味的是,因为您的问题表明该方法有效。
Compare the older POD "Plain Old Data" to mean a C++ class that doesn't do anything a C struct couldn't do (more or less, non-virtual members that aren't destructors or trivial constructors don't stop it being considered POD), and the newer (and more directly comparable) POCO "Plain Old CLR Object" in .NET.
将较旧的 POD“Plain Old Data”与 C++ 类进行比较,该类不执行 C 结构无法执行的任何操作(或多或少,不是析构函数或普通构造函数的非虚拟成员不会阻止它成为考虑 POD),以及 .NET 中更新的(更直接可比的)POCO“Plain Old CLR Object”。
回答by Subodh
POJO class acts as a bean which is used to set and get the value.
POJO 类充当用于设置和获取值的 bean。
public class Data
{
private int id;
private String deptname;
private String date;
private String name;
private String mdate;
private String mname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMdate() {
return mdate;
}
public void setMdate(String mdate) {
this.mdate = mdate;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
}
回答by Md Azaharuddin Ali
POJO:- POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.
POJO:- POJO 是一个 Java 对象,除了 Java 语言规范强制的限制之外,不受任何限制的约束。
Properties of POJO
POJO 的属性
- All properties must be public setter and getter methods
- All instance variables should be private
- Should not Extend prespecified classes.
- Should not Implement prespecified interfaces.
- Should not contain prespecified annotations.
- It may not have any argument constructors
- 所有属性必须是公共的 setter 和 getter 方法
- 所有实例变量都应该是私有的
- 不应扩展预先指定的类。
- 不应实现预先指定的接口。
- 不应包含预先指定的注释。
- 它可能没有任何参数构造函数
Example of POJO
POJO 示例
public class POJO {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
回答by mohit sarsar
there are mainly three options are possible for mapping purpose
主要有三个选项可用于映射目的
- serialize
- XML mapping
- POJO mapping.(Plain Old Java Objects)
- 连载
- XML 映射
- POJO 映射。(Plain Old Java Objects)
While using the pojo classes,it is easy for a developer to map with the database. POJO classes are created for database and at the same time value-objects classes are created with getter and setter methods that will easily hold the content.
在使用 pojo 类时,开发人员很容易与数据库进行映射。POJO 类是为数据库创建的,同时值对象类是使用 getter 和 setter 方法创建的,这些方法可以轻松保存内容。
So,for the purpose of mapping in between java with database, value-objects and POJO classes are implemented.
因此,为了在java与数据库之间进行映射,实现了值对象和POJO类。
回答by Abdullah Sarfaraz
import java.io.Serializable;
public class Course implements Serializable {
protected int courseId;
protected String courseName;
protected String courseType;
public Course() {
courseName = new String();
courseType = new String();
}
public Course(String courseName, String courseType) {
this.courseName = courseName;
this.courseType = courseType;
}
public Course(int courseId, String courseName, String courseType) {
this.courseId = courseId;
this.courseName = courseName;
this.courseType = courseType;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseType() {
return courseType;
}
public void setCourseType(String courseType) {
this.courseType = courseType;
}
@Override
public int hashCode() {
return courseId;
}
@Override
public boolean equals(Object obj) {
if (obj != null || obj instanceof Course) {
Course c = (Course) obj;
if (courseId == c.courseId && courseName.equals(c.courseName)
&& courseType.equals(c.courseType))
return true;
}
return false;
}
@Override
public String toString() {
return "Course[" + courseId + "," + courseName + "," + courseType + "]";
}
}
回答by Gowtham Subramaniam
public class UserInfo {
String LoginId;
String Password;
String FirstName;
String LastName;
String Email;
String Mobile;
String Address;
String DOB;
public String getLoginId() {
return LoginId;
}
public void setLoginId(String loginId) {
LoginId = loginId;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getMobile() {
return Mobile;
}
public void setMobile(String mobile) {
Mobile = mobile;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getDOB() {
return DOB;
}
public void setDOB(String DOB) {
this.DOB = DOB;
}
}
回答by Naresh Joshi
According to Martin Fowler
根据马丁福勒的说法
The term was coined while Rebecca Parsons, Josh MacKenzie and I were preparing for a talk at a conference in September 2000. In the talk, we were pointing out the many benefits of encoding business logic into regular java objects rather than using Entity Beans. We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely.
这个术语是在 Rebecca Parsons、Josh MacKenzie 和我准备在 2000 年 9 月的一次会议上进行演讲时创造的。在演讲中,我们指出了将业务逻辑编码为常规 Java 对象而不是使用实体 Bean 的许多好处。我们想知道为什么人们如此反对在他们的系统中使用常规对象并得出结论,这是因为简单的对象缺乏一个花哨的名字。所以我们给了他们一个,它很受欢迎。
Generally, a POJO is not bound to any restriction and any Java object can be called a POJO but there are some directions. A well-defined POJO should follow below directions.
通常,POJO 不受任何限制,任何 Java 对象都可以称为 POJO,但有一些方向。定义明确的 POJO 应遵循以下说明。
- Each variable in a POJO should be declared as private.
- Default constructor should be overridden with public accessibility.
- Each variable should have its Setter-Getter method with public accessibility.
- Generally POJO should override equals(), hashCode() and toString() methods of Object (but it's not mandatory).
- Overriding compare() method of Comparable interface used for sorting (Preferable but not mandatory).
- POJO 中的每个变量都应声明为私有。
- 默认构造函数应该被公共可访问性覆盖。
- 每个变量都应具有可公开访问的 Setter-Getter 方法。
- 通常 POJO 应该覆盖 Object 的 equals()、hashCode() 和 toString() 方法(但这不是强制性的)。
- 覆盖用于排序的 Comparable 接口的 compare() 方法(首选但非强制性)。
And according to Java Language Specification, a POJO should not have to
根据 Java 语言规范,POJO 不应该
- Extend pre-specified classes
- Implement pre-specified interfaces
- Contain pre-specified annotations
- 扩展预先指定的类
- 实现预先指定的接口
- 包含预先指定的注释
However, developers and frameworks describe a POJO still requires the use prespecified annotations to implement features like persistence, declarative transaction management etc. So the idea is that if the object was a POJO before any annotations were added would return to POJO status if the annotations are removed then it can still be considered a POJO.
然而,开发人员和框架描述 POJO 仍然需要使用预先指定的注解来实现诸如持久性、声明性事务管理等功能。因此,如果在添加任何注解之前对象是 POJO,如果注解是删除,那么它仍然可以被认为是一个 POJO。
A JavaBean is a special kind of POJO that is Serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.
JavaBean 是一种特殊的 POJO,它是可序列化的,具有无参数构造函数,并允许使用遵循简单命名约定的 getter 和 setter 方法访问属性。
Read more on Plain Old Java Object (POJO) Explained.
回答by Misbagas
- File-setting-plugins-Browse repositories
- Search RoboPOJOGenerator and install, Restart Android studio
- Open Project and right click on package select on Generate POJO from JSON
- Paste JSON in dialogbox and select option according your requirements
- Click on Generate button
- 文件设置插件-浏览存储库
- 搜索 RoboPOJOGenerator 并安装,重启 Android studio
- 打开项目并右键单击从 JSON 生成 POJO 上的包选择
- 在对话框中粘贴 JSON 并根据您的要求选择选项
- 点击生成按钮