Hibernate是什麼,有多少好處,想必查找這類博文的都知道,所以就不多說了。下面是我對Hibernate簡單使用的一個小小的總結。與君(主要是剛入門的)共勉吧!
這裡僅僅是將上面提到的那些必須的jar包進行相關的路徑的配置。我這裡是將Hibernate基礎項目所需的jar包建立了一個自己的userlibrary。這樣方便以後自己隨意的導入。但是應該注意的是今後那些以來的文件的文職千萬不要隨意的變動了,否則可能會使得eclipse找不到。還有Mysql的JDBC的jar千萬不要忘記了。另外Junit作為一個調試的使用也是必不可少的。
步驟一:當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)即可。選擇默認的就可以了。
步驟二:創建Hibernate的配置文件:
只要是連接過MySQL數據庫,都是知道這些個字段的含義的,不再過多的敘述咯。
<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>
舉個簡單的小例子咯。如下:
import java.util.Date;/** * 學生類,設計原則要遵循javaBean的設計原則* * 1、共有的類2、屬性私有3、屬性使用setter和getter封裝4、提供龔鷗的不帶參數的默認的構造方法* * @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 + "]";}}同樣使用插件幫助我們生成。在src目錄下點擊右鍵,new--others--hibernate,選擇Hibernate XML Mapping file(hbm.xml)文件,找到我們要進行映射的學生類,然後選擇默認的即可。
然後在剛才創建的hibernate.cfg.xml文件中添加一個mapping標籤即可。如下
<mapping resource="Students.hbm.xml" />
這裡我使用Navacat軟件新建了一個字符集為UTF-8的數據庫。名稱為hibernate.
- 使用Junit進行測試:
- @Test註解:表明這是一個測試方法,一般為void的無參的throws異常的方法。
- @Before註解:表明這是一個初始化方法,用於初始化一些信息。
- @After註解:表明這是一個釋放資源的方法,用於收尾的工作。
點擊項目名,右鍵選擇創建一個source folder.作為我們的測試所用。我的為test。然後新建一個測試類就可以了。這裡我們需要測試的是我們的Students類,所以創建了一個StudentsTest就行。
可見為如下代碼:
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() {// 創建配置對象Configuration config = new Configuration().configure();// 創建服務註冊對象ServiceRegistry serviceRegister = new ServiceRegistryBuilder().applySettings(config.getProperties()) .buildServiceRegistry();// 創建會化工廠對象sessionFactory = config.buildSessionFactory(serviceRegister);// 會話對象session = sessionFactory.openSession();// 開啟事務transaction = session.beginTransaction();}@Test public void testSaveStudents() {Students s = new Students(1, "張三", "男", new Date(), "DLUT");// 保存對象進入數據庫session.save(s);}@After public void destory() {// 先提交事務transaction.commit();session.close();sessionFactory.close();}}我最後在測試方法上點擊了一下,發現報錯了。是org.hibernate.MappingException: Unknown Entity:Students。
然後我就看了看hibernate.cfg.xml文件,發現數據庫的一切都是正確的啊。也沒錯。
就想不明白了,然後查了查網上的相似的錯誤。也沒有發現正確的解決辦法,最後靈光一閃,肯定是映射文件出錯了。那麼到底是哪個呢,就一個一個的排查吧。然後我就找到了錯誤的根源了,不是Student.hbm.xml的錯誤,而是hibernate.cfg.xml中忘記了添加mapping的標籤。哈哈。這次,又運行了一下,成功了。
效果圖如下:
本文適合剛入門的Hibernate童鞋,所以並沒有一些很複雜的配置啊,和其他額外的處理啊什麼的。就是為了簡單。
這裡面使用到了Hibernate-tools插件,幫助我們乾了不少活。省事也省心了。個人建議安裝JBoss的這款,包含了不少的東西呢。
以上就是本文關於Hibernate初體驗及簡單錯誤排除代碼詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!