目前很多人使用Hibernate作為持久層,如果我們已經寫了配置文件poweracl.hbm.XML,則不必再費勁寫SQL的DDL。除了利用工具SchemaExport之外,還可以編寫程序來自動初始化數據庫,並且生成SQL DDL。
(1)Hibernate配置文件hibernate.cfg.XML
<?XML version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.Net/hibernate-configuration-2.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/hibtest</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.passWord">123456</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="com/hibtest/poweracl.hbm.XML"/>
</session-factory>
</hibernate-configuration>
注意:(1)JDBC驅動為com.MySQL.jdbc.Driver,可以根據所使用的庫而更換。
(2)dialect為數據庫方言,根據所使用數據庫不同而不同。這裡是MySQL。
(3)jdbc.fetch_size和jdbc.batch_size過小會降低性能,這裡是建議設置。
(4)mapping文件根據文件所在路徑而不同。這裡是放在WEB-INF/classes/com/hibtest/目錄下。
(2)數據庫映射配置poweracl.hbm.XML
<?XML version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.Net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.hibtest.user" table="user" discriminator-value="W">
<id name="loginname" type="string" >
<column name="loginname" sql-type="char(16)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="passWord" type="string">
<column name="passWord" sql-type="varchar(20)" />
</property>
<property name="name" type="string">
<column name="name" sql-type="varchar(20)" />
</property>
<property name="email" type="string">
<column name="email" sql-type="varchar(60)" />
</property>
<property name="modifIEd" type="date">
<column name="modifIEr" />
</property>
<property name="creater" type="date">
<column name="creater" />
</property>
<property name="lastlogin" type="date">
<column name="lastlogin" />
</property>
</class>
</hibernate-mapping>
說明:具體的poweracl.hbm.XML要根據數據庫表而設置,這裡只是列舉一個user表。
(3) 初始化數據庫類
package com.hibtest;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import Java.io.File;
import Java.util.HashSet;
import Java.util.Set;
/**
* <p/> vedadou
* Date: 2004-02-25
* Time: 9:40:15
*/
public class InitDB {
static Session session;
public static void main(String[] args) {
Configuration config = null;
Transaction tx = null;
try {
config = new Configuration().configure(new File("hibernate.cfg.XML"));
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
注意:在初始化之前,應該先手工創建一個空數據庫,然後再執行InitDB程序