使用 Hibernate Tools 自動生成hbm.xml及model java檔案
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檔案