springboot+mybatis 事务管理

之前就调研过基于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方法

@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运行时,发生运行时异常,异常抛出,则回滚。在之前删除的数据会回滚。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值