经过前面的两节分析:1、Hibernate之生成SessionFactory源码追踪 和 2、Spring的LocalSessionFactoryBean创建过程源码分析 。我们可以得到这样一个结论,spring的LocalSessionFactoryBean具体是调用Hibernate的Configuration中configure(...)方法来读取并解析xxx.cfg.xml文件的,同样也会得到一个原生态的org.hibernate.cfg.Configuration 和 org.hibernate.SessionFactory属性。

所以,我们可以看见LocalSessionFactoryBean中保存有hibernate的configuration、sessionFactory、properties 以及 metadataSourceQueue。

从前面两节分析中我们知道,LocalSessionFactoryBean会读取并解析xxx.cfg.xml和xxx.hbm.xml文件,所以在配置LocalSessionFactoryBean的时候需要明确指定这两个文件的位置。当然,连接数据库的dataSource也要配置在spring的bean中:

下面是一个spring整合hibernate的实例:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 配置自动扫描的包 -->
<!-- 实体和dao -->
<context:component-scan base-package="comentity,com.dao.impl"></context:component-scan>
<!-- service -->
<context:component-scan base-package="com.service.impl"></context:component-scan> <!-- 导入数据库资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
</bean> <!-- 配置sessionFacory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 如果有多个配置文件,可以使用属性configLocations来配置,多个配置文件之间用逗号“,”来分割,
如:classpath:hibernate.cfg.xml,classpath:extension.cfg.xml -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- ORM映射关系配置文件 -->
<property name="mappingLocations" value="classpath:com/gzpp123/web/entity/*.hbm.xml"></property>
</bean> <!-- 配置spring的声明式事物 -->
<!-- 1、配置hibernate的事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 2、配置事物属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!-- 3、配置事物切入点,再把事物属性和事务切入点关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> <aop:config>
<aop:pointcut expression="execution (* com.service.CoreService.*(..))" id="txPointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut1"/>
</aop:config> </beans>

再看看类路径下的db.properties文件:

jdbc.user=root
jdbc.password=tiger123
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/gzpp jdbc.initialPoolSize=2
jdbc.maxPoolSize=10
jdbc.minPoolSize=1
jdbc.acquireIncrement=5
#...
#appSecret: 62e5c0141c2fc9a3bc9d2ae73fb7cd12
#appid: wx15fc2152e1406d02

由于mapping以及dataSource都在spring中配置完成了,所以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">
<hibernate-configuration>
<session-factory> <!-- 配置 hibernate 的基本属性 --> <!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 是否显示及格式化 SQL -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property> <!-- 生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 二级缓存相关 --> </session-factory>
</hibernate-configuration>

3、Spring整合Hibernate的更多相关文章

  1. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  2. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  3. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  4. spring 整合hibernate

    1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory2). 让 Hibernate 使用上 Spring 的 ...

  5. Spring 整合 Hibernate

    Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...

  6. 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能

    1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...

  7. Spring整合Hibernate详细步骤

    阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...

  8. SSH整合之spring整合hibernate

    SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...

  9. 【Spring】Spring系列6之Spring整合Hibernate

    6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...

随机推荐

  1. Notes of the scrum meeting(11/3)

    meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...

  2. FushionCharts Free 的运用[2D/3D图表处理]

    由于先前在一些论坛中谈论到这个插件的运用,留了一些QQ联系方式,最近老是被一些程序员“骚扰”,说是请教一些关于FushionChart Free图表的处理技术,先前还是比较乐意接受的,但发现后来一些完 ...

  3. java中的IO流之文件复制

    O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...

  4. 前端之JavaScript第二天学习(4)-JavaScript-注释

    JavaScript 注释可用于提高代码的可读性. JavaScript 注释 JavaScript 不会执行注释. 我们可以添加注释来对 JavaScript 进行解释,或者提高代码的可读性. 单行 ...

  5. 【CentOS】搭建Web服务器

    参考资料:     http://www.paipat.com/?post=24     http://www.cnblogs.com/xiaoluo501395377/archive/2013/04 ...

  6. 【BZOJ】【2809】【APIO2012】派遣dispatching

    贪心/可并堆 跪了……我这么弱果然还是应该回家种红薯去…… 考虑选人的时候,每个人对答案的贡献其实是一样的,都是1,那么我们就贪心地去选花钱少的就好啦~ 具体的做法:倒着枚举(因为有b[i]<i ...

  7. dblink应用

    当我们要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中就必须要创建远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据. 用法例子: (1) ...

  8. java中byte和blob互转

    1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...

  9. Hibernate SQL方言 (hibernate.dialect)

    数据库 hibernate方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect ...

  10. 【转载】MySQL索引原理及慢查询优化

    原文链接:美团点评技术团队:http://tech.meituan.com/mysql-index.html MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型 ...