MyBatis6:MyBatis集成Spring事物管理(下篇)
前言
前一篇文章《MyBatis5:MyBatis 集成 Spring 事物管理(上篇)》复习了 MyBatis 的基本使用以及使用 Spring 管理 MyBatis 的事物的做法,本文的目的是在这个的基础上稍微做一点点的进阶:多数据的事物处理。文章内容主要包含两方面:
1、单表多数据的事物处理
2、多库 / 多表多数据的事物处理
这两种都是企业级开发中常见的需求,有一定的类似,在处理的方法与技巧上又各有不同,在进入文章前,先做一些准备工作,因为后面会用到多表的插入事物管理,前面的文章建立了一个 Student 相关表及类,这里再建立一个 Teacher 相关的表及类。第一步是建立一张 Teacher 表:
create table teacher ( teacher_id int auto_increment, teacher_name varchar(20) not null, primary key(teacher_id) )
建立 teacher_mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="TeacherMapper">
<resultMap type="Teacher" id="TeacherMap">
<id column="teacher_id" property="teacherId" jdbcType="INTEGER" />
<result column="teacher_name" property="teacherName" jdbcType="VARCHAR" />
</resultMap><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">select </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="selectAllTeachers"</span><span style="color: rgba(255, 0, 0, 1)"> resultMap</span><span style="color: rgba(0, 0, 255, 1)">="TeacherMap"</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)"> select teacher_id, teacher_name from teacher; </span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">select</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">insert </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="insertTeacher"</span><span style="color: rgba(255, 0, 0, 1)"> useGeneratedKeys</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> keyProperty</span><span style="color: rgba(0, 0, 255, 1)">="teacher_id"</span><span style="color: rgba(255, 0, 0, 1)"> parameterType</span><span style="color: rgba(0, 0, 255, 1)">="Teacher"</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)"> insert into teacher(teacher_id, teacher_name) values(null, #{teacherName, jdbcType=VARCHAR}); </span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">insert</span><span style="color: rgba(0, 0, 255, 1)">></span>
</mapper>
建立 Teacher.java:
public class Teacher { private int teacherId; private String teacherName;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getTeacherId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> teacherId; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setTeacherId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> teacherId) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.teacherId =<span style="color: rgba(0, 0, 0, 1)"> teacherId; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getTeacherName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> teacherName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setTeacherName(String teacherName) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.teacherName =<span style="color: rgba(0, 0, 0, 1)"> teacherName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Teacher{teacherId:" + teacherId + "], [teacherName:" + teacherName + "}"<span style="color: rgba(0, 0, 0, 1)">; }
}
还是再次提醒一下,推荐重写 toString() 方法,打印关键属性。不要忘了在 config.xml 里面给 Teacher.java 声明一个别名:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>
<typeAliases>
<typeAlias alias="Student" type="org.xrq.domain.Student" />
<typeAlias alias="Teacher" type="org.xrq.domain.Teacher" />
</typeAliases>
</configuration>
接着是 TeacherDao.java 接口:
public interface TeacherDao { public List<Teacher> selectAllTeachers(); public int insertTeacher(Teacher teacher); }
其实现类 TeacherDaoImpl.java:
@Repository public class TeacherDaoImpl extends SqlSessionDaoSupport implements TeacherDao { private static final String NAMESPACE = "TeacherMapper.";@Resource </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.setSqlSessionFactory(sqlSessionFactory); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<Teacher><span style="color: rgba(0, 0, 0, 1)"> selectAllTeachers() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> getSqlSession().selectList(NAMESPACE + "selectAllTeachers"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> insertTeacher(Teacher teacher) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> getSqlSession().insert(NAMESPACE + "insertTeacher"<span style="color: rgba(0, 0, 0, 1)">, teacher); }
}
OK,这样准备工作就全部做完了,有需要的朋友可以实际去把 TeacherDao 中的方法正确性先验证一下,下面进入文章的内容。
单表事物管理
有一个很常见的需求,在同一张表里面,我想批量插入 100 条数据,但是由于这 100 条数据之间存在一定的相关性,只要其中任何一条事物的插入失败,之前插入成功的数据就全部回滚,这应当如何实现?这里有两种解决方案:
1、使用 MyBatis 的批量插入功能
2、使用 Spring 管理事物,任何一条数据插入失败
由于我们限定的前提是单表,因此比较推荐的是第一种做法。
第二种做法尽管也可以实现我们的目标,但是每插入一条数据就要发起一次数据库连接,即使使用了数据库连接池,但在性能上依然有一定程度的损失。而使用 MyBatis 的批量插入功能,只需要发起一次数据库的连接,这 100 次的插入操作在 MyBatis 看来是一个整体,其中任何一个插入的失败都将导致整体插入操作的失败,即:要么全部成功,要么全部失败。
下面来看一下实现,首先在 student_mapper.xml 中新增一个批量新增的方法 <insert>:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="StudentMapper">
<resultMap type="Student" id="StudentMap">
<id column="student_id" property="studentId" jdbcType="INTEGER" />
<result column="student_name" property="studentName" jdbcType="VARCHAR" />
</resultMap>... </span><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">insert </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="batchInsert"</span><span style="color: rgba(255, 0, 0, 1)"> useGeneratedKeys</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> parameterType</span><span style="color: rgba(0, 0, 255, 1)">="java.util.List"</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">selectKey </span><span style="color: rgba(255, 0, 0, 1)">resultType</span><span style="color: rgba(0, 0, 255, 1)">="int"</span><span style="color: rgba(255, 0, 0, 1)"> keyProperty</span><span style="color: rgba(0, 0, 255, 1)">="studentId"</span><span style="color: rgba(255, 0, 0, 1)"> order</span><span style="color: rgba(0, 0, 255, 1)">="AFTER"</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)"> SELECT LAST_INSERT_ID() </span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">selectKey</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)"> insert into student(student_id, student_name) values </span><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">foreach </span><span style="color: rgba(255, 0, 0, 1)">collection</span><span style="color: rgba(0, 0, 255, 1)">="list"</span><span style="color: rgba(255, 0, 0, 1)"> item</span><span style="color: rgba(0, 0, 255, 1)">="item"</span><span style="color: rgba(255, 0, 0, 1)"> index</span><span style="color: rgba(0, 0, 255, 1)">="index"</span><span style="color: rgba(255, 0, 0, 1)"> separator</span><span style="color: rgba(0, 0, 255, 1)">=","</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)"> (#{item.studentId, jdbcType=INTEGER}, #{item.studentName, jdbcType=VARCHAR}) </span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">foreach</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">insert</span><span style="color: rgba(0, 0, 255, 1)">></span>
</mapper>
这里主要是利用 MyBatis 提供的 foreach,对传入的 List 做了一次遍历,并取得其中的属性进行插入。
然后在 StudentDao.java 中新增一个批量新增的方法 batchInsert:
public interface StudentDao { public List<Student> selectAllStudents(); public int insertStudent(Student student); public int batchInsertStudents(List<Student> studentList);}
StudentDaoImpl.java 实现它:
@Repository public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao { private static final String NAMESPACE = "StudentMapper.";@Resource </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.setSqlSessionFactory(sqlSessionFactory); }
...</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> batchInsertStudents(List<Student><span style="color: rgba(0, 0, 0, 1)"> studentList) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> getSqlSession().insert(NAMESPACE + "batchInsert"<span style="color: rgba(0, 0, 0, 1)">, studentList); }
}
接着验证一下,首先 drop 一下 student 这张表并重新建一下,然后写一段测试程序:
public class StudentTest { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); StudentDao studentDao = (StudentDao)ac.getBean("studentDaoImpl"); List<Student> studentList = null;Student student0 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student(); student0.setStudentName(</span>"Smith"<span style="color: rgba(0, 0, 0, 1)">); Student student1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student(); student1.setStudentName(</span>"ArmStrong"<span style="color: rgba(0, 0, 0, 1)">); studentList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); studentList.add(student0); studentList.add(student1); studentDao.batchInsertStudents(studentList); System.out.println(</span>"-----Display students------"<span style="color: rgba(0, 0, 0, 1)">); studentList </span>=<span style="color: rgba(0, 0, 0, 1)"> studentDao.selectAllStudents(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0, length = studentList.size(); i < length; i++<span style="color: rgba(0, 0, 0, 1)">) System.out.println(studentList.get(i)); }
}
运行结果为:
-----Display students------ Student{[studentId:1], [studentName:Smith]} Student{[studentId:2], [studentName:ArmStrong]}
看到批量插入成功。
从另外一个角度来看,假如我们这么建立这个 studentList:
Student student0 = new Student(); student0.setStudentName("Smith"); Student student1 = new Student(); student1.setStudentName(null); studentList = new ArrayList<>(); studentList.add(student0); studentList.add(student1); studentDao.batchInsertStudents(studentList);
故意制造第一条插入 OK,第二条插入报错的场景,此时再运行一下程序,程序会抛出异常,即使第一条数据是 OK 的,依然不会插入。
最后,这里是批量插入,批量修改、批量删除也是一样的做法,可以自己试验一下。
多库 / 多表事物管理
上面的场景是对于单表的事物管理做法的推荐:实际上这并没有用到事物管理,而是使用 MyBatis 批量操作数据的做法,目的是为了减少和数据库的交互次数。
现在有另外一种场景,我要对单库 / 多库的两张表(Student 表、Teacher 表)同时插入一条数据,要么全部成功,要么全部失败,该如何处理?此时明显就不可以使用 MyBatis 批量操作的方法了,要实现这个功能,可以使用 Spring 的事物管理。
前面文章有讲,Dao 层中的方法更多的是一种对数据库的增删改查的原子性操作,而 Service 层中的方法相当于对这些原子性的操作做一个组合,这里要同时操作 TeacherDao、StudentDao 中的 insert 方法,因此建立一个 SchoolService 接口:
public interface SchoolService { public void insertTeacherAndStudent(Teacher teacher, Student student); }
写一下这个接口的实现类:
@Service public class SchoolServiceImpl implements SchoolService { @Resource private StudentDao studentDao;@Resource </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> TeacherDao teacherDao; @Transactional </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertTeacherAndStudent(Teacher teacher, Student student) { studentDao.insertStudent(student); teacherDao.insertTeacher(teacher); }
}
这里用到了两个注解,解释一下。
(1)@Service 注解
严格地说这里使用 @Service 注解不是特别好,因为 Service 作为服务层,更多的是应该对同一个 Dao 中的多个方法进行组合,如果要用到多个 Dao 中的方法,建议应该是放到 Controller 层中,引入两个 Service,这里为了简单,就简单在一个 Service 中注入了 StudentDao 和 TeacherDao 两个了。
(2)@Transactional 注解
这个注解用于开启事物管理,注意 @Transactional 注解的使用前提是该方法所在的类是一个 Spring Bean,因此(1)中的 @Service 注解是必须的。换句话说,假如你给方法加了 @Transactional 注解却没有给类加 @Service、@Repository、@Controller、@Component 四个注解其中之一将类声明为一个 Spring 的 Bean,那么对方法的事物管理,是不会起作用的。关于 @Transactional 注解,会在下面进一步解读。
接着写一个测试类测试一下:
public class SchoolTest { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); SchoolService schoolService = (SchoolService)ac.getBean("schoolServiceImpl");Student student </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student(); student.setStudentName(</span>"Billy"<span style="color: rgba(0, 0, 0, 1)">); Teacher teacher </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Teacher(); teacher.setTeacherName(</span>"Luna"<span style="color: rgba(0, 0, 0, 1)">); schoolService.insertTeacherAndStudent(teacher, student); }
}
可以看一下数据库,Student 表和 Teacher 表会同时多一条记录。接着继续从另外一个角度讲,我这么建立 Student 和 Teacher:
Student student = new Student(); student.setStudentName("White"); Teacher teacher = new Teacher(); teacher.setTeacherName(null);
故意制造 Teacher 报错的场景,此时尽管 Student 没有问题,但是由于 Teacher 插入报错,因此 Student 的插入进行回滚,查看 Student 表,是不会有 student_name 为 "White" 这条记录的。
@Transactional 注解
@Transactional 这个注解绝对是 Java 程序员的一个福音,如果没有 @Transactional 注解,我们使用配置文件的做法进行声明式事务管理,我网上随便找一段配置文件:
<!-- 事物切面配置 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> <tx:method name="insert" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice><aop:config>
<aop:pointcut id="testService" expression="execution (* com.baobao.service.MyBatisService.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="testService"/>
</aop:config>
这种声明式的做法不得不说非常不好控制以及进行调试,尤其在要进行事物管理的内容不断增多之后,尤其体现出它的不方便。
使用 @Transactional 注解就不一样了,它可以精细到具体的类甚至具体的方法上(区别是同一个类,对方法的事物管理配置会覆盖对类的事务管理配置),另外,声明式事物中的一些属性,在 @Transaction 注解中都可以进行配置,下面总结一下常用的一些属性。
(1) @Transactional(propagation = Propagation.REQUIRED)
最重要的先说,propagation 属性表示的是事物的传播特性,一共有以下几种:
事物传播特性 | 作 用 |
Propagation.REQUIRED | 方法运行时如果已经处在一个事物中,那么就加入到这个事物中,否则自己新建一个事物,REQUIRED 是默认的事物传播特性 |
Propagation.NOT_SUPPORTED | 如果方法没有关联到一个事物,容器不会为它开启一个事物,如果方法在一个事物中被调用,该事物会被挂起直到方法调用结束再继续执行 |
Propagation.REQUIRES_NEW | 不管是否存在事物,该方法总会为自己发起一个新的事物,如果方法已经运行在一个事物中,则原有事物挂起,新的事物被创建 |
Propagation.MANDATORY | 该方法只能在一个已经存在的事物中执行,业务方法不能发起自己的事物,如果在没有事物的环境下被调用,容器抛出异常 |
Propagation.SUPPORTS | 该方法在某个事物范围内被调用,则方法成为该事物的一部分,如果方法在该事物范围内被调用,该方法就在没有事物的环境下执行 |
Propagation.NEVER | 该方法绝对不能在事物范围内执行,如果在就抛出异常,只有该方法没有关联到任何事物,才正常执行 |
Propagation.NESTED | 如果一个活动的事物存在,则运行在一个嵌套的事物中。如果没有活动事物,则按 REQUIRED 属性执行,它只对 DataSourceTransactionManager 事物管理器有效 |
因此我们可以来简单分析一下上面的 insertTeacherAndStudent 方法:
- 由于没有指定 propagation 属性,因此事物传播特性为默认的 REQUIRED
- StudentDao 的 insertStudent 方法先运行,此时没有事物,因此新建一个事物
- TeacherDao 的 insertTeacher 方法接着运行,此时由于 StudentDao 的 insertStudent 方法已经开启了一个事物,insertTeacher 方法加入到这个事物中
- StudentDao 的 insertStudent 方法和 TeacherDao 的 insertTeacher 方法组成了一个事物,两个方法要么同时执行成功,要么同时执行失败
(2)@Transactional(isolation = Isolation.DEFAULT)
事物隔离级别,这个不细说了,可以参看事物及事物隔离级别一文。
(3)@Transactional(readOnly = true)
该事物是否为一个只读事物,配置这个属性可以提高方法执行效率。
(4)@Transactional(rollbackFor = {ArrayIndexOutOfBoundsException.class, NullPointerException.class})
遇到方法抛出 ArrayIndexOutOfBoundsException、NullPointerException 两种异常会回滚数据,仅支持 RuntimeException 的子类。
(5)@Transactional(noRollbackFor = {ArrayIndexOutOfBoundsException.class, NullPointerException.class})
这个和上面的相反,遇到 ArrayIndexOutOfBoundsException、NullPointerException 两种异常不会回滚数据,同样也是仅支持 RuntimeException 的子类。对(4)、(5)不是很理解的朋友,我给一个例子:
@Transactional(rollbackForClassName = {"NullPointerException"}) public void insertTeacherAndStudent(Teacher teacher, Student student) {studentDao.insertStudent(student); teacherDao.insertTeacher(teacher); String s = null; s.length();}
构造 Student、Teacher 的数据运行一下,然后查看下库里面有没有对应的记录就好了,然后再把 rollbackForClassName 改为 noRollbackForClassName,对比观察一下。
(6)@Transactional(rollbackForClassName = {"NullPointerException"})、@Transactional(noRollbackForClassName = {"NullPointerException"})
这两个放在一起说了,和上面的(4)、(5)差不多,无非是(4)、(5)是通过.class 来指定要回滚和不要回滚的异常,这里是通过字符串形式的名字来制定要回滚和不要回滚的异常。
(7)@Transactional(timeout = 30)
事物超时时间,单位为秒。
(8)@Transactional(value = "tran_1")
value 这个属性主要就是给某个事物一个名字而已,这样在别的地方就可以使用这个事物的配置。