hibernate 類生成表 的實現源碼

      使用hibernate的schemaExport 工具類實現將實體類轉換成數據庫中的表。在工程設計中,應該先設計表結構而不是先生成實體類,但是這只是個方法,雖然不推薦使用,但是還是需要記下來。

第一步:
在test包中創建一個生成表的java類:

 

  1. package com.test;   
  2.   
  3. import org.hibernate.cfg.Configuration;   
  4. import org.hibernate.tool.hbm2ddl.SchemaExport;   
  5.   
  6. public class 生成表 {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.         Configuration cfg = new Configuration().configure();   
  13.         SchemaExport ex   = new SchemaExport(cfg);   
  14.         ex.create(truetrue);   
  15.            
  16.     }   
  17.   
  18. }  

 

第二步:
寫一個創建session的類:

  1. package com.test;   
  2.   
  3. import org.hibernate.Session;   
  4. import org.hibernate.SessionFactory;   
  5. import org.hibernate.cfg.Configuration;   
  6.   
  7. public class HibernateSessionFactory {   
  8.     private static Configuration cfg = new Configuration().configure();   
  9.     private static SessionFactory factory = cfg.buildSessionFactory();   
  10.     private static ThreadLocal<Session> local  = new ThreadLocal<Session>();   
  11.        
  12.     public static Session getSession(){   
  13.         Session session = local.get(); //取   
  14.         if (session==null || session.isOpen()==false){   
  15.             session = factory.openSession();   
  16.             local.set(session); //存   
  17.         }   
  18.         return session;   
  19.     }   
  20.        
  21. }  

第三步:
修改hibernate的配置文件相關屬性、驅動。

  1. <?xml version='1.0' encoding='UTF-8'?>   
  2. <!DOCTYPE hibernate-configuration PUBLIC   
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->   
  7. <hibernate-configuration>   
  8.   
  9.     <session-factory>   
  10.         <property name="myeclipse.connection.profile">【這裏是什麼數據庫就寫什麼】mysql</property>   
  11.         <property name="connection.url">   
  12.             【數據庫連接是什麼數據庫寫什麼數據庫test   
  13. 】jdbc:mysql://localhost:3306/test   
  14.         </property>   
  15.         <property name="dialect">   
  16.             【是mysql就寫mysql orc就orc】org.hibernate.dialect.MySQLDialect   
  17.         </property>   
  18.         <property name="connection.username">root</property>   
  19.     <property name="connection.password">admin</property>   
  20.         <property name="connection.driver_class">   
  21.             【驅動要改 】com.mysql.jdbc.Driver   
  22.         </property>   
  23.         <property name="show_sql">true</property>   
  24.         <property name="format_sql">true</property>   
  25.         <mapping resource="com/pojos/TSaleformDetail3.hbm.xml" />   
  26.         <mapping resource="com/pojos/TSaleform3.hbm.xml" />   
  27.   
  28.     </session-factory>   
  29.   
  30. </hibernate-configuration>

第四步 改pojo映射文件:

  1. <hibernate-mapping>   
  2.     <class name="com.pojos.TSaleform3" table="T_SALEFORM3" schema="test">【這地方的schema=“數據庫名字”   
  3. 】  

本文章轉自網絡,僅供學習交流用

 

 

發佈了17 篇原創文章 · 獲贊 18 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章