hibernate是什么,有多少好處,想必查找這類博文的都知道,所以就不多說了。下面是我對hibernate簡單使用的一個小小的總結(jié)。與君(主要是剛?cè)腴T的)共勉吧!
創(chuàng)建的順序
- 創(chuàng)建hibernate的配置文件
- 創(chuàng)建持久化的類
- 創(chuàng)建對象-關(guān)系的映射文件
- 通過hibernateapi編寫訪問數(shù)據(jù)庫的代碼
關(guān)于詳細的步驟
- 導(dǎo)入hibernate必須的jar包(hibernate-release-版本號.final\lib\required)
- 然后是導(dǎo)入mysql的jdbc的驅(qū)動(mysql-connector-java-版本號-bin.jar)
- 導(dǎo)入junit4的jar包(junit4-版本號.jar)
eclipse上進行環(huán)境的搭建
這里僅僅是將上面提到的那些必須的jar包進行相關(guān)的路徑的配置。我這里是將hibernate基礎(chǔ)項目所需的jar包建立了一個自己的userlibrary。這樣方便以后自己隨意的導(dǎo)入。但是應(yīng)該注意的是今后那些以來的文件的文職千萬不要隨意的變動了,否則可能會使得eclipse找不到。還有mysql的jdbc的jar千萬不要忘記了。另外junit作為一個調(diào)試的使用也是必不可少的。
創(chuàng)建配置文件
步驟一:當hibernate-tools 沒有自動生成配置文件必須的dtd文檔的時候,我們需要手動的進行添加。比如
hibernate-release-4.2.4.final\project\hibernate-core\src\main\resources\org\hibernate\hibernate-mapping-3.0.dtd
在項目的src目錄上點擊鼠標右鍵,然后使用hibernate插件,點擊hibernate configuration file(cfg.xml)即可。選擇默認的就可以了。
步驟二:創(chuàng)建hibernate的配置文件:
只要是連接過mysql數(shù)據(jù)庫,都是知道這些個字段的含義的,不再過多的敘述咯。
1
2
3
4
5
|
<property name= "connection.username" >root</property> <property name= "connection.password" >mysql</property> <property name= "connection.driver_class" >com.mysql.jdbc.driver</property> <property name= "connection.url" >jdbc:mysql: ///hibernate?useunicode=true&characterencoding=utf-8</property> <property name= "connection.dialect" >org.hibernate.dialect.mysqldialect</property> |
創(chuàng)建持久化類
舉個簡單的小例子咯。如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import java.util.date; /** * 學(xué)生類,設(shè)計原則要遵循javabean的設(shè)計原則 * * 1、共有的類 2、屬性私有 3、屬性使用setter和getter封裝 4、提供龔鷗的不帶參數(shù)的默認的構(gòu)造方法 * * @author administrator * */ public class students { private string sname; private int sid; private date birthday; private string gender; private string adress; public students() { } public string getsname() { return sname; } public void setsname(string sname) { this .sname = sname; } public int getsid() { return sid; } public void setsid( int sid) { this .sid = sid; } public date getbirthday() { return birthday; } public void setbirthday(date birthday) { this .birthday = birthday; } public string getgender() { return gender; } public void setgender(string gender) { this .gender = gender; } public string getadress() { return adress; } public void setadress(string adress) { this .adress = adress; } public students(string sname, int sid, date birthday, string gender, string adress) { this .sname = sname; this .sid = sid; this .birthday = birthday; this .gender = gender; this .adress = adress; } @override public string tostring() { return "students [sname=" + sname + ", sid=" + sid + ", birthday=" + birthday + ", gender=" + gender + ", adress=" + adress + "]" ; } } |
創(chuàng)建對象關(guān)系映射文件
同樣使用插件幫助我們生成。在src目錄下點擊右鍵,new--others--hibernate,選擇hibernate xml mapping file(hbm.xml)文件,找到我們要進行映射的學(xué)生類,然后選擇默認的即可。
然后在剛才創(chuàng)建的hibernate.cfg.xml文件中添加一個mapping標簽即可。如下
1
|
<mapping resource= "students.hbm.xml" /> |
創(chuàng)建自己的測試用的數(shù)據(jù)庫
這里我使用navacat軟件新建了一個字符集為utf-8的數(shù)據(jù)庫。名稱為hibernate.
編寫的第一個hibernate的測試的小例子
- 使用junit進行測試:
- @test注解:表明這是一個測試方法,一般為void的無參的throws異常的方法。
- @before注解:表明這是一個初始化方法,用于初始化一些信息。
- @after注解:表明這是一個釋放資源的方法,用于收尾的工作。
點擊項目名,右鍵選擇創(chuàng)建一個source folder.作為我們的測試所用。我的為test。然后新建一個測試類就可以了。這里我們需要測試的是我們的students類,所以創(chuàng)建了一個studentstest就行。
編寫最后的內(nèi)容,使用hibernateapi來操作數(shù)據(jù)庫
可見為如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import java.util.date; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import org.hibernate.service.serviceregistry; import org.hibernate.service.serviceregistrybuilder; import org.junit.after; import org.junit.before; import org.junit.test; public class studentstest { private sessionfactory sessionfactory; private session session; private transaction transaction; @before public void init() { // 創(chuàng)建配置對象 configuration config = new configuration().configure(); // 創(chuàng)建服務(wù)注冊對象 serviceregistry serviceregister = new serviceregistrybuilder().applysettings(config.getproperties()) .buildserviceregistry(); // 創(chuàng)建會化工廠對象 sessionfactory = config.buildsessionfactory(serviceregister); // 會話對象 session = sessionfactory.opensession(); // 開啟事務(wù) transaction = session.begintransaction(); } @test public void testsavestudents() { students s = new students( 1 , "張三" , "男" , new date(), "dlut" ); // 保存對象進入數(shù)據(jù)庫 session.save(s); } @after public void destory() { // 先提交事務(wù) transaction.commit(); session.close(); sessionfactory.close(); } } |
檢驗一下,實施的效果吧
我最后在測試方法上點擊了一下,發(fā)現(xiàn)報錯了。是org.hibernate.mappingexception: unknown entity:students。
然后我就看了看hibernate.cfg.xml文件,發(fā)現(xiàn)數(shù)據(jù)庫的一切都是正確的啊。也沒錯。
就想不明白了,然后查了查網(wǎng)上的相似的錯誤。也沒有發(fā)現(xiàn)正確的解決辦法,最后靈光一閃,肯定是映射文件出錯了。那么到底是哪個呢,就一個一個的排查吧。然后我就找到了錯誤的根源了,不是student.hbm.xml的錯誤,而是hibernate.cfg.xml中忘記了添加mapping的標簽。哈哈。這次,又運行了一下,成功了。
效果圖如下:
總結(jié)
本文適合剛?cè)腴T的hibernate童鞋,所以并沒有一些很復(fù)雜的配置啊,和其他額外的處理啊什么的。就是為了簡單。
這里面使用到了hibernate-tools插件,幫助我們干了不少活。省事也省心了。個人建議安裝jboss的這款,包含了不少的東西呢。
以上就是本文關(guān)于hibernate初體驗及簡單錯誤排除代碼詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/marksinoberg/article/details/51457071