1. 程式人生 > >Intellij IDEA下的第一個Hibernate項目

Intellij IDEA下的第一個Hibernate項目

popu 目錄 tran 事務 rac ocl bar 應該 出生日期

轉自:http://blog.csdn.net/qq_15096707/article/details/51419304 ,如需轉載,請聯系原博主。

參考:intellij配置hibernate自動生成hbm.xml文件 從零開始用Intellij idea14創建hibernate項目

下面我要講的創建方式,可能更加原生態,更加類似於Eclipse下創建Hibernate項目的方式,我想這也有助於對在Intellij IDEA下創建Hibernate項目的理解。

首先需要在Intellij IDEA下創建一個項目Project,相當於Eclipse下的workspace(工作空間),當然如果你此時選擇了創建Hibernate項目的方式,Intellij 在創建Project成功後會在Project下創建這一Hibernate項目。可能看起來有點奇怪,沒關系,我們可以把默認創建的東西刪除,然後創建我們的Module,相當於Eclipse下的Project。

創建Module --》選擇 Java Enterprise選項卡,點擊右側,勾選Web Application 和 Hibernate,如下:

技術分享圖片

選擇右下角的 Configure... ,選擇Module Library:

技術分享圖片

點擊下一步,輸入Module的名稱,這裏我取名為:Hibernate_00,如:

技術分享圖片

等待 Hibernate 相關jar包下載完畢……

技術分享圖片

技術分享圖片

還需要添加mysql-jdbc jar包 和 junit jar包(junit jar包實際中可以不添加,根據實際需要進行添加):

對Hibernate_00 右鍵,選擇 Open Module Settings,或者按F4:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片 選擇 From Maven。

技術分享圖片

技術分享圖片

以同樣的方式下載 junit jar包並進行添加:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

為方便以後創建Hibernate項目,可以為 hibernate 的配置文件創建模板:

技術分享圖片

技術分享圖片

技術分享圖片

hibernate.cfg.xml 模板內容如下(實際中應該進行相應的修改):

[html] view plain copy
  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="connection.username">root</property>
  8. <property name="connection.password"></property>
  9. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
  11. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  12. <property name="show_sql">true</property>
  13. <property name="format_sql">true</property>
  14. <property name="hbm2ddl.auto">create</property>
  15. <mapping resource="Students.hbm.xml"/>
  16. <!-- DB schema will be updated if needed -->
  17. <!-- <property name="hbm2ddl.auto">update</property> -->
  18. </session-factory>
  19. </hibernate-configuration>


同樣地方式,創建對象/關系映射的配置文件模板,這裏創建 entity2.hbm.xml 模板如下(實際中應該進行相應地修改):

[html] view plain copy
  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="Students" table="students">
  7. <id name="sid" type="int">
  8. <column name="sid"/>
  9. <generator class="assigned"/>
  10. </id>
  11. <property name="sname" type="java.lang.String">
  12. <column name="sname"/>
  13. </property>
  14. <property name="gender" type="java.lang.String">
  15. <column name="gender"/>
  16. </property>
  17. <property name="birthday" type="java.util.Date">
  18. <column name="birthday"/>
  19. </property>
  20. <property name="address" type="java.lang.String">
  21. <column name="address"/>
  22. </property>
  23. </class>
  24. </hibernate-mapping>
現在準備工作做好後,我們可以根據模板在src下創建 hibernate 配置文件 hibernate.cfg.xml:(參考:Creating hibernate.cfg.xml) [html] view plain copy
  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="connection.username">root</property>
  8. <property name="connection.password"></property>
  9. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
  11. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  12. <property name="show_sql">true</property>
  13. <property name="format_sql">true</property>
  14. <property name="hbm2ddl.auto">create</property>
  15. <mapping resource="Students.hbm.xml"/>
  16. <!-- DB schema will be updated if needed -->
  17. <!-- <property name="hbm2ddl.auto">update</property> -->
  18. </session-factory>
  19. </hibernate-configuration>

在src下創建 Students 類: [java] view plain copy
  1. import java.util.Date;
  2. /**
  3. * Created by DreamBoy on 2016/5/15.
  4. */
  5. //學生類
  6. public class Students {
  7. //1. 必須為公有的類
  8. //2. 必須提供公有的不帶參數的默認的構造方法
  9. //3. 屬性私有
  10. //4. 屬性setter/getter封裝
  11. private int sid; //學號
  12. private String sname; //姓名
  13. private String gender; //性別
  14. private Date birthday; //出生日期
  15. private String address; //地址
  16. public Students() {
  17. }
  18. public Students(int sid, String sname, String gender, Date birthday, String address) {
  19. this.sid = sid;
  20. this.sname = sname;
  21. this.gender = gender;
  22. this.birthday = birthday;
  23. this.address = address;
  24. }
  25. public int getSid() {
  26. return sid;
  27. }
  28. public void setSid(int sid) {
  29. this.sid = sid;
  30. }
  31. public String getSname() {
  32. return sname;
  33. }
  34. public void setSname(String sname) {
  35. this.sname = sname;
  36. }
  37. public String getGender() {
  38. return gender;
  39. }
  40. public void setGender(String gender) {
  41. this.gender = gender;
  42. }
  43. public Date getBirthday() {
  44. return birthday;
  45. }
  46. public void setBirthday(Date birthday) {
  47. this.birthday = birthday;
  48. }
  49. public String getAddress() {
  50. return address;
  51. }
  52. public void setAddress(String address) {
  53. this.address = address;
  54. }
  55. @Override
  56. public String toString() {
  57. return "Students{" +
  58. "sid=" + sid +
  59. ", sname=‘" + sname + ‘\‘‘ +
  60. ", gender=‘" + gender + ‘\‘‘ +
  61. ", birthday=" + birthday +
  62. ", address=‘" + address + ‘\‘‘ +
  63. ‘}‘;
  64. }
  65. }

在src下創建 對象/關系映射的配置文件(根據模板進行創建) Students.hbm.xml [html] view plain copy
  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="Students" table="students">
  7. <id name="sid" type="int">
  8. <column name="sid"/>
  9. <generator class="assigned"/>
  10. </id>
  11. <property name="sname" type="java.lang.String">
  12. <column name="sname"/>
  13. </property>
  14. <property name="gender" type="java.lang.String">
  15. <column name="gender"/>
  16. </property>
  17. <property name="birthday" type="java.util.Date">
  18. <column name="birthday"/>
  19. </property>
  20. <property name="address" type="java.lang.String">
  21. <column name="address"/>
  22. </property>
  23. </class>
  24. </hibernate-mapping>


在hibernate.cfg.xml 配置文件中添加Students對象的映射。(前面的配置文件中已添加)

[html] view plain copy
  1. <mapping resource="Students.hbm.xml"/>


在Module下創建test目錄,為了能在該目錄中創建java類,需要將目錄修改為 Sources Root:

技術分享圖片

此時才可以創建java類:StudentsTest.java

(這裏使用到 junit 中的註解 @Test、@Before、@After)

[java] view plain copy
  1. import org.hibernate.Session;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.Transaction;
  4. import org.hibernate.cfg.Configuration;
  5. import org.hibernate.service.ServiceRegistry;
  6. import org.hibernate.service.ServiceRegistryBuilder;
  7. import org.junit.After;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import java.util.Date;
  11. /**
  12. * Created by DreamBoy on 2016/5/15.
  13. */
  14. //測試類
  15. public class StudentsTest {
  16. private SessionFactory sessionFactory;
  17. private Session session;
  18. private Transaction transaction;
  19. @Before
  20. public void init() {
  21. //創建配置對象
  22. Configuration config = new Configuration().configure();
  23. //創建服務註冊對象
  24. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
  25. //創建會話工廠對象
  26. sessionFactory = config.buildSessionFactory(serviceRegistry);
  27. //會話對象
  28. session = sessionFactory.openSession();
  29. //開啟事務
  30. transaction = session.beginTransaction();
  31. }
  32. @After
  33. public void destory() {
  34. transaction.commit(); //提交事務
  35. session.close(); //關閉會話
  36. sessionFactory.close(); //關閉會話工廠
  37. }
  38. @Test
  39. public void testSaveStudents() {
  40. //生成學生對象
  41. Students s = new Students(1, "張三豐", "男", new Date(), "武當山");
  42. session.save(s); //保存對象進入數據庫
  43. }
  44. }


在hibernate.cfg.xml配置文件中,可以知道,這裏我配置使用的數據庫,名為hibernate,所以我們需要在運行程序前,使用mysql創建好 hibernate 數據庫,不用創建Students表。

技術分享圖片

運行 StudentsTest.java ,可能會出現如下錯誤:

技術分享圖片

大概是java編譯輸出的位置出錯了,所以要對output輸出路徑進行配置:

這裏選擇在當前Module下classes文件夾下:

技術分享圖片

再次運行程序:

技術分享圖片

技術分享圖片

程序運行成功,我們可以查看一下數據庫,會發現程序已經幫我們自動創建好了數據表,並添加了一條數據:

技術分享圖片技術分享圖片

成功運行Hibernate項目。

Intellij IDEA下的第一個Hibernate項目