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);
}
}

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

被折叠的 条评论
为什么被折叠?



