天天看點

Hibernate Tools插件的安裝和使用

大學的時候使用Myeclipse自帶的功能自動産生hibernate的配置檔案cfg.xml、entity class、hbm.xml以及DAO等,工作後,就再也沒有用過Myeclipse了,而是用eclipse,是以想有沒有eclipse插件可以自動産生hibernate相關檔案的,最近在項目開發中,就發現了Hibernate Tools的使用。

在網上找了一些關于Hibernate Tools的安裝,使用裡面提到的兩個連結,發現按照内容說的步驟按部就班的話,會出現問題,提示無法安裝,跟eclipse的版本有問題。後來,Ricki在http://www.jboss.org/tools/download/下找到對應eclipse版本的update site,然後【Help】-【Sofeware Updates...】-【Add Site...】,輸入update site,選擇【Hibernate Tools】,單擊【Install...】即可。

安裝後,重新開機,檢視【Help】-【About Eclipse Platform】是否已經安裝成功,該視窗下方顯示的圖示是安裝在eclipse的所有插件。

打開【Window】-【Open Perspective】-【Others...】,選擇Hibernate,切換到Hibernate工作視圖。

選擇File-New-Hibernate Configuration File(cfg.xml)(此時已經把資料庫驅動包已置于工程的build path下)

[img]http://dl.iteye.com/upload/attachment/367740/93844539-dd7e-3b78-bcc0-e281ca258f7e.jpg[/img]

如果選中【Create a console configuration】,則按鈕【Next>】可觸發,單擊該按鈕,進入【Create Hibernate Console Configuration】視窗,進行更詳細的配置,這個在以後配置也行。

選擇【finish】後,在Hibernate Configuration中,可以看到:

[img]http://dl.iteye.com/upload/attachment/367743/b80551ef-b37c-329b-a069-8342439fffcd.jpg[/img]

展開Database,可以看到:

[img]http://dl.iteye.com/upload/attachment/367745/08dfb73f-3ec0-38de-ba27-d2c527a99432.jpg[/img]

接下來,選擇下圖中右下角紅色邊框中的按鈕

[img]http://dl.iteye.com/upload/attachment/367736/6c3cc3fd-ba73-37c0-8a0b-ac9a9fcbf6f7.jpg[/img]

選擇Hibernate Code Generation Configurations,在打開的視窗左邊右鍵create new....

[img]http://dl.iteye.com/upload/attachment/367747/fc3305c6-60f1-3f2b-9470-bbce5a934ca4.jpg[/img]

Output directory:用于存放java檔案的輸出目錄。

Package:存放java檔案的包。

選擇【Stetup...】按鈕,在彈出的視窗中,選擇【Create New...】,之後打開Configure Table filter,如果看不到Database schema下的表,單擊【Refresh】重新整理一下,這個視窗主要是選擇哪些表來生成java和hbm檔案,設定後,單擊【finish】傳回之前視窗,設定Exporters,如下:

[img]http://dl.iteye.com/upload/attachment/367750/dfe2ca63-d388-34ae-a98e-08a91c965b45.jpg[/img]

如果上面沒有問題的話,結果将在ricki.cheung.entity下産生entity java和hbm檔案,不過産生的檔案還需要手動修改。

有空的話,歡迎你到[url=http://hongfanshu.blogbus.com/]紅番薯[/url]逛逛

繼續閱讀