springboot+mybatis 事务管理
2016-10-13 18:07
477 查看
之前就调研过基于spring和hibernate的事务管理配置,在项目中需要使用mybatis,所有就测试了一下如何使用,感觉还是比较方便的。记录如下:
1、如何测试事务?
应用场景是有一个删除操作,需要删除多个表的多条记录,并且要求,要嘛都删除,要嘛都不删除。
2、mybatis的sql.xml文件中添加一个删除方法
<!-- 删除一条记录 -->
<delete id="deleteByOneCond" parameterType="java.util.Map">
delete from ${params._tableName} where ${params._columnName} = #{params._value}
</delete>
3、applicationcontext.xml中添加
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="MarketingDataSource"></property>
</bean>
<tx:annotation-driven/>
<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"
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">
4、添加一个服务,需要使用多个delete方法
5、测试事务是否正常运行
1、如何测试事务?
应用场景是有一个删除操作,需要删除多个表的多条记录,并且要求,要嘛都删除,要嘛都不删除。
2、mybatis的sql.xml文件中添加一个删除方法
<!-- 删除一条记录 -->
<delete id="deleteByOneCond" parameterType="java.util.Map">
delete from ${params._tableName} where ${params._columnName} = #{params._value}
</delete>
3、applicationcontext.xml中添加
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="MarketingDataSource"></property>
</bean>
<tx:annotation-driven/>
<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"
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">
4、添加一个服务,需要使用多个delete方法
@Transactional(isolation=Isolation.SERIALIZABLE,timeout=3000) @Override public BaseResponse delTheme(BaseBusinessRequest request) { DelThemeEntity delThemeEntity=(DelThemeEntity)request; BaseResponse baseResponse =new BaseResponse(); Map map=new HashMap<String,Object>(); String theme_en=delThemeEntity.getTheme_en(); //获取kpis map.put("theme_en", theme_en); List<KpiEntity> kpis=magiccubeDao2.getKpis(map); //先删除kpi下面的figures for(KpiEntity kpi:kpis){ map=new HashMap<String,Object>(); map.put("_tableName", "KF_MAGICCUBE_ONLINE_FIGURE"); map.put("_columnName", "kpi_en"); map.put("_value", kpi.getKpi_en()); magiccubeDao2.deleteByOneCond(map); } <span style="color:#FF0000;"> //人为制造异常抛出 //int a =1/0;</span> //再删除theme下面的kpi map=new HashMap<String,Object>(); map.put("_tableName", "KF_MAGICCUBE_ONLINE_KPI"); map.put("_columnName", "theme_en"); map.put("_value", theme_en); magiccubeDao2.deleteByOneCond(map); //最后删除theme本身 map=new HashMap<String,Object>(); map.put("_tableName", "KF_MAGICCUBE_ONLINE_THEME"); map.put("_columnName", "theme_en"); map.put("_value", theme_en); magiccubeDao2.deleteByOneCond(map); baseResponse.setReturn_code(StatusCode.OK.getValue()); baseResponse.setReturn_msg(StatusCode.OK.getName()); return baseResponse; }
5、测试事务是否正常运行
locations={ "classpath:applicationContext.xml" })很重要,告诉springboot同时加载xml,不然在DI时会报找不到bean的错误
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MagicCube.class,locations={ "classpath:applicationContext.xml" })//springboot 启动类 @WebAppConfiguration public class OnlineServiceImplTest { @Autowired public IOnlineService onlineService; @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testTransactionalDel() { DelThemeEntity delEntity=new DelThemeEntity(); delEntity.setTheme_en("huanlegou_sale"); onlineService.delTheme(delEntity); } }6、在1/0运行时,发生运行时异常,异常抛出,则回滚。在之前删除的数据会回滚。
相关文章推荐
- SpringCloud SpringBoot mybatis 分布式微服务(十八)Spring Boot中的事务管理
- spring boot配置mybatis和事务管理
- spring boot配置mybatis和事务管理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十一)事务管理(1)
- springboot+shiro+mybatis整合发现部分功能事务没有被spring管理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十二)事务管理(2)
- Springboot+Mybatis+Druid 实现多数据源,与事务管理
- spring boot配置mybatis和事务管理
- springboot mybatis 事务管理
- spring boot配置mybatis和事务管理
- mybatis集成spring的事务管理
- Spring Transaction + MyBatis SqlSession事务管理机制研究学习
- Mybatis + SpringMVC事务管理
- spring + springmvc+ mybatis 事务管理及控制
- Spring+mybatis中事务管理
- mybatis集成spring的事务管理
- springboot+mybatis+mysql事务未生效
- MyBatis+Spring 事务管理
- spring+mybatis管理多个数据源(非分布式事务)
- Spring Transaction + MyBatis SqlSession事务管理机制[marked]