美文网首页
Hibernate配置文件详解

Hibernate配置文件详解

作者: 暮秋moco | 来源:发表于2020-02-27 21:31 被阅读0次

Hibernate配置文件有两种形式:XML与properties
个人建议使用XML,因为properties中不能配置关联的映射文件,在后续的实现中会带来一些没必要的编码

1. XML(hibernate.cfg.xml)文件详解:

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
    <session-factory>
        <!-- 指定连接数据库所用的驱动 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 指定连接数据库的url,hibernate连接的数据库名 -->
        <property name="connection.url">jdbc:mysql://localhost/数据库名</property>
        <!-- 指定连接数据库的用户名 -->
        <property name="connection.username">root</property>
        <!-- 指定连接数据库的密码 -->
        <property name="connection.password">32147</property>
        <!-- 指定连接池里最大连接数 -->
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 指定连接池里最小连接数 -->
        <property name="hibernate.c3p0.min_size">1</property>
        <!-- 指定连接池里连接的超时时长 -->
        <property name="hibernate.c3p0.timeout">5000</property>
        <!-- 指定连接池里最大缓存多少个Statement对象 -->
        <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">true</property>
        <!-- 指定数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <!-- 根据需要自动创建数据表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 显示Hibernate持久化操作所生成的SQL -->
        <property name="show_sql">true</property>
        <!-- 将SQL脚本进行格式化后再输出 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 罗列所有的映射文件 -->
        <mapping resource="映射文件路径/News.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

2. properties(hibernate.properties)文件详解

## 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://127.0.0.1/datdabseName
#用户名
hibernate.connection.username root
#密码
hibernate.connection.password 12345
#是否在控制台输出sql语句
hibernate.show_sql true/false
#设置当创建sessionfactory时,是否根据映射文件自动建立数据库表。 create-drop:表示关闭sessionFactory时,将drop刚建的数据库表。该属性可以是update/create-drop/create
hibernate.hbm2ddl.auto update/create-drop/create

###########################
### C3P0 Connection Pool C3P0连接池###
###########################
#连接池最大链接数
hibernate.c3p0.max_size 2
#连接池最小连接数
hibernate.c3p0.min_size 2
#连接池连接的超时时长
hibernate.c3p0.timeout 5000
#缓存statements 的数量
hibernate.c3p0.max_statements 100
hibernate.c3p0.idle_test_period 3000
hibernate.c3p0.acquire_increment 2
hibernate.c3p0.validate true/false


############
### JNDI (java naming directory interface)Java命名目录接口###
###当无需hibernate自己管理数据源而是直接访问容器管理数据源 使用JNDI
############
#指定数据源JNDI名字
hibernate.connection.datasource dddd
#文件系统下
hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory
hibernate.jndi.url file:/

#网络
#指定JND InitialContextFactory 的实现类,该属性也是可选的。如果JNDI与Hibernate持久化访问的代码处于同一个应用,无需指定该属性
hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory
#指定JNDI提供者的URL,该属性可选 如果JNDI与Hibernate持久化访问的代码处于同一个应用,无需指定该属性
hibernate.jndi.url iiop://localhost:900/

#指定链接数据库用户名
hibernate.connection.username  root
#指定密码
hibernate.connection.password  1111
#指定方言
hibernate.dialect org.hibernate.dialect.MySQLDialect

#######################
### Transaction API   事务属性说明###
#######################

#指定是否在事务结束后自动关闭session 
hibernate.transaction.auto_close_session true/false
#指定session是否在事务完成后自动将数据刷新到底层数据库
hibernate.transaction.flush_before_completion true/false

## 指定hibernate所有的事务工厂的类型,该属性必须是TransactionFactory的直接或间接子类

hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.factory_class org.hibernate.transaction.JDBCTransactionFactory

## 该属性值是一个JNDI名,hibernate将使用JTATTransactionFactory从应用服务器中取出JTAYserTransaction

jta.UserTransaction jta/usertransaction
jta.UserTransaction javax.transaction.UserTransaction
jta.UserTransaction UserTransaction

## 该属性值为一个transactionManagerLookup类名,当使用JVM级别的缓存时,或在JTA环境中使用hilo生成器策略时,需要该类

hibernate.transaction.manager_lookup_class org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.transaction.manager_lookup_class org.hibernate.transaction.WeblogicTransactionManagerLookup
hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup
hibernate.transaction.manager_lookup_class org.hibernate.transaction.OrionTransactionManagerLookup
hibernate.transaction.manager_lookup_class org.hibernate.transaction.ResinTransactionManagerLookup

附另

Hibernate的核心接口一共有5个,分别为:Session、SessionFactory、Transaction、Query和 Configuration。这5个核心接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务控制。

Session接口:
Session接口负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的 SQL语句)。但需要注意的是Session对象是非线程安全的。同时,Hibernate的session不同于JSP应用中的 HttpSession。这里当使用session这个术语时,其实指的是Hibernate中的session,而以后会将HttpSesion对象称 为用户session。

SessionFactory接口:
SessionFactroy接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建 Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个 SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。

Configuration接口:
Configuration接口负责配置并启动Hibernate,创建SessionFactory对 象。在Hibernate的启动的过程中,Configuration类的实例首先定位映射文档位置、读取配置,然后创建SessionFactory对 象。

Transaction接口:
Transaction接口负责事务相关的操作。它是可选的,开发人员也可以设计编写自己的底层事务处理代码。

Query和Criteria接口:
Query和Criteria接口负责执行各种数据库查询。它可以使用HQL语言或SQL语句两种表达方式。

参考

https://www.cnblogs.com/lcycn/p/8126783.html
https://blog.csdn.net/usench_10000/article/details/84703179?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

相关文章

网友评论

      本文标题:Hibernate配置文件详解

      本文链接:https://www.haomeiwen.com/subject/agdfhhtx.html