建立好数据库后,点击一个键,代码就生成了,这实在是份十分惬意的事情,前面有介绍hibernate tools就可以生成代码,那么我们怎么利用它来生成代码呢。
下面就把详细步骤贴出来,同样,有图有真相。
step1:安装好hibernate tools,建立一个dynamic web project,笔者工程名为“hibernatetest”
step2:笔记以mysql为示例,所以安装一个mysql,建立相应测试数据库及表,下图为笔者的数据库及表
step3:将mysql的jdbc驱动拷贝至lib目录,笔者的为mysql-connector-java-5.1.14-bin.jar,可以去mysql官网下载
step4:利用hibernate tools生成hibernate.cfg.xml文件,如下图:
点击后可以对hibernate.cfg.xml命令,不管它直接默认转
此文来自: 马开东博客 转载请注明出处 网址: http://www.makaidong.com
到下一步的配置界面,笔者的配置如下图:
点击完成后,它将生成如下xml的配置文件
encoding=
?><br><!doctype hibernate-configuration public<br>
<br>
><br><hibernate-configuration><br> <session-factory><br> <property name=
>com.mysql.jdbc.driver</property><br> <property name=
>
</property><br> <property name=
>jdbc:mysql://localhost:
/javadb</property><br> <property name=
>root</property><br> <property name=
>org.hibernate.dialect.mysqldialect</property><br> </session-factory><br></hibernate-configuration><br>
step5:hibernate console 配置
a.右键工程,点击run as-run configurations进入如下界面并选择hibernate console configuration:
b.新建一个configure名为hibernateconsole,名字可随个人爱好来命名
笔者各标签页配置如下:
step6:利用hibernat tools生成reveng.xml
a.工程中右键如下图,添加reveng.xml
b.没啥说的命个名,选择放置位置
c.按照箭头方向进行操作,选择console的配置,再点击刷新,就能看到数据库中的表,将它们include进来,如下图
d:最终效果
step7:这是最后一步了,激动人心的时刻就要到了
a.在eclipse的工具栏中将hibernate code generate显示出来,显示出来后点击hibernate code generate configurations,如下图:
如果找不到这个按钮:
b.main标签页中选择控制台,选择文件输出路径,包名,reveng.xml文件
c.exporters标签页,选择要生成的文件,记得勾上hebernate xml mappings及hebernate xml configuration,它们将自动生成映射文件并注册,
配置好后,点击run即可生成代码。
大功告成,最后的生成的代码结构如下图: