如何创建 POJO?

发布于 2024-09-15 08:50:44 字数 841 浏览 4 评论 0原文

最近我开始听说“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

多彩岁月 2024-09-22 08:50:45

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;
    }
}

POJO class acts as a bean which is used to set and get the value.

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;
    }
}
牵你的手,一向走下去 2024-09-22 08:50:45

当您没有做任何事情来使您的类专门设计为与给定框架、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.

埖埖迣鎅 2024-09-22 08:50:45

根据 Martin Fowler

这个术语是在 2000 年 9 月 Rebecca Parsons、Josh MacKenzie 和我准备在一次会议上发表演讲时创造的。在演讲中,我们指出将业务逻辑编码为常规 Java 对象而不是使用 Entity 的许多好处豆子。我们想知道为什么人们如此反对在系统中使用常规对象,并得出结论,这是因为简单的对象缺乏花哨的名称。所以我们给了他们一个,而且很受欢迎。

一般来说,POJO 不受任何限制,任何 Java 对象都可以称为 POJO,但有一些方向。定义明确的 POJO 应遵循以下指示。

  1. POJO 中的每个变量都应声明为私有变量。
  2. 应使用公共可访问性覆盖默认构造函数。
  3. 每个变量都应该有其具有公共可访问性的 Setter-Getter 方法。
  4. 一般来说,POJO 应该重写 Object 的 equals()、hashCode() 和 toString() 方法(但这不是强制性的)。
  5. 重写Comparable接口的compare()方法用于排序(首选但不是强制)。

根据Java语言规范,POJO不应该

  1. 扩展预先指定的类
  2. 实现预先指定的接口
  3. 包含预先指定的注释

但是,开发人员和框架描述POJO仍然需要使用预先指定的注释来实现持久性,声明性事务等功能因此,我们的想法是,如果在添加任何注释之前该对象是 POJO,那么如果删除注释,它仍然可以被视为 POJO。

JavaBean 是一种特殊的 POJO,它是可序列化的,具有无参构造函数,并允许使用遵循简单命名约定的 getter 和 setter 方法来访问属性。

阅读更多关于普通旧 Java 对象 (POJO) 解释< /a>.

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.

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.

  1. Each variable in a POJO should be declared as private.
  2. Default constructor should be overridden with public accessibility.
  3. Each variable should have its Setter-Getter method with public accessibility.
  4. Generally POJO should override equals(), hashCode() and toString() methods of Object (but it's not mandatory).
  5. Overriding compare() method of Comparable interface used for sorting (Preferable but not mandatory).

And according to Java Language Specification, a POJO should not have to

  1. Extend pre-specified classes
  2. Implement pre-specified interfaces
  3. 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.

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.

Read more on Plain Old Java Object (POJO) Explained.

心病无药医 2024-09-22 08:50:45

主要有以下三种用于映射目的的选项:

  1. 序列化
  2. XML 映射
  3. POJO 映射。(普通旧 Java 对象)

在使用 pojo 类时,开发人员可以轻松地与数据库进行映射。
POJO 类是为数据库创建的,同时值对象类是使用 getter 和 setter 方法创建的,可以轻松保存内容。

因此,为了实现java与数据库之间的映射,实现了值对象和POJO类。

there are mainly three options are possible for mapping purpose

  1. serialize
  2. XML mapping
  3. POJO mapping.(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.

So,for the purpose of mapping in between java with database, value-objects and POJO classes are implemented.

瑾兮 2024-09-22 08:50:45
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 + "]";
    }
}
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 + "]";
    }
}
子栖 2024-09-22 08:50:45
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;
        }
    }
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;
        }
    }
郁金香雨 2024-09-22 08:50:45
  1. 文件-设置-插件-浏览存储库
  2. 搜索 RoboPOJOGenerator 并安装,重新启动 Android studio
  3. 打开项目并右键单击包,选择从 JSON 生成 POJO
  4. 在对话框中粘贴 JSON,然后根据您的要求选择选项
  5. 单击生成按钮
  1. File-setting-plugins-Browse repositories
  2. Search RoboPOJOGenerator and install, Restart Android studio
  3. Open Project and right click on package select on Generate POJO from JSON
  4. Paste JSON in dialogbox and select option according your requirements
  5. Click on Generate button
失而复得 2024-09-22 08:50:45

如果类没有陷入框架,那么从该类创建的对象将被识别为POJO

让我们看一些例子:

class MyServlet extends HttpServlet{
    //....
}

MyServlet 类的唯一含义是由 HttpServlet 类给出的。因此,从 MyServlet 创建的对象不是 POJO

class MyClass implements Serializable{
    //...
}

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:

class MyServlet extends HttpServlet{
    //....
}

The sole meaning of MyServlet class is given by the HttpServlet class. Therefore the objects created from the MyServlet are not POJOs.

class MyClass implements Serializable{
    //...
}

The Serializable interface does not give a meaning to the class MyClass. Therefore the objects created from the MyClass are POJOs.

夜夜流光相皎洁 2024-09-22 08:50:44

POJO 只是一个普通的、旧的 Java Bean,删除了限制。 Java Bean 必须满足以下要求:

  1. 默认无参数构造函数
  2. 对于名为 foo 的可变属性,遵循 getFoo(或布尔值的 isFoo)和 setFoo 方法的 Bean 约定;如果 foo 是不可变的,则不要使用 setFoo。
  3. 必须实现 java.io.Serialized

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。

这是一个您可以理解的示例:

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; } 
}

A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:

  1. Default no-arg constructor
  2. 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.
  3. Must implement 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.

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:

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; } 
}
¢好甜 2024-09-22 08:50:44

POJO:- POJO 是一个 Java 对象,除了 Java 语言规范强制执行的限制之外,不受任何限制的约束。

POJO 的属性

  1. 所有属性必须是公共 setter 和 getter 方法
  2. 所有实例变量都应该是私有的
  3. 不应扩展预先指定的类。
  4. 不应实现预先指定的接口。
  5. 不应包含预先指定的注释。
  6. 它可能没有任何参数构造函数

POJO 的示例

public class POJO {

    private String value;

    public String getValue() {
         return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

POJO:- POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.

Properties of POJO

  1. All properties must be public setter and getter methods
  2. All instance variables should be private
  3. Should not Extend prespecified classes.
  4. Should not Implement prespecified interfaces.
  5. Should not contain prespecified annotations.
  6. It may not have any argument constructors

Example of POJO

public class POJO {

    private String value;

    public String getValue() {
         return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
善良天后 2024-09-22 08:50:44

POJO 是一个普通旧 Java 对象

从我链接到的维基百科文章:

在计算软件中,POJO 是一个
普通旧 Java 对象的缩写。这
名称用于强调给定的
object 是一个普通的 Java 对象,而不是
一个特殊的物体,特别是
不是 Enterprise JavaBean

您的类似乎已经是 POJO。

A POJO is a Plain Old Java Object.

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

Your class appears to already be a POJO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文