1. 程式人生 > >【Hibernate】自動生成資料庫表

【Hibernate】自動生成資料庫表

  雖說整體上對SSH有一定的把控使用能力,但還是見微知著,點滴積累。Hibernate本意是冬眠,很好的封裝了JDBC和資料庫互動,實現了物件的持久化操作。所以也可以理解物件的持久化其實就是“冬眠”。那麼如何通過Hibernate實現自動生成資料庫表?不再依賴於db工具。

一、User類

<span style="font-size:18px;">package com.xx.hibernate;

import java.util.Date;

public class User {
	private String id;

	private String name;

	private String password;

	private Date createDate;

	private Date expireDate;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getExpireDate() {
		return expireDate;
	}

	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}

}
</span>

二、User.hbm.xml 對映檔案

<span style="font-size:18px;"><?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.xx.hibernate.User">
		<id name="id">
			<generator class="uuid"></generator>
		</id>
		<property name="name" />
		<property name="password" />
		<property name="createDate" />
		<property name="expireDate" />
	</class>
</hibernate-mapping></span>

三、hibernate.cfg.xml 配置檔案

<span style="font-size:18px;"><?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

		<mapping resource="com/xx/hibernate/User.hbm.xml" /> 
	</session-factory>
</hibernate-configuration></span>

四、ExportDB 工具類

<span style="font-size:18px;"><pre name="code" class="html">package com.xx.hibernate;

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

public class ExportDB {

	/**
	 * 將hbm轉成ddl
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 如果直接new Configuration 預設讀取的是hibernate.properties 檔案
		// 後邊再加.configure()讀取的才是hibernate.cfg.xml 檔案,
		Configuration cfg = new Configuration().configure();
		// 建立SchemaExport物件
		SchemaExport export = new SchemaExport(cfg);
		// 生成ddl檔案,並在控制檯輸出
		export.create(true, true);
	}

}
</span>


  執行工具類就可以生成表,這是一種實現方式;還有第二種方案,在hibernate.cfg.xml中新增: <property name="hibernate.hbm2ddl.auto" value="create" />;