如何创建 POJO?
最近我开始听说“POJO”(普通旧 Java 对象)。我用谷歌搜索了一下,但仍然不太理解这个概念。谁能给我一个 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;
}
}
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?
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?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
POJO 类充当一个 bean,用于设置和获取值。
POJO class acts as a bean which is used to set and get the value.
当您没有做任何事情来使您的类专门设计为与给定框架、ORM 或其他需要特殊类的系统一起使用时,您就有一个普通的旧 Java 对象 (POJO)。
讽刺的是,创造这个词的原因之一是,人们在明智的情况下会避开它们,有些人得出结论,这是因为他们没有一个花哨的名字。具有讽刺意味的是,因为你的问题表明这种方法是有效的。
比较旧的 POD“纯旧数据”,意味着 C++ 类不做任何 C 结构不能做的事情(或多或少,非析构函数或普通构造函数的非虚拟成员不会阻止它被被认为是 POD),以及 .NET 中较新(且更直接可比)的 POCO“普通旧 CLR 对象”。
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.
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.
根据 Martin Fowler
一般来说,POJO 不受任何限制,任何 Java 对象都可以称为 POJO,但有一些方向。定义明确的 POJO 应遵循以下指示。
根据Java语言规范,POJO不应该
但是,开发人员和框架描述POJO仍然需要使用预先指定的注释来实现持久性,声明性事务等功能因此,我们的想法是,如果在添加任何注释之前该对象是 POJO,那么如果删除注释,它仍然可以被视为 POJO。
阅读更多关于普通旧 Java 对象 (POJO) 解释< /a>.
According to Martin Fowler
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.
And according to Java Language Specification, a POJO should not have to
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.
Read more on Plain Old Java Object (POJO) Explained.
主要有以下三种用于映射目的的选项:
在使用 pojo 类时,开发人员可以轻松地与数据库进行映射。
POJO 类是为数据库创建的,同时值对象类是使用 getter 和 setter 方法创建的,可以轻松保存内容。
因此,为了实现java与数据库之间的映射,实现了值对象和POJO类。
there are mainly three options are possible for mapping purpose
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.
So,for the purpose of mapping in between java with database, value-objects and POJO classes are implemented.
如果类没有陷入框架或库,那么从该类创建的对象将被识别为POJO 。
让我们看一些例子:
MyServlet 类的唯一含义是由 HttpServlet 类给出的。因此,从 MyServlet 创建的对象不是 POJO。
Serialized 接口没有赋予 MyClass 类任何意义。因此,从 MyClass 创建的对象是POJO。
If a class is not bogged down from a framework or a library, then an object created from that class is recognized as a POJO.
Let's see some examples:
The sole meaning of MyServlet class is given by the HttpServlet class. Therefore the objects created from the MyServlet are not POJOs.
The Serializable interface does not give a meaning to the class MyClass. Therefore the objects created from the MyClass are POJOs.
POJO 只是一个普通的、旧的 Java Bean,删除了限制。 Java Bean 必须满足以下要求:
POJO 不强制执行其中任何一项。顾名思义:在 JDK 下编译的对象可以被视为普通旧 Java 对象。没有应用程序服务器,没有基类,不需要使用接口。
首字母缩略词 POJO 是对 EJB 2.0 的反应,EJB 2.0 需要多个接口、扩展基类和大量方法来完成简单的事情。有些人(其中包括 Rod Johnson 和 Martin Fowler)反对这种复杂性,并寻求一种无需编写 EJB 即可实现企业级解决方案的方法。
马丁·福勒创造了一个新的缩写词。
Rod Johnson 写了“没有 EJB 的 J2EE”,写了 Spring,对 EJB 产生了足够的影响,因此 3.1 版本看起来非常像 Spring 和 Hibernate,并从中获得了 VMWare 的甜蜜 IPO。
这是一个您可以理解的示例:
A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:
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.
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.
Martin Fowler coined a new acronym.
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.
Here's an example that you can wrap your head around:
POJO:- POJO 是一个 Java 对象,除了 Java 语言规范强制执行的限制之外,不受任何限制的约束。
POJO 的属性
POJO 的示例
POJO:- POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.
Properties of POJO
Example of POJO
POJO 是一个普通旧 Java 对象。
从我链接到的维基百科文章:
您的类似乎已经是 POJO。
A POJO is a Plain Old Java Object.
From the wikipedia article I linked to:
Your class appears to already be a POJO.