您的位置:首页 > 数据库

利用Hibernate配置文件反向生成数据库

2014-04-28 16:13 423 查看

1.Hibernate配置
文件hibernate.cfg.xml

<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">test</property>
    
   <mapping resource="com/test/fileInfo.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/test/目录下。 

2.数据库映射配置FileInfo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test">
<class name="FileInfo" table="t_fileInfo">
<id name="fileId" type="java.lang.Integer">
<column name="fileId" precision="6" scale="0" />
<generator class="native">
</generator>
</id>
<property name="fileName" column="fileName" />
<property name="fileTitle" column="fileTitle" />
<property name="fileAuthor" column="fileAuthor" />
<property name="fileStatus" column="fileStatus" />
<property name="fileIndexFlag" column="fileIndexFlag" />
<property name="fileKeyWord" column="fileKeyWord" />
<property name="directoryId" column="directoryId" />
<property name="fileFormat" column="fileFormat" />
<property name="fileSize" column="fileSize" />
<property name="filePath" column="filePath" />
<property name="creatDate" column="creatDate" />
<property name="createAccountsId" column="createAccountsId" />
<property name="createAccountsName" column="createAccountsName" />
<property name="verifyAccountsId" column="verifyAccountsId" />
<property name="templateId" column="templateId" />
<property name="remark" column="remark" />
<property name="content" column="content" />
</class>
</hibernate-mapping>

3.初始化数据库类

package com.test;

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

public class ExportDB {

public static void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: