将 SQL 表转换为 java bean 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16523408/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:12:36  来源:igfitidea点击:

convert SQL table to java bean class

javasqljavabeans

提问by Nabil Salah

I'm working in web application using a huge number of SQL tables. I need to create for each table a java bean class. I'm searching for a tool that can convert SQL table to a java bean class. It will help to save time.

我正在使用大量 SQL 表的 Web 应用程序中工作。我需要为每个表创建一个 java bean 类。我正在寻找一种可以将 SQL 表转换为 java bean 类的工具。这将有助于节省时间。

Here is an example:

下面是一个例子:

studentTable (studentId, firstname, lastname, yearLevel)

studentTable (studentId, firstname, lastname, yearLevel)

-->

public class Student {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int studentId;
    @Column
    private String firstname;
    @Column
    private String lastname;
    @Column
    private int yearLevel;

    public Student(){}
    public Student(int studentId, String firstname, String lastname,
            int yearLevel) {
        super();
        this.studentId = studentId;
        this.firstname = firstname;
        this.lastname = lastname;
        this.yearLevel = yearLevel;
    }
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public int getYearLevel() {
        return yearLevel;
    }
    public void setYearLevel(int yearLevel) {
        this.yearLevel = yearLevel;
    }
}

Do you have any idea about such tool.

你对这样的工具有什么想法吗?

采纳答案by JeroenVP

Use Hibernate.

使用休眠。

It allows you to generate model-classes for your database tables.

它允许您为数据库表生成模型类。

The "function" is called: Hibernate mapping files and POJO'S from database, also use Hibernate Reverse Engineering Wizard.

“函数”被称为:Hibernate mapping files and POJO'S from database,也使用Hibernate Reverse Engineering Wizard

I use Netbeans and Hibernate to generate all these models.

我使用 Netbeans 和 Hibernate 来生成所有这些模型。

回答by Eugene

Eclipse has such functionality, but I'm not sure that it works correctly for all cases and that it properly handles relations between tables. In any case take a look at this link(Creating and Modifying Entitiessection), it might help.

Eclipse 具有这样的功能,但我不确定它是否适用于所有情况以及它是否正确处理了表之间的关系。无论如何,请查看此链接创建和修改实体部分),它可能会有所帮助。