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

本文介绍了Hibernate框架的基本配置方法及数据库映射配置细节。包括hibernate.cfg.xml配置文件的详细参数设置,如数据库连接信息、方言选择等;同时展示了FileInfo实体类的数据库映射文件FileInfo.hbm.xml,用于实现Java对象与数据库表之间的映射。

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>

注意:

1JDBC驱动为com.mysql.jdbc.Driver,可以根据所使用的库而更换。
2dialect 为数据库方言,根据所使用数据库不同而不同。这里是Mysql

3jdbc.fetch_size jdbc.batch_size过小会降低性能,这里是建议设置。

4mapping文件根据文件所在路径而不同。这里是 放在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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

macrobn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值