Hibernate初體驗及簡單錯誤排除代碼詳解
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)建配置文件
步驟一:當(dā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ù)庫,都是知道這些個字段的含義的,不再過多的敘述咯。
<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)建持久化類
舉個簡單的小例子咯。如下:
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標簽即可。如下
<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ù)庫
可見為如下代碼:
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)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
實例解決Java異常之OutOfMemoryError的問題
在本篇文章中,我們給大家分享了關(guān)于解決Java異常之OutOfMemoryError的問題的方法,有此需要的朋友們學(xué)習(xí)下。2019-02-02