一 、第一种:全注解声明式事务 

Xml代码 复制代码 收藏代码
.<?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:p="http://www.springframework.org/schema/p"
. xmlns:tx="http://www.springframework.org/schema/tx"
. xmlns:aop="http://www.springframework.org/schema/aop"
. xmlns:context="http://www.springframework.org/schema/context"
. xsi:schemaLocation="http://www.springframework.org/schema/beans
. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
. http://www.springframework.org/schema/tx
. http://www.springframework.org/schema/tx/spring-tx-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/context
. http://www.springframework.org/schema/context/spring-context-2.5.xsd"
. default-init-method="init">
.
. <!-- 引入jdbc配置文件 -->
. <context:property-placeholder location="classpath:jdbc.properties" />
.
. <!--创建jdbc数据源 -->
. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
. <property name="driverClassName" value="${driver}" />
. <property name="url" value="${url}" />
. <property name="username" value="${username}" />
. <property name="password" value="${password}" />
. </bean>
.
. <!-- 创建SqlSessionFactory,同时指定数据源 -->
. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
. <property name="dataSource" ref="dataSource" />
. <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同 -->
. <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
. </bean>
.
. <!-- 配置事务管理器:第一种 全注解声明式事务 -->
. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
. <property name="dataSource" ref="dataSource" />
. </bean>
. <tx:annotation-driven transaction-manager="transactionManager" />
. <!-- 第一种 结束位置 -->
. <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
. <property name="basePackage" value="com.xieke.test.mapper" />
. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
. <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
. </bean>
.
.</beans> 使用时:在需要事务控制的类上加上@Transactional注解就可以了. 二、第二种:使用tx标签配置的拦截器声明式事务 Xml代码 复制代码 收藏代码
.<?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:p="http://www.springframework.org/schema/p"
. xmlns:tx="http://www.springframework.org/schema/tx"
. xmlns:aop="http://www.springframework.org/schema/aop"
. xmlns:context="http://www.springframework.org/schema/context"
. xsi:schemaLocation="http://www.springframework.org/schema/beans
. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
. http://www.springframework.org/schema/tx
. http://www.springframework.org/schema/tx/spring-tx-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/context
. http://www.springframework.org/schema/context/spring-context-2.5.xsd"
. default-init-method="init">
.
. <!-- 引入jdbc配置文件 -->
. <context:property-placeholder location="classpath:jdbc.properties" />
.
. <!--创建jdbc数据源 -->
. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
. <property name="driverClassName" value="${driver}" />
. <property name="url" value="${url}" />
. <property name="username" value="${username}" />
. <property name="password" value="${password}" />
. </bean>
.
. <!-- 创建SqlSessionFactory,同时指定数据源 -->
. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
. <property name="dataSource" ref="dataSource" />
. <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同 -->
. <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
. </bean>
.
. <!-- 配置事务管理器:第二种 使用tx标签配置的拦截器声明式事务 -->
. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
. <property name="dataSource" ref="dataSource" />
. </bean>
. <!-- 配置事务的传播特性 -->
. <tx:advice id="txAdvice" transaction-manager="transactionManager">
. <tx:attributes>
. <tx:method name="add*" propagation="NESTED" />
. <tx:method name="del*" propagation="NESTED" />
. <tx:method name="*" read-only="true" />
. </tx:attributes>
. </tx:advice>
. <!-- 配置事务的切入点 -->
. <aop:config>
. <aop:pointcut id="targetMethod" expression="execution(* com.xieke.test.service.impl.*.*(..))" />
. <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />
. </aop:config>
. <!-- 第二种 结束结束位置 -->
.
. <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
. <property name="basePackage" value="com.xieke.test.mapper" />
. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
. <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
. </bean>
.
.</beans> 三、 第三种:使用拦截器声明式事务 Xml代码 复制代码 收藏代码
.<?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:p="http://www.springframework.org/schema/p"
. xmlns:tx="http://www.springframework.org/schema/tx"
. xmlns:aop="http://www.springframework.org/schema/aop"
. xmlns:context="http://www.springframework.org/schema/context"
. xsi:schemaLocation="http://www.springframework.org/schema/beans
. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
. http://www.springframework.org/schema/tx
. http://www.springframework.org/schema/tx/spring-tx-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/context
. http://www.springframework.org/schema/context/spring-context-2.5.xsd"
. default-init-method="init">
.
. <!-- 引入jdbc配置文件 -->
. <context:property-placeholder location="classpath:jdbc.properties" />
.
. <!--创建jdbc数据源 -->
. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
. <property name="driverClassName" value="${driver}" />
. <property name="url" value="${url}" />
. <property name="username" value="${username}" />
. <property name="password" value="${password}" />
. </bean>
.
. <!-- 创建SqlSessionFactory,同时指定数据源 -->
. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
. <property name="dataSource" ref="dataSource" />
. <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同 -->
. <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
. </bean>
.
. <!-- 配置事务管理器:第三种 使用拦截器声明式事务 -->
. <bean id="transactionManager"
. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
. <property name="dataSource" ref="dataSource" />
. </bean>
. <bean id="transactionInterceptor"
. class="org.springframework.transaction.interceptor.TransactionInterceptor">
. <property name="transactionManager" ref="transactionManager" />
. <!-- 配置事务属性 -->
. <property name="transactionAttributes">
. <props>
. <!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务 -->
. <prop key="add*">PROPAGATION_REQUIRED</prop>
. <prop key="del*">PROPAGATION_REQUIRED</prop>
. </props>
. </property>
. </bean>
. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
. <property name="beanNames">
. <list>
. <value>*ServiceImpl</value>
. </list>
. </property>
. <property name="interceptorNames">
. <list>
. <value>transactionInterceptor</value>
. </list>
. </property>
. </bean>
. <!-- 第三种 结束位置 -->
.
. <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
. <property name="basePackage" value="com.xieke.test.mapper" />
. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
. <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
. </bean>
.
.</beans>

Spring事务的传播属性:
下载地址   
PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

Spring事务管理的三种方式的更多相关文章

  1. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  2. Spring事务管理的四种方式(以银行转账为例)

    Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败.   原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...

  3. 配置spring事务管理的几种方式(声明式事务)

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  4. Spring事务管理之几种方式实现事务(转)

    一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  5. Spring事务管理之几种方式实现事务

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  6. Spring事务管理的两种方式

    参考文档: http://www.iteye.com/topic/1123347 http://blog.csdn.net/lcj8/article/details/2835432 PS:好像还是tx ...

  7. Spring事务配置的五种方式(转发)

    Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...

  8. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

    转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...

  9. spring生成EntityManagerFactory的三种方式

    spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...

随机推荐

  1. App Widget

    AppWidgetProviderInfo对象: 为App Widget提供元数据(描述数据的数据,如XML.关系型数据的表结构),包括布 局,更新频率等数据.这个对象被定义在XML文件当中: App ...

  2. jQuery方法position()与offset()区别

    参考别人写得比较明白的,红色部分为重点吧: 使用jQuery获取元素位置时,我们会使用position()或offset()方法,两个方法都返回一个包含两个属性的对象-左边距和上边距,它们两个的不同点 ...

  3. 3.Java基础之Date对象

    毕向东老师Java基础学习笔记——Date对象 今天学习Java中的Date对象后,感觉这个对象对我们主要有以下几点用处. 1.获取时间和日期并按照自己定义的格式显示. 2.网站设计时显示时间.  知 ...

  4. 小白Linux入门 三

    环境变量 shell 变量: 内存空间 ,命名的内存空间 echo $SHELL 其中SHELL是变量 里面是/bin/bash sudo su  进入root printenv 命令 命令: 内部命 ...

  5. 关系数据库SQL之可编程性函数(用户自定义函数)

    前言 在关系型数据库中除了前面几篇基本的数据库和数据表操作之外,还提供了可编程性的函数.存储过程.事务.触发器及游标. 本文介绍的是函数. 函数分为两种: 系统函数 用户自定义函数 准备工作 这里以银 ...

  6. HtmlHelper拓展实现CheckBoxList

    经过一番折腾(主要是SelectList这个类操作有些繁琐)实现了CheckBoxList,过程RadioList基本一样 拓展方法 public static MvcHtmlString Check ...

  7. 基于Metronic的Bootstrap开发框架经验总结(12)--页面链接收藏夹功能的实现

    在一个系统里面,往往有很多菜单项目,每个菜单项对应一个页面,一般用户只需要用到一些常用的功能,如果每次都需要去各个层次的菜单里面去找对应的功能,那确实有点繁琐.特别是在菜单繁多,而客户又对系统整体不熟 ...

  8. 探索DOMNode

    实现代码 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...

  9. SQL Server帐号孤立的问题解决

    网站近日经常遭到攻击,好几次数据库挂马,前几天把论坛升级了,今天又升级了数据库,把之前的MSSQL 2000 升级到MSSQL 2005,用的是数据库还原功能还原的,遇到了这个帐号孤立的问题. 什么是 ...

  10. LA4287--tarjan

    题目大意: 在数学中,我们常常需要完成若干个命题的等价性证明.比如,有4个命题a,b,c,d,我们证明a↔b,然后b↔c,最后c↔d.注意每次证明都是双向的,因此一共完成了6次推导.另一种方法是a→b ...