SSH Hibernate之SchemaExport+配置文件生成表結(jié)構(gòu)

2018-09-28 18:58 更新

Hibernate之SchemaExport+配置文件生成表結(jié)構(gòu)

今天說(shuō)點(diǎn)基礎(chǔ)的東西,說(shuō)說(shuō)如何通過(guò)SchemaExport跟Hibernate的配置文件生成表結(jié)構(gòu)。其實(shí)方法非常簡(jiǎn)單,只需要兩個(gè)配置文件,兩個(gè)Java類(lèi)就可以完成。

首先要生成表,得先有實(shí)體類(lèi),以Person.java為例:

/** 
 *  
 * @author Administrator 
 * @hibernate.class table="T_Person" 
 */  
public class Person {  

    /** 
     * @hibernate.id 
     * generator-class="native" 
     */  
    private int id;  

    /** 
     * @hibernate.property 
     */  
    private String name;  

    /** 
     * @hibernate.property 
     */  
    private String sex;  

    /** 
     * @hibernate.property 
     */  
    private String address;  

    /** 
     * @hibernate.property 
     */  
    private String duty;  

    /** 
     * @hibernate.property 
     */  
    private String phone;  

    /** 
     * @hibernate.property 
     */  
    private String description;  

    /** 
     * @hibernate.many-to-one 
     */  
    private Orgnization org;  

    public String getAddress() {  
        return address;  
    }  
    public void setAddress(String address) {  
        this.address = address;  
    }  
    public String getDescription() {  
        return description;  
    }  
    public void setDescription(String description) {  
        this.description = description;  
    }  
    public String getDuty() {  
        return duty;  
    }  
    public void setDuty(String duty) {  
        this.duty = duty;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getPhone() {  
        return phone;  
    }  
    public void setPhone(String phone) {  
        this.phone = phone;  
    }  
    public String getSex() {  
        return sex;  
    }  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
    public Orgnization getOrg() {  
        return org;  
    }  
    public void setOrg(Orgnization org) {  
        this.org = org;  
    }  
}  

接下來(lái)就是Person類(lèi)對(duì)應(yīng)的配置文件Person.hbm.xml,配置如下:

<hibernate-mapping>  
  <class table="T_Person" name="com.tgb.model.Person">  
    <id name="id">  
      <generator class="native"/>  
    </id>  
    <property name="name"/>  
    <property name="sex"/>  
    <property name="address"/>  
    <property name="duty"/>  
    <property name="phone"/>  
    <property name="description"/>  
    <many-to-one name="org"></many-to-one>  
  </class>  
</hibernate-mapping>  

還有包含Person.hbm.xml相關(guān)信息的Hibernate默認(rèn)配置文件,hibernate.cfg.xml:

<hibernate-configuration>  
  <session-factory>  
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>  
    <property name="hibernate.connection.username">root</property>  
    <property name="hibernate.connection.password">123456</property>  
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
    <property name="hibernate.show_sql">true</property>  
    <property name="hibernate.hbm2ddl.auto">update</property>  
    <property name="hibernate.current_session_context_class">thread</property>  
    <mapping resource="com/tgb/model/Person.hbm.xml"/>  
  </session-factory>  
</hibernate-configuration>  

萬(wàn)事俱備只欠東風(fēng),最后我們還需要一個(gè)根據(jù)上述內(nèi)容生成數(shù)據(jù)表的小工具,即ExportDB.Java:

import org.hibernate.cfg.Configuration;  
import org.hibernate.tool.hbm2ddl.SchemaExport;  

public class ExportDB {  

    /**  
     * @param args  
     */    
    public static void main(String[] args) {    

        // 默認(rèn)讀取hibernate.cfg.xml文件    
        Configuration cfg = new Configuration().configure();    

        // 生成并輸出sql到文件(當(dāng)前目錄)和數(shù)據(jù)庫(kù)    
        SchemaExport export = new SchemaExport(cfg);    

        // 創(chuàng)建表結(jié)構(gòu),第一個(gè)true 表示在控制臺(tái)打印sql語(yǔ)句,第二個(gè)true 表示導(dǎo)入sql語(yǔ)句到數(shù)據(jù)庫(kù)  
        export.create(true, true);    
    }    
}  

完成以上步驟以后,只需要執(zhí)行ExportDB類(lèi)即可,當(dāng)然前提是已經(jīng)在mysql中創(chuàng)建了對(duì)應(yīng)的數(shù)據(jù)庫(kù),我們這里創(chuàng)建了一個(gè)名為test的測(cè)試數(shù)據(jù)庫(kù)。執(zhí)行成功之后我們就可以看到數(shù)據(jù)庫(kù)里已經(jīng)有了我們的t_person表了,如下圖所示:


OK,你會(huì)了嗎,就是這么簡(jiǎn)單,如果之前沒(méi)弄過(guò),就來(lái)試試吧!

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)