3、Spring整合Hibernate
经过前面的两节分析: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的更多相关文章
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- spring 整合hibernate
1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory2). 让 Hibernate 使用上 Spring 的 ...
- Spring 整合 Hibernate
Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...
- Spring整合Hibernate详细步骤
阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
- 【Spring】Spring系列6之Spring整合Hibernate
6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...
随机推荐
- 卷积神经网络CNN介绍:结构框架,源码理解【转】
1. 卷积神经网络结构 卷积神经网络是一个多层的神经网络,每层都是一个变换(映射),常用卷积convention变换和pooling池化变换,每种变换都是对输入数据的一种处理,是输入特征的另一种特征表 ...
- Jquery $.getJSON()设置同步
如下: $.ajaxSettings.async = false; $.getJSON('/AjaxSwitchDynamicInfo/GetPortUsedCount.cspx', { switch ...
- 修改Input中Placeholder默认提示颜色(兼容)
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #f00; } input:-moz-pl ...
- JQuery常用实用的事件[较容易忽略的方法]
JQuery常用实用的事件 注:由于JQuery片段较多就没有用插入代码文本插入,请见谅!JQuery 事件处理ready(fn)代码: $(document).ready(function(){ ...
- android中的“visible ”、“invisible”、“gone”的区别(转载)
在Android开 发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”.“invisible”.“gone”.主要用来设置控制控件的显示和隐藏.有些人可能会疑惑I ...
- 关于django批量上传图片
本来想一张一张上传的,但是明显会对客户造成不必要的麻烦,所以如果前台一次性上传五张十张的话,那就简单的多. 但是后台我数据库对于图片存储的字段只有一个,不可能有多少张照片就要多少个字段来存储.也就是说 ...
- block extends include三者的差别跟用法
block extends include三者的差别跟用法 一.定义基础模板,在html内容中定义多个block块,block由子模板引用同名block块,来决定是否替换这些部分{% block ti ...
- Spring MVC 入门教程示例 (一)
今天和大家分享下 Spring MVC 入门教程 首先还是从 HelloWorld web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...
- hdu 3046 Pleasant sheep and big big wolf 最小割
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...