您的位置:首页 > 编程语言 > Java开发

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方法

@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运行时,发生运行时异常,异常抛出,则回滚。在之前删除的数据会回滚。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: