hibernate反向工程步骤(由数据库表创建java实体类)
(1)先创建(右键——new——web project)创建web项目程序。填写好工程名字,勾选java ee 5.0
(2)接着创建hibernate.cfg.xml配置文件,点击工程——右键——add Hibernate capabilities——next
(3)继续next ,输入已经在db browsers创建好的名,选择自动出现用户名字和密码
(4)自动跳转这个页面,表示创建成功。接着创建实体类 EMP,相关的字段、以及字段的get和set方法,加上一个空的构造方法,一个String方法。
package entity;
import java.util.Date;
public class Emp implements java.io.Serializable {
// Fields
private Integer empno;
private String ename;
private String job;
private Short mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
// Constructors
/** default constructor */
public Emp() {
}
/** full constructor */
public Emp(String ename, String job, Short mgr, Date hiredate, Double sal, Double comm, Integer deptno) {
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Integer getEmpno() {
return this.empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return this.ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
public Short getMgr() {
return this.mgr;
}
public void setMgr(Short mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return this.hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getSal() {
return this.sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return this.comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp [comm=" + comm + ", deptno=" + deptno + ", empno=" + empno
+ ", ename=" + ename + ", hiredate=" + hiredate + ", job="
+ job + ", mgr=" + mgr + ", sal=" + sal + "]";
}
}
(5)创建Emp.hbm.xml配置文件,相对应的字段和类型与类相对应。同时在hibernate.cfg.xml配置文件中<session-factory></session-factory>里面写上:<mapping resource="entity/Emp.hbm.xml" />映射
Emp.hbm.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/car
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql2</property>
<mapping resource="entity/Emp.hbm.xml" />
</session-factory>
</hibernate-configuration>
(6)创建InitEvn类
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
//初始化环境工具类
public class InitEvn {
public static void main(String[] args) {
//Hibernate的配置类对象,用于创建SessionFactory.
Configuration conf = new Configuration().configure();
SchemaExport se = new SchemaExport(conf);
//正向工程 (是否显示脚本, 是否执行脚本)
se.create(true, true);
}
}
(7)运行程序,创建成功。
(8)创建.sql后缀名的文档,查询表格show tables;是否创建成功
(9)总结:正向工程的映射关系必须注意,弄清楚否则会出现比较多的问题,而反向工程直接在数据库创建好再用就可以了。