ssm-spring集成mybatis事务
ssm-spring集成mybatis事务
事务
MyBatis-Spring库的引入,无需创建新的MyBatis事务管理器,就能使MyBatis接入到Spring事。 引入的方式既可以是注解,也可以是aop。未配置事务实例
首先来看看未配置事务时,执行一组先增加后删除(删除异常)的数据库语句场景。数据库连接配置延用之前的,这里不再介绍。- 编写DAO
- mapper配置文件中编写对应sql:
- 分别编写测试方法测试正确的增加和删除: 增加:
- 模拟异常事务 现在模拟场景:新增后立即删除。如果新增成功但是删除失败,此时我们的需求期望时整个过程需要回滚。现在先将删除sql故意写错来模拟,看看异常现象:
public interface StudentMapper {
void add(Map
声明了新增和删除两个接口。
<?xml version="1.0" encoding="UTF-8" ?>
INSERT INTO student (`name`,sex) VALUES (#{name},#{sex});
DELETE FROM student WHERE `name` = #{name};
@Test
public void testTransaction() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
StudentMapper mapper = (StudentMapper) context.getBean("exampleMapper");
Map
运行后确定数据库中有写入一条新的数据:
删除:
@Test
public void testTransaction() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
StudentMapper mapper = (StudentMapper) context.getBean("exampleMapper");
mapper.delete("曹操");
}
运行后正确的将刚才新增的“曹操”记录删除:
DELETE FROM student WHERE `name` = #{name}s;
@Test
public void testTransaction() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
StudentMapper mapper = (StudentMapper) context.getBean("exampleMapper");
Map
运行时发现,新增成功但是删除时抛出了语法错误的异常:
我们看看数据库结果:

虽然整个过程的第二步删除失败了,但数据还是写入到了数据库,这显然与需求不符,下面我们来看看Mybatis-Spring是如何管理事务的。
Mybatis-Spring事务
基于上面的需求,我们现在来看看通过事务,将写入和删除一致性实现。按照官方的事务配置说明,这里介绍注解式事务实现。- 将数据库操作从test中抽离到业务接口
public class StudentServiceImpl implements StudentService { private StudentMapper mapper;public StudentMapper getMapper() { return mapper; } public void setMapper(StudentMapper mapper) { this.mapper = mapper; } @Override public void handle() throws Exception { Map<Object, Object> student = new HashMap<>(); student.put("name", "曹操"); student.put("sex", "男"); mapper.add(student); mapper.delete("曹操"); }
}
注意:提供给transactionManager的数据源一定和创建sqlSessionFactory使用同一个,否则无法生效。
注意:aop切入点一定是注入到mapper对应的业务接口或其实现方法上,否则不生效,也不要用在@Test方法上
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
@Test
public void testTransaction() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
StudentService studentService = (StudentService) context.getBean("studentServiceImpl");
studentService.handle();
}

删除语句依旧报语法错误,我们到数据库中看是否插入成功:

可以看出,并没有写入“曹操”这条数据,说明事务生效。