Intellij IDEA下的第一個Hibernate項目


轉自: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.   
  13.         <property name="show_sql">true</property>  
  14.         <property name="format_sql">true</property>  
  15.         <property name="hbm2ddl.auto">create</property>  
  16.   
  17.         <mapping resource="Students.hbm.xml"/>  
  18.         <!-- DB schema will be updated if needed -->  
  19.         <!-- <property name="hbm2ddl.auto">update</property> -->  
  20.     </session-factory>  
  21. </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.   
  7.     <class name="Students" table="students">  
  8.         <id name="sid" type="int">  
  9.             <column name="sid"/>  
  10.             <generator class="assigned"/>  
  11.         </id>  
  12.         <property name="sname" type="java.lang.String">  
  13.             <column name="sname"/>  
  14.         </property>  
  15.         <property name="gender" type="java.lang.String">  
  16.             <column name="gender"/>  
  17.         </property>  
  18.         <property name="birthday" type="java.util.Date">  
  19.             <column name="birthday"/>  
  20.         </property>  
  21.         <property name="address" type="java.lang.String">  
  22.             <column name="address"/>  
  23.         </property>  
  24.     </class>  
  25. </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.   
  13.         <property name="show_sql">true</property>  
  14.         <property name="format_sql">true</property>  
  15.         <property name="hbm2ddl.auto">create</property>  
  16.   
  17.         <mapping resource="Students.hbm.xml"/>  
  18.         <!-- DB schema will be updated if needed -->  
  19.         <!-- <property name="hbm2ddl.auto">update</property> -->  
  20.     </session-factory>  
  21. </hibernate-configuration>  

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

       在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.   
  7.     <class name="Students" table="students">  
  8.         <id name="sid" type="int">  
  9.             <column name="sid"/>  
  10.             <generator class="assigned"/>  
  11.         </id>  
  12.         <property name="sname" type="java.lang.String">  
  13.             <column name="sname"/>  
  14.         </property>  
  15.         <property name="gender" type="java.lang.String">  
  16.             <column name="gender"/>  
  17.         </property>  
  18.         <property name="birthday" type="java.util.Date">  
  19.             <column name="birthday"/>  
  20.         </property>  
  21.         <property name="address" type="java.lang.String">  
  22.             <column name="address"/>  
  23.         </property>  
  24.     </class>  
  25. </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.   
  11. import java.util.Date;  
  12.   
  13. /** 
  14.  * Created by DreamBoy on 2016/5/15. 
  15.  */  
  16. //測試類  
  17. public class StudentsTest {  
  18.     private SessionFactory sessionFactory;  
  19.     private Session session;  
  20.     private Transaction transaction;  
  21.   
  22.     @Before  
  23.     public void init() {  
  24.         //創建配置對象  
  25.         Configuration config = new Configuration().configure();  
  26.         //創建服務注冊對象  
  27.         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();  
  28.         //創建會話工廠對象  
  29.         sessionFactory = config.buildSessionFactory(serviceRegistry);  
  30.         //會話對象  
  31.         session = sessionFactory.openSession();  
  32.         //開啟事務  
  33.         transaction = session.beginTransaction();  
  34.     }  
  35.   
  36.     @After  
  37.     public void destory() {  
  38.         transaction.commit(); //提交事務  
  39.         session.close(); //關閉會話  
  40.         sessionFactory.close(); //關閉會話工廠  
  41.     }  
  42.   
  43.     @Test  
  44.     public void testSaveStudents() {  
  45.         //生成學生對象  
  46.         Students s = new Students(1, "張三豐", "男", new Date(), "武當山");  
  47.         session.save(s); //保存對象進入數據庫  
  48.     }  
  49. }  


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

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

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

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

       再次運行程序:

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

       成功運行Hibernate項目。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM