1.安装Hibernate Tools工具
Help-->Eclipse Marketpalce...-->搜索JBoss tools->install
勾选Hibernate tools,点击next,进行安装。
安装后,重启Eclipse,启用Hibernate perspective: Windows->Perspective->Open Perspective->Other...->选择Hibernate
2.新建一个Dynamic web project进行自动化代码生成
可以参考 http://blog.csdn.net/caixiexin/article/details/7019307
1)新建Dynamic web project,并将mysql的connector jar放到WEB-INF/lib下
2) 右键工程->new->other->选择hibernate configuration file(cfg.xml)->
Database dialect: mysql
Driver class: com.mysql.jdbc.Driver
Connection URL: jdbc:mysql://localhost:3306/exam
username: root
password: root
3) 点击Finish
3.配置Run设置
Run->Run as configurations...->Hibernate Console Configuration->双击新建一个配置项
1)Main tab中选中Annotations, hibernate version: 3.5, Project选择当前项目,Configuration file选择cfg.xml存放的位置(一般放在src目录)
2) Options tab中Database dialect选择mysql
3) Common tab中shared file:选择当前项目的src目录,勾选run, Allocate console, Input file输入: ${workspace_loc:/当前项目目录名}
4) 点击Apply
4.配置reveng.xml
1) 右键工程->New->Other...->Hibernate Reverse Engineering File->选择reveng.xml存放位置(一般放在src下)
2) 点击next->选择Configuration->点击refresh->选择table->点击Include->点击Finish
5.自动化生成Model对应的hbm.xml及java文件
1) 切换视图到Hibernate
2) 在Run按钮的旁边的旁边有个小按钮,选择Hibernate Code Generation Configurations...
3) 新建Hibernate Code Generation
Main Tab中选择Console Configuration,选择Output目录(一般选择src),勾选Reverse Enginerring,输入package,选择reveng.xml
Exporters Tab中勾选Use Java 5 Syntax, 勾选Domain, Hibernate xml file(.hbm.xml),Hibernate cfg xml file
点击Run,生成java及hbm.xml文件