SSM框架下声明式事务管理(注解配置方式)
一、spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
"> <!-- 数据源设置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</bean> <!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:xy/mapping/*.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xy.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* xy.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config> <!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor"
class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
scope="prototype">
<property name="patterns">
<list>
<value>xy.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut" />
</aop:config> </beans>
要加入的配置代码
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
二、在调用的方法上加上@Transactional注解(必须为public方法才行,不要捕捉异常,要让异常自动抛出,否则不能进行事务回滚。方法要写在服务层中在controller中无效)
@Transactional
@Override
public void transactionalTest(List<Tuser> list) {
for(int i=0;i<list.size();i++){
/* if(i==0){
tuserMapper.insertSelective(list.get(i));
}else{
throw new RuntimeException();
}*/ if(i==0){
tuserMapper.updateByPrimaryKeySelective(list.get(i));
}else if(i==1){
//int j=1/0;//产生异常
}else{
tuserMapper.insertSelective(list.get(i));
}
}
}
三、测试方法(用JUnit进行测试)
@Test
public void test4(){
List<Tuser> list = new ArrayList<Tuser>();
Tuser tuser = new Tuser();
tuser.setId("0ce64eea-98d6-462b-9982-4b0816126495");
tuser.setName("name1edit");
tuser.setPwd("0");
tuser.setSjh("111");
list.add(tuser);
//int l = userService.insertSelective(tuser);
//int i=1/0;
Tuser tuser1 = new Tuser();
tuser1.setId(UUID.randomUUID().toString());
tuser1.setName("name2");
tuser1.setPwd("1");
tuser1.setSjh("222");
list.add(tuser1);
Tuser tuser2 = new Tuser();
tuser2.setId(UUID.randomUUID().toString());
tuser2.setName("name3");
tuser2.setPwd("2");
tuser2.setSjh("333");
list.add(tuser2);
//int l2 = userService.insertSelective(tuser1);
userService.transactionalTest(list);
}
简单记录下,仅供参考。
SSM框架下声明式事务管理(注解配置方式)的更多相关文章
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- Spring声明式事务管理与配置详解
转载:http://www.cnblogs.com/hellojava/archive/2012/11/21/2780694.html 1.Spring声明式事务配置的五种方式 前段时间对Spring ...
- spring4声明式事务—02 xml配置方式
1.配置普通的 controller,service ,dao 的bean. <!-- 配置 dao ,service --> <bean id="bookShopDao& ...
- spring4声明式事务—02 xml配置方式
1.配置普通的 controller,service ,dao 的bean. <!-- 配置 dao ,service --> <bean id="bookShopDao& ...
- 零基础学习java------39---------json格式交互,Restful(不懂),静态资源映射,SSM整合(ssm整合思想,application.xml文件详解(声明式事务管理),)
一. json格式交互(知道) 1 . 回顾ajax基本语法 $.ajax({ url:"", // 请求的后台路径 data:{"":"" ...
- Spring—SSJ集成&声明式事务管理
1. 课程介绍 1. SSJ集成;(掌握) 2. 声明式事务管理;(掌握) 什么是三大框架 2.1. ssh Struts/Struts2 Spring Hibernate 2.2. ss ...
- 【Spring】Spring的事务管理 - 2、声明式事务管理(实现基于XML、Annotation的方式。)
声明式事务管理 文章目录 声明式事务管理 基于XML方式的声明式事务 基于Annotation方式的声明式事务 简单记录 - 简单记录-Java EE企业级应用开发教程(Spring+Spring M ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)
在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
随机推荐
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- python爬虫 - Urllib库及cookie的使用
http://blog.csdn.net/pipisorry/article/details/47905781 lz提示一点,python3中urllib包括了py2中的urllib+urllib2. ...
- EventBus的其他常用函数
上一篇EventBus最简易使用方式介绍了EventBus最简易的使用方式,摆脱了叽里呱啦+图片的长篇大论.目的是为了让刚开始接触的人们不晕头转向.那么这篇..我也要开始图片+叽里呱啦了. 转载请注明 ...
- 如何反编译APK?
1.概述 一些商业的app都包含很多精美的图片还有一些比较好的配置文件,以前某师兄就说过apk把后缀改为zip,然后解压一下就可以获得很多图片资源,但是这时候你打开一下解压出来的xml资源全是乱码.通 ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- Android虚拟机 USB转串口调试方法
有时候需要在虚拟机调试串口,首先安装串口的驱动程序(不知道的话可以用驱动精灵),然后打开设备管理器找到驱动,查看驱动使用的端口(比如COM3),虚拟机需要在命令行启动: 将SDK下的tools文件夹加 ...
- OpenCV进行图像相似度对比的几种办法
转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/43853435, 来自:shiter编写程序的艺术 对计算图像相似度的方法,本文做 ...
- Git与远程reposiory的相关命令
问题1:Git如何同步远程repository的分支(branch) 某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢? 分析: ...
- Working with bounded Task Flows
Working with Bounded Task Flows, Regions and Routers in JDeveloper 11g Purpose In this tutorial, y ...
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...