Hibernate Tools的官方网站:http://hibernate.org/tools/
Step1、安装好Hibernate Tools,建立一个Dynamic web project,工程名为“test”。
Step2、以Mysql为示例,建立相应的测试数据库及表,如下所示:
- mysql> use test;
- Database changed
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | customer |
- | login |
- +----------------+
- 2 rows in set (0.10 sec)
Step3、将MySql的Jdbc驱动库(mysql-connector-java-5.0.8-bin.jar)拷贝至lib目录。
Step4、利用Hibernate Tools生成Hibernate的cfg.xml。如下图:
直接默认转到下一步的配置界面,配置如下图:
点击完成(Finish)后,它将生成如下xml的配置文件(hibernate.cfg.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">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
- <property name="hibernate.connection.password">gjs@y1</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
- </session-factory>
- </hibernate-configuration>
Step5、利用Hibernate Tools生成Hibernate的Console配置。如下图:
点击完成(Finish)后,它将生成配置文件,如下图:
Step6、利用Hibernat Tools生成Hibernate的reveng.xml配置。如下图:
直接默认转到下一步的配置界面,配置如下图:
点击完成(Finish)后,它将生成如下xml的配置文件(hibernate.reveng.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-reverse-engineering PUBLIC
- "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
- <hibernate-reverse-engineering>
- <table-filter match-catalog="test" match-name="customer" />
- <table-filter match-catalog="test" match-name="login" />
- </hibernate-reverse-engineering>
Step7、最后一步了,生成映射文件和Java代码。如下图:
点击运行(Run)后,它将生成如下映射文件和Java代码文件,如下图:
参考:http://www.cnblogs.com/abllyboy/archive/2010/12/23/1914577.html