2016年7月22日 星期五

建置第一個Hibernate In Intellj

環境配置:

  • Intellj 1.4
  • JDBC4
  • Hibernate jar (少其中一個可能都會出錯)




















內容參考至:




Step1.檔案結構目錄
Step2.設置hibernate.cfg.xml
  1. <hibernate-configuration>
  2.     <session-factory>
  3.         <!-- 顯示實際操作資料庫時的SQL -->
  4.         <property name="show_sql">true</property>
  5.         <!-- 將顯示的SQL排版,方便觀看 -->
  6.         <property name="format_sql">true</property>
  7.         <!-- SQL方言,這邊設定的是SQLServer -->
  8.         <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  9.         <!-- JDBC驅動程式 -->
  10.         <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  11.         <!-- JDBC URL -->
  12.         <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb</property>
  13.         <!-- 資料庫使用者 -->
  14.         <property name="connection.username">sa</property>
  15.         <!-- 資料庫密碼 -->
  16.         <property name="connection.password">578989</property>
  17.         <!-- 物件與資料庫表格映射文件 -->
  18.         <mapping resource="User.hbm.xml">
  19.     </mapping></session-factory>
  20. </hibernate-configuration>



Step3.設置User.hbm.xml
-重點注意事項
1.資料庫每個欄位都要記得設定到<property>
2.其中id是個特殊的屬性,Hibernate會使用它來作為主鍵識別,您可以定義主鍵產生的方式,這是在XML映射文件中完成
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping
  3.         PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5.  
  6. <hibernate-mapping>
  7.     <!--<class>標籤的name屬性為所映射的物件,而table為所映射的表格;-->
  8.     <class name="Hibernate.UserInformationEntity" table="user_information">
  9.         <id name="id" column="id"> <!--<id>中 column屬性指定了表格欄位,而 type屬性指定了User實例的中的id之型態。-->
  10.             <generator class="native"/> <!--<id>中主鍵的產生方式在這邊設定為"native",表示主鍵的生成方式由Hibernate根據資料庫Dialect 的定義來決定-->
  11.             <!--<generator class="identity"/>-->
  12.             <!--identity-->
  13.         </id>
  14.         <!--<property>標籤中的column與type都各自指明了表格中欄位與物件中屬性的對應。-->
  15.         <property name="account" column="account"/>
  16.         <property name="name" column="name"/>
  17.         <property name="age" column="age"/>
  18.         <property name="create_time" column="create_time"/>
  19.     </class>
  20. </hibernate-mapping>


Step4.建立實體,與表格對應
-重點注意事項
1.資料庫欄位『型態是甚麼』,實體就要設一樣,否則會出錯
ex.資料庫:age int not null  實體:int age;
  1. public class UserInformationEntity {
  2.     int id =0;
  3.     String account;
  4.     String name;
  5.     int age = 0;
  6.     Date create_time;
  7.  
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public void setId(int id) {
  12.         this.id = id;
  13.     }
  14.     public String getAccount() {
  15.         return account;
  16.     }
  17.     public void setAccount(String account) {
  18.         this.account = account;
  19.     }
  20.     public String getName() {
  21.         return name;
  22.     }
  23.     public void setName(String name) {
  24.         this.name = name;
  25.     }
  26.     public int getAge() {
  27.         return age;
  28.     }
  29.     public void setAge(int age) {
  30.         this.age = age;
  31.     }
  32.     public Date getCreate_time() {
  33.         return create_time;
  34.     }
  35.     public void setCreate_time(Date create_time) {
  36.         this.create_time = create_time;
  37.     }
  38. }
Sept5.設定SessionFactory實例,連線用 
-重點注意事項  
1.也可直接寫在main class之中,一個應用程式當中通常只需要一個SessionFactory實例, 為了方便整個應用取得同一個SessionFactory實例, 可以撰寫一個HibernateUtil類別 
2.要如何寫在main class可參考: http://blog.csdn.net/zs234/article/details/9108533
  1. package Hibernate;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;
  4. /**
  5.  * Created by ytchen on 2016/7/22.
  6.  */
  7. public class HibernateUtil {
  8.     private static SessionFactory sessionFactory;
  9.     static {
  10.         try {
  11.             sessionFactory = new Configuration().configure()
  12.                     .buildSessionFactory();
  13.         } catch (Throwable ex) {
  14.             throw new ExceptionInInitializerError(ex);
  15.         }
  16.     }
  17.     public static SessionFactory getSessionFactory() {
  18.         return sessionFactory;
  19.     }
  20.     public static void shutdown() {
  21.         getSessionFactory().close();
  22.     }
Sept6.馬上來試用Hibernate吧 
  1. package Hibernate;
  2.  
  3. import org.hibernate.Transaction;
  4.  
  5. import org.hibernate.Session;
  6.  
  7.  
  8.  
  9. import java.util.Date;
  10.  
  11.  
  12.  
  13. /**
  14.  
  15.  * Created by ytchen on 2016/7/22.
  16.  
  17.  */
  18.  
  19. public class HibernateDemo {
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         UserInformationEntity user = new UserInformationEntity();
  24.  
  25.         user.setAccount("Ian");
  26.  
  27.         user.setName("Ian");
  28.  
  29.         user.setAge(20);
  30.  
  31.         user.setCreate_time(new Date());
  32.  
  33.  
  34.  
  35.         // 開啟Session,相當於開啟JDBC的Connection
  36.  
  37.         Session session = HibernateUtil.getSessionFactory().openSession();
  38.  
  39.         // Transaction表示一組會話操作
  40.  
  41.         Transaction tx= session.beginTransaction();
  42.  
  43.         // 將物件映射至資料庫表格中儲存
  44.  
  45.         session.save(user);
  46.  
  47.         tx.commit();
  48.  
  49.         session.close();
  50.  
  51.  
  52.  
  53.         System.out.println("新增資料OK!請用Sqlserver觀看結果!");
  54.  
  55.  
  56.  
  57.         HibernateUtil.shutdown();
  58.  
  59.     }
  60.  
  61. }
  62.  

沒有留言:

張貼留言