Spring3 集成 Hibernate4事务
注意事项
<!-- 5. 配置事务管理器 注意是,hibernate4,配置成hibernate3报错 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
配置文件:
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" > <!-- 1. 配置datasource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定连接数据库的驱动 --> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- 指定连接数据库的URL --> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hibernate?useunicode=true&characterEncoding=utf8"/> <!-- 指定连接数据库的用户名 --> <property name="user" value="root"/> <!-- 指定连接数据库的密码 --> <property name="password" value="root"/> <!-- 指定连接数据库连接池的最大连接数 --> <property name="maxPoolSize" value="40"/> <!-- 指定连接数据库连接池的最小连接数 --> <property name="minPoolSize" value="1"/> <!-- 指定连接数据库连接池的初始化连接数 --> <property name="initialPoolSize" value="1"/> <!-- 指定连接数据库连接池的连接的最大空闲时间 --> <property name="maxIdleTime" value="20"/> </bean> <!-- 2. 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!--dataSource属性指定要用到的连接池--> <property name="dataSource" ref="dataSource"/> <!--指定要用到的实体映射文件--> <property name="mappingResources"> <list> <value>com/test/hibernate/mapping/News.hbm.xml</value> </list> </property> <!--配置Hibernate的属性--> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true hibernate.temp.use_jdbc_metadata_defaults=false </value> </property> </bean> <!-- 配置HibernateTemplate, hibernate4里已经舍弃了HibernateTemplate,这个配置不需要了, 直接注入 sessionFactory到Dao组件 在hibernate4里已经舍弃了HibernateTemplate,因为在hibernate4里的事务已经完全可以实现事务了 由于hibernate4已经完全可以实现事务了 与spring3.1中的hibernatedao,hibernateTemplete等有冲突, 所以spring3.1里已经不提供hibernatedaosupport,hibernateTemplete了, 只能用hibernate原始的方式用session <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> --> <!-- 3. 配置Dao层 Bean,该Dao继承自HibernateDaoSupport,需要注入HibernateTemplate --> <bean id="newsDao" class="test.hibernate.pojo.dao.NewsDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 4. 配置business业务逻辑层Bean,需要注入Dao层Bean --> <bean id="newsBusiness" class="test.hibernate.business.NewsBusiness"> <property name="newsDao" ref="newsDao"></property> </bean> <!-- 5. 配置事务管理器 注意是,hibernate4,配置成hibernate3报错 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--6. 事务配置,主要是使用的transaction-manager不一样 配置增强处理的bean(相当于切面类),也是Spring自动生成普通 业务逻辑bean(targetBean)的代理Bean.里面的tx:method配置每个方法的事务属性, name配置方法名,可使用通配符. --> <tx:advice id="txJpaAdvice" transaction-manager="txManager"> <!-- 配置详细的事务语义 --> <tx:attributes> <!-- 表示get开头的方法是只读的 --> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <!-- 其他方法使用默认的事务设置 --> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 全局事务切面配置,AOP元素配置 --> <aop:config> <!-- 配置一个切入点 test.hibernate.business包下面所有类的所有方法,都会被加入事务 --> <aop:pointcut id="myJpaPoint" expression="execution(* test.hibernate.business.*.*(..))" /> <!-- 配置 (事务代理)切入点(aop:pointcut) 和 切面类(tx:advice),将二者关联起来 --> <aop:advisor advice-ref="txJpaAdvice" pointcut-ref="myJpaPoint" /> </aop:config> </beans>
相关推荐
Spring与Hibernate集成声明式事务.ppt
声明式事务控制,spring2.5+hibernate3集成源码,附带有数据库脚本,可以直接导入到项目的工作空间。欢迎下载
Spring2.5和Hibernate3集成 采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承...
NULL 博文链接:https://quicker.iteye.com/blog/674029
1、 同时使用了Struts2、Spring4、Hibernate4、Quartz、JUnit4、Log4j、SLF4J、Ehcache、Gson等库或框架,搭建一个最基本的项目原型。 2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1...
NULL 博文链接:https://chenming47.iteye.com/blog/437199
本文试图解释如何使用Spring来集成组件(包括组件的事务关系)。在J2EE应用程序中,连接到单个存储数据没有什么困难。但是一旦要求集成企业级组件的时候,情况就复杂了。一个组件一般会受到一个或多个存储数据的支持...
本资源主要是结合博文 Spring+Hibernate: 声明式事务 http://blog.csdn.net/ManagementAndJava/article/details/78050519 的源代码,主要是讲解了spring4.3和hibernate5.2.11集成实现原理;
Struts1.3+Spring3.0+Hibernate3.3+DWR2.X框架集成文档v1.4.pdf 包括事务处理配置,个人整理,仅供学习,请务拍砖。呵呵~~
atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA) 结合spring 和durid进行配置,
spring3.1+hibernate4+jpa框架集成Dao部分,并且已经写好测试类,dao已经封装完毕,已经配置好事务。dao采用泛型。
springboot(4) 整合mybatis和hibernate 请删除项目中本地仓库的配置,或者改成国内的公共仓库,否则将无法启动
本例写于2013年6月5日,全部采用最新的jar包,利用maven构建工具集成struts2 + spring3 + hibernate4 写成的web示例程序。该示例严格采用三层架构,提供事务处理机制。可直接基于该示例进行更深层业务的开发。
NULL 博文链接:https://88548886.iteye.com/blog/1528486
Hibernate Session的轻量级封装 * 默认情况下运行期异常才会回滚(包括继承了RuntimeException子类),普通异常是不会滚的 * 编写业务逻辑方法时,最好将异常一直向上抛出,在表示层(struts)处理 * 关于事务...
spring配置式事务管理(jdk动态代理,每个service必须对应一个接口) BaseService里注入BaseDao 和transactionTemplate(用于编程式事务处理,只用于特殊需要,因为已经存在配置式事务,一般符合命名的方法会自动...
【第八章】 对ORM的支持 之 8.2 集成Hibernate3 ——跟我学spring3 【第八章】 对ORM的支持 之 8.3 集成iBATIS ——跟我学spring3 【第八章】 对ORM的支持 之 8.4 集成JPA ——跟我学spring3 【第九章】 Spring的...
struts与hibernate集成原理,基础知识补充AOP,编程式事务,声明式事务,spring注入学习笔记
集成了Spring 3.2.2 Hibernate 3.3.2 Struts 2.3.4 用JPA的注解实现orm 不用创建数据库表 只需本机上有oracle即可 用JSR注解进行Spring的依赖注入 用Spring Test整合Junit4 进行测试并自动事务回滚 不对数据库造成...