当前位置 : 主页 > 编程语言 > java >

springmvc、MyBatis事务管理回滚失败、单元测试回滚却成功

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt spring配置文档applicationContext.xml 业务逻辑层 @Transactional public int updateByID(Base base){ if(basePicMapper.updateByID(base.getBasePic())=0){ throw new RuntimeException(); } int i=2/0; if(baseMapper.updateByID(b
gistfile1.txt
spring配置文档
applicationContext.xml
   
 
    
    

    

    

    

业务逻辑层

    @Transactional
    public int updateByID(Base base){

        if(basePicMapper.updateByID(base.getBasePic())<=0){
            throw new RuntimeException();
        }

        int i=2/0;
        if(baseMapper.updateByID(base)<=0){
            throw new RuntimeException();
        }
        return 1;
    }

控制层


    @RequestMapping("updateTextPic")
    public String updateTextPic(Base model, HttpServletRequest request, HttpServletResponse response) {
        Base base=baseService.selectByID(model.getId());

        base.setContent(model.getContent());
        base.getBasePic().setLink(model.getBasePic().getLink());
        baseService.updateByID(base);

        request.setAttribute("rdtPath","/admin/list/base");
        return "/redirect_page";
    }

这样没有回滚

一个声明bean实例的工具类
public class SpringUtil{
    public Object getBean(String beanName){
        ApplicationContext xtx=new ClassPathXmlApplicationContext("applicationContext.xml");
        return xtx.getBean(beanName);
    }
}

单元测试就可以回滚
applicationContext.xml配置bean
    

单元测试代码
public class MenutypeServiceTest {
    MenuTypeService menutypeService;
    @Before
    public void setUp() throws Exception {
        menutypeService =(MenuTypeService) (new SpringUtil()).getBean("menuTypeServices");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void selectAll() throws Exception {
        System.out.println(menutypeService);
    }

}

通过控制台调用不能回滚,通过单元测试调用就能回滚。为什么?试过在控制层里用SpringUtil从配置文档获取一个bean实例而不是自动注入,依然无法回滚。
另一个问题,需不需要判断插入、修改的语句返回值是否大于0?为什么?
网友评论