Hibernate:利用設定檔產生資料庫

來源:互聯網
上載者:User

目前很多人使用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程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.