Springboot+ibatis 事务控制

guthub:https://github.com/liubin192837/spring/tree/master/springbootIbatisTransaction

1.首先声明开启事务

@SpringBootApplication
@EnableTransactionManagement
public class SpringbootibatisApplication {
   public static void main(String[] args) {
      SpringApplication.run(SpringbootibatisApplication.class, args);
   }
}
2.在数据访问的接口注解事务属性

@Service
public class GirlServiceImpl implements GirlService{
   @Transactional
   @Override
   public boolean saveGirl(String name, Integer age) throws Exception {
       Girl girl = new Girl();
       girl.setAge(age);
       girl.setName(name);
       girlMapper.saveGirl(girl);
       String a = null;
       /*测试事务是否回滚*/
       //测试1
       /*int i = 1/0;*/
       //测试2,被catch的异常不会触发回滚
/*      try {
           int i = 1 / 0;
       } catch (Exception e) {
           e.printStackTrace();
       }*/

       return true;

   }
 }

3.测试回滚

1)测试1
输入:http://localhost:8080/girls/saveGirl/test/19
查询数据库:
mysql> select * from girls;
Empty set (0.00 sec)
mysql> select * from girls;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | asa  |   19 |
+----+------+------+
1 row in set (0.00 sec)




猜你喜欢

转载自blog.csdn.net/liubin192837/article/details/79642141