一、hibernate.cfg.xml 主配置文件模板
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">false</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
二、hibernate.cfg.xml 主配置文件详解
2.1 src/hibernate.cfg.xml 注意事项
1. 该主配置文件中主要用户配置:
数据库连接信息、其他参数、映射信息!
2. 该配置文件必须放在src根目录下。
因为Configuration会默认加载该路径下的配置。
3. 配置hibernate.cfg.xml可以借助hibernate.properties配置文件。
hibernate.properties:
该配置文件存储着许多key-value值。
主要是hibernate.cfg.xml的常用配置。
4. hibernate.cfg.xml的key可以省略hibernate前缀。
比如:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
可以简写为:
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hbm2ddl.auto">update</property>
2.2 数据库连接配置
+++ MySql 数据库配置
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
+++ Oracle 数据库配置
#hibernate.dialect org.hibernate.dialect.Oracle8iDialect
#hibernate.dialect org.hibernate.dialect.Oracle9iDialect
#hibernate.dialect org.hibernate.dialect.Oracle10gDialect
#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
#hibernate.connection.username ora
#hibernate.connection.password ora
#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl
#hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE
2.2.1 案例:连接mysql
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">false</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.2.2 案例:连接oracle
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522:XE</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">false</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.2.3 数据库方言的选择
2.3 连接池配置
hibernate对连接池C3P0的配置可以参考配置文档。
下面是配置文档的部分代码
###########################
### C3P0 Connection Pool###
###########################
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
2.3.1 hibernate对连接池的支持
DBCP的bug非常多,因此Hibernate3.0已经不再支持DBCP连接池,
而推荐使用C3PO。
2.3.2 案例:配置c3p0连接池
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory >
<!-- **************************** 1. 数据库连接配置【必须】 **************************** -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://106.13.58.30:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方法配置.hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- **************************** 2. 其他相关配置【必须】 **************************** -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">false</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 3. 加载所有映射
<mapping resource="org/jsoft/demo/EmpEntity.hbm.xml"/>
-->
<!-- **************************** 3. 连接池配置【可选】 **************************** -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">false</property>
</session-factory>
</hibernate-configuration>
2.3 其他配置
+++ 显示生成的sql
#hibernate.show_sql true
+++ 格式化sql
#hibernate.format_sql true
+++ 根据映射文件,生成表
#hibernate.hbm2ddl.auto
create-drop 每次在创建sessionFactory时候执行创建表;
当调用sesisonFactory的close方法的时候,删除表!
create 每次都重新建表; 如果表已经存在就先删除再创建
update 如果表不存在就创建; 表存在就不创建;
validate (生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!
这三个是常用的其他配置,还有其他的配置。
【如果需要其他的配置在源码中查找】
2.3.1 显示sql(不格式化)
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">false</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.3.2 显示sql(格式化)
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.3.3 自动建表
+++ 自动创建表有两种方式:
1.通过主配置文件配置
2.通过硬编码。
推荐使用硬编码方式。因为自动建表,不会生成任何脚本文件。
对数据库的控制是极不安全的。
所以如果有自动建表的需求,手动编码就好。
+++ 自动建表的原理:
我们有完整的实体与表的映射关系。
所以我们可以通过这些映射关系,来实现表的创建。
====》配置建表
<property name="hibernate.hbm2ddl.auto">create-drop</property>
每次创建sessionfactory时创建表,关闭sessionfactory时,删除表
<property name="hibernate.hbm2ddl.auto">create</property>
每次都重新建表。如果表已经存在,就会先删除表,在新建。
<property name="hibernate.hbm2ddl.auto">update</property>
如果表不存在,就会新建。如果表存在,就不创建。
<property name="hibernate.hbm2ddl.auto">validate</property>
执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory name="21">
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2.3 -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 3. 加载所有映射 -->
<mapping resource="UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
====》硬编码建表
public class App_ddl {
// 自动建表
@Test
public void testCreate() throws Exception {
// 创建配置管理类对象
Configuration config = new Configuration();
// 加载主配置文件
config.configure();
// 创建工具类对象
SchemaExport export = new SchemaExport(config);
// 建表
// 第一个参数: 是否在控制台打印建表语句
// 第二个参数: 是否执行脚本
export.create(true, true);
}
}
2.3.4 在 Hibernate 中设置隔离级别
JDBC 数据库连接使用数据库系统默认的隔离级别。在 Hibernate 的配置文件中可以显式的设置隔离级别. 每一个隔离级别都对应一个整数:
1. READ UNCOMMITED
2. READ COMMITED
4. REPEATABLE READ
8. SERIALIZEABLE
Hibernate 通过为 Hibernate 映射文件指定 hibernate.connection.isolation 属性来设置事务的隔离级别。
<!-- 设置 Hibernate 的事务隔离级别 2表示读已提交 -->
<property name="connection.isolation">2</property>
2.4 映射配置
<mapping resource="org/jsoft/demo/UserEntity.hbm.xml"/>
加载映射文件。该映射配置必须在property标签之下。
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory name="21">
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 3. 加载所有映射 -->
<mapping resource="org/jsoft/demo/UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、加载映射文件
1.1.1 硬编码方式加载
在hibernate.cfg.xml中可以不用指定映射文件,
可以通过硬编码的方式加入映射文件。
这种方式,一般是在测试时使用。
new Configuration()
.configure()
.addClass(UserEntity.class)
.buildSessionFactory();
它会加载当前包下与类型一样的映射文件, 文件名:类名.hbm.xml。
即UserEntity.hbm.xml
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory name="21">
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Demo.java
public class Demo {
public static SessionFactory sf;
static {
sf=new Configuration()
.configure()
//测试中使用。在hibernate.cf.xml不用配置映射文件。
//它会自动加载同包下的UserEntity.hbm.xml文件
.addClass(UserEntity.class)
.buildSessionFactory();
}
public static void main(String[] args) {
}
}
1.1.2 配置方式加载映射文件
映射文件的名称可以随意指定。只需要在hibernate.cfg.xml中引入即可。
org/jsoft/demo/xxx.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.jsoft.demo">
<class name="UserEntity" table="USER">
<!-- 主键 ,映射 -->
<id name="id" column="u_id" >
</id>
<!-- 非主键,映射 -->
<property name="name" column="u_name"></property>
<property name="birth" column="u_birth"></property>
</class>
</hibernate-mapping>
src/hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory name="21">
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 3. 加载所有映射 -->
<mapping resource="org/jsoft/demo/xxx.hbm.xml"/>
</session-factory>
</hibernate-configuration>