标签:
一、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方法才行,不要捕捉异常,要让异常自动抛出。否则不能进行事务回滚)
@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); }
简单记录下,仅供参考。
标签:
原文地址:http://www.cnblogs.com/aegisada/p/5485520.html