Hibernate 自動生產表

來源:互聯網
上載者:User

標籤:use   預設   知識   exp   封裝   otl   語句   layout   levels   

 hibernate是一個開放原始碼的對象關係映射架構,它對JDBC進行了非常輕量級的對象封裝,使得Java程式員可以隨心所欲的使用對象編程思維來操縱資料庫。

 

      今天就來示範一下Hibernate最初級的操作,使用SchemaExport建立資料表。

      

      1.首先建立POJO類

 

  1. package com.bjpowernode.hibernate;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  * 使用者 
  7.  * @author Longxuan 
  8.  * 
  9.  */  
  10. public class User {  
  11.       
  12.     private String  id;  
  13.       
  14.     private String name;  
  15.       
  16.     private String password;  
  17.       
  18.     private Date createTime;  
  19.       
  20.     private Date expireTime;  
  21.   
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(String id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.   
  38.     public String getPassword() {  
  39.         return password;  
  40.     }  
  41.   
  42.     public void setPassword(String password) {  
  43.         this.password = password;  
  44.     }  
  45.   
  46.     public Date getCreateTime() {  
  47.         return createTime;  
  48.     }  
  49.   
  50.     public void setCreateTime(Date createTime) {  
  51.         this.createTime = createTime;  
  52.     }  
  53.   
  54.     public Date getExpireTime() {  
  55.         return expireTime;  
  56.     }  
  57.   
  58.     public void setExpireTime(Date expireTime) {  
  59.         this.expireTime = expireTime;  
  60.     }  
  61.       
  62. }  


      2、根據POJO類裡面裡面相關的欄位,在包中建立User.hbm.xml對應檔

 

 

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping>  
  6.     <class name="com.bjpowernode.hibernate.User" >  
  7.         <!--hibernate為我們產生主鍵id-->  
  8.         <id name="id">  
  9.             <generator class="uuid" />  
  10.         </id>  
  11.           
  12.         <!--預設把類的變數映射為相同名字的表列,當然我們使用column屬性修改表欄位-->  
  13.         <property name="name" column="name"></property>  
  14.         <property name="password"></property>  
  15.         <property name="createTime"></property>  
  16.         <property name="expireTime"></property>  
  17.     </class>  
  18. </hibernate-mapping>  


      3、在src中建立hibernate.cfg.xml

 

 

  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4.   
  5. <hibernate-configuration>  
  6.     <session-factory name="foo">  
  7.         <!-- 資料庫的串連也可以直接使用hibernate.properties檔案 -->  
  8.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>  
  10.         <property name="hibernate.connection.username">root</property>  
  11.         <property name="hibernate.connection.password">root</property>  
  12.           
  13.         <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 -->  
  14.         <property name="hibernate.show_sql">true</property><!-- 設定是否顯示產生sql語句 -->  
  15.         <property name="hibernate.format_sql">true</property><!-- 設定是否格式化sql語句-->  
  16.           
  17.         <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"  />  
  18.     </session-factory>  
  19. </hibernate-configuration>  


      4、建立ExportDB類

 

 

  1. package com.bjpowernode.hibernate;  
  2.   
  3. import org.hibernate.cfg.Configuration;  
  4. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  5.   
  6.   
  7.   
  8. /** 
  9.  * 將hbm產生ddl 
  10.  * @author Longxuan 
  11.  * 
  12.  */  
  13. public class ExportDB {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.           
  20.         // 預設讀取hibernate.cfg.xml檔案  
  21.         Configuration cfg = new Configuration().configure();  
  22.           
  23.         // 產生並輸出sql到檔案(目前的目錄)和資料庫  
  24.         SchemaExport export = new SchemaExport(cfg);  
  25.           
  26.         // true 在控制台列印sql語句,true 匯入sql語句到資料庫,即可執行  
  27.         export.create(true, true);  
  28.     }  
  29. }  


      5、建立log4j.properties記錄檔

 

 

  1. ### direct log messages to stdout ###  
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  3. log4j.appender.stdout.Target=System.out  
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  6.   
  7. ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###  
  8.   
  9. log4j.rootLogger=warn, stdout  

 

      現在可以測試了。首先在MySQL中建立hibernate_test資料庫:

      運行ExportDB的main方法,結果

Hibernate 自動生產表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.