今天說點基礎的東西,說說如何通過SchemaExport跟Hibernate的配置檔案生成表結構。其實方法非常簡單,只需要兩個配置檔案,兩個Java類就可以完成。
首先要生成表,得先有實體類,以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; } }
接下來就是Person類對應的配置檔案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相關資訊的Hibernate預設配置檔案,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>
萬事俱備只欠東風,最後我們還需要一個根據上述內容生成資料表的小工具,即ExportDB.Java:
import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { /** * @param args */ public static void main(String[] args) { // 預設讀取hibernate.cfg.xml檔案 Configuration cfg = new Configuration().configure(); // 生成並輸出sql到檔案(當前目錄)和資料庫 SchemaExport export = new SchemaExport(cfg); // 建立表結構,第一個true 表示在控制檯列印sql語句,第二個true 表示匯入sql語句到資料庫 export.create(true, true); } }
完成以上步驟以後,只需要執行ExportDB類即可,當然前提是已經在mysql中建立了對應的資料庫,我們這裡建立了一個名為test的測試資料庫。執行成功之後我們就可以看到資料庫裡已經有了我們的t_person表了,如下圖所示:
OK,你會了嗎,就是這麼簡單,如果之前沒弄過,就來試試吧!