1. 程式人生 > >Hibernate(二)----Hibernate前期準備

Hibernate(二)----Hibernate前期準備

前言:使用hibernate之前,需要進行一系列的準備工作,主要包括:下載hibernate、匯入hibernate的相關jar包、匯入hibernate-configuration.dtd檔案、編寫配置檔案、配置對映檔案的路徑等;

1. 下載hibernate

使用hibernate是需要一些支撐檔案的,所以我們需要去下載hibernate,可以去hibernate官網上下載,也可以到我的百度雲盤下載。(連結:https://pan.baidu.com/s/1zEDpno32C-wisFQyAthuMg 密碼:6on0)

下載完畢解壓後,開啟檔案,會看到hibernate-release-5.0.7.Final目錄結構如下:

 

接下來對這些目錄做一下介紹:

documentation檔案:存放了hibernate的相關文件,包括參考文件的api文件。

lib檔案:存放hibernate編譯和執行的依賴jar包,其中的required子目錄是hibernate執行必須依賴的jar包。

project檔案:存放hibernate的各種相關原始碼。

2. 匯入hibernate的相關jar包

執行hibernate必須依賴的jar包在lib/required資料夾下;在myeclipse中的web專案匯入jar包的方式:複製jar包,然後把複製的jar包貼上到專案的WebRoot/WEB-INF/lib路徑下就可以了。

3. 匯入hibernate-configuration.dtd檔案

hibernate-configuration.dtd檔案是一種dtd型別的檔案,這類檔案可以在不聯網的情況在編寫配置檔案的時候有程式碼提示功能。(如果在聯網的情況下,會自動快取);所需要的dtd檔案可以去我的百度雲盤裡下載(連結:https://pan.baidu.com/s/1QoPI7dAffgNAhh1ocIkf-Q 密碼:igio)

dtd資料夾裡面有兩個檔案,如下圖:

 

一個是hibernate-configuration.dtd檔案,另一個是hibernate-mapping.dtd檔案,第一個檔案是進行基本配置的程式碼提示檔案;第二個檔案是後面進行物件與資料庫表的對映的程式碼提示檔案,第二個檔案可以先忽略。

接下來看一下如何匯入hibernate-configuration.dtd檔案?首先,點選myeclipse的Windows-->Preferences,在輸入框裡搜尋 catalog,如下圖所示:

 

然後,點選 add 進行檔案的新增,選擇 Catalog Entry,在Location一欄,點選File System去選擇hibernate-configuration.dtd檔案;Key type選擇URI;Key中填寫“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”(關於這一地址可以在Web App Librarias/hibernate-core-5.0.7.Final.jar/org.hibernate/hibernate-configuration-3.0.dtd中找到),然後點選OK就可以了。hibernate的配置dtd檔案就被成功添加了。

 

4. 編寫hibernate.cfg.xml配置檔案

hibernate-configuration.dtd檔案已經成功匯入了,現在我們來編寫與資料庫連線的配置檔案,在src目錄下新建一個名為“hibernate.cfg.xml”的xml型別的檔案(也可以命名為其他名字,但是“hibernate.cfg.xml”是預設的名字)。在上文中說過的hibernate-configuration-3.0.dtd檔案中,有一段如下圖所示的配置,將這段配置複製下來,貼上到剛剛新建的hiberbate.cfg.xml檔案中。

 

hibernate的基本配置檔案的結構是

<hibernate-configuration>

<session-factory>

<!-- 基本配置 -->

</session-factory>

</hibernate-configuration>

所以,要在配置檔案把<hibernate-configuration>和<session-factory>標籤新增上。接下來可以通過hibernate-release-5.0.7.Final\project\etc\hibernate.properties來看下具體的配置,在hibernate.properties檔案中搜索“MySQL”,可以搜到如下圖所示的配置:

 

其中:hibernate.connection.driver_class是資料庫驅動類的配置;

hibernate.connection.url是連線資料庫的URL;

hibernate.connection.username是資料庫的名字;

hibernate.connection.password是資料庫的密碼;

hibernate.dialect是資料庫方言;

每一個配置空格的後面是選項;

好了,到此我們已經大體上知道了資料庫配置的內容是什麼了。那就來學習以下如何書寫資料庫配置吧。

<session-factory>標籤下書寫

<property name=”hibernate.connection..driver_class”>com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">pgmx0835</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

很明顯,資料庫配置的格式是<property name=配置名>具體配置內容</property>

5. 配置對映檔案的路徑

<session-factory>標籤下繼續編寫配置,書寫如下內容:

<mapping resource=.../.../XXX.hbm.xml/>

因為我們後面還會進行java物件與資料庫表的對映,所以需要知道具體的對映路徑。(在後續的講解中會講到這一部分,暫時不理解沒關係,這裡先這樣寫。)

6. 總結

這次我們講解了使用hibernate之前必須要的一些配置,可以知道,hibernate與資料庫連線的配置檔案的結構大致如下:

<hibernate-configuration>

<session-factory>

<!-- 與資料庫的配置 -->

<property name=配置名>具體的配置內容</property>

<!-- 對映配置檔案路徑設定 -->

<mapping resource=.../.../XXX.hbm.xml>

</session-factory>

</hibernate-configuration>