SpringBoot整合Mybatis中如何实现事务控制
1、在Spring之中提供的事务控制里面有以下的几种事务级别。

2、如果现在要想在springboot里面去启用mybatis事务,那么就可以通过如下的方式来解决。
修改IDeptDAO接口,追加一个只读事务控制:
package com.gwolf.service;
import com.gwolf.vo.Dept;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface IDeptService {
@Transactional(readOnly = true)
public List<Dept> getAll();
}

3、此时配置了一个只读的事务操作,那么也就是说在这个业务方法只能够采用读的模式来进行操作。但是现在你配置了一个注解并不表示当前已经合理的支持了事务,如果要进行事务的启用,还需要在程序的启动类上增加一个新的注解配置:
package com.gwolf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication //启动SpringBoot程序,而后自带子包扫描。
@EnableTransactionManagement
public class StartSpringBootMain {
public static void main(String[] args) {
SpringApplication.run(StartSpringBootMain.class, args);
}
}

4、如果现在要想更好的观察到事务的问题,最简单的做法是编写一个数据增加操作,而后为这个业务方法设置只读配置。修改IDeptDAO接口追加一个新的方法:
package com.gwolf.dao;
import com.gwolf.vo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Mapper
public interface IDeptDAO {
public List<Dept> findAll();
public boolean doCreate(Dept vo);
}

5、修改Dept.xml配置文件,追加一个方法的实现的SQL语句。
<?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="com.gwolf.dao.IDeptDAO">
<select id="findAll" resultType="Dept">
SELECT deptno,dname,loc FROM dept ;
</select>
<insert id="doCreate" parameterType="Dept">
insert into dept(dname) values(#{dname});
</insert>
</mapper>

6、在IDeptService接口之中追加有一个业务方法:
package com.gwolf.service;
import com.gwolf.vo.Dept;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface IDeptService {
@Transactional(readOnly = true)
public List<Dept> getAll();
@Transactional(readOnly = true)
public boolean add(Dept dept);
}

7、编写单元测试类:
package com.gwolf.test;
import com.gwolf.StartSpringBootMain;
import com.gwolf.service.IDeptService;
import com.gwolf.vo.Dept;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestDeptService {
@Resource
private IDeptService deptService;
@Test
public void testList() throws Exception{
System.out.println(this.deptService.getAll());
}
@Test
public void testAdd() throws Exception{
Dept dept = new Dept();
dept.setDname("百度");
dept.setDeptno(100L);
System.out.println(this.deptService.add(dept));
}
}


8、如果在实际的工作之中,对于更新操作应该强制性的启动一个事物控制才对:
package com.gwolf.service;
import com.gwolf.vo.Dept;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface IDeptService {
@Transactional(readOnly = true)
public List<Dept> getAll();
@Transactional(propagation = Propagation.REQUIRED)
public boolean add(Dept dept);
}

9、此时应该明确的表示该操作方法应该启动一个事物的配置项。

10、在使用spring+mybatis里面处理的时候应该考虑到信息的显示问题,所以此处建议使用logback日志组件来进行日志信息的配置。将logback.xml配置文件拷贝到src/main/resouces之中;
在项目之中去引入logback的依赖配置文件。
在正常情况下mybatis中的日志信息的输出操作必须设置其对应的命名控制,在logback.xml中追加如下信息;
<logger name="com.gwolf.dao" level="trace" />