问题
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.
Here is an example:
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.
回答1:
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.
I use Netbeans and Hibernate to generate all these models.
回答2:
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 Entities section), it might help.
来源:https://stackoverflow.com/questions/16523408/convert-sql-table-to-java-bean-class