SpringBoot整合Mybatis,並實現事務控制
1、 在pom文件里添加相關maven文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、 在application.properties配置文件中引入數據源,創建數據庫表,並插入兩條原始數據:andy 余額200元,lucy 余額300元
1 spring.datasource.url=jdbc:mysql://localhost:3306/springboot_demo 2 spring.datasource.username=root 3 spring.datasource.password=root 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
DROP TABLE IF EXISTS tbl_account; CREATE TABLE tbl_account ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL, balance float, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; insert into tbl_account(id,name,balance) values(1, 'andy','200'); insert into tbl_account(id,name,balance) values(2, 'lucy','300');
數據庫初始值如下:

3、 開發實體類,dao,service,controller,mapper等
實體類 :
public class Account { private int id; private String name; private float balance; public Account() { } // 省略setter / getter }
dao :
public interface AccountDao { public void moveIn(@Param("id") int id, @Param("money") float money); // 轉入 public void moveOut(@Param("id") int id, @Param("money") float money); // 轉出 }
service :
1 public interface AccountService { 2 //轉賬 3 public void transfer(int outter,int inner,Integer money); 4 5 }
service 實現類:
1 @Service 2 public class AccountServiceImpl implements AccountService{ 3 4 @Autowired 5 private AccountDao accountDao; 6 7 public void transfer(int outter, int inner, Integer money) { 8 9 accountDao.moveOut(outter, money); //轉出 10 accountDao.moveIn(inner, money); //轉入 11 12 } 13 }
controller:
1 @RestController 2 @RequestMapping(value = "/account") 3 public class AccountController { 4 5 @Autowired 6 private AccountService accountService; 7 8 9 @RequestMapping("/transfer") 10 public String test(){ 11 try { 12 // andy 給lucy轉賬50元 13 accountService.transfer(1, 2, 50); 14 return "轉賬成功"; 15 } catch (Exception e) { 16 e.printStackTrace(); 17 return "轉賬失敗"; 18 } 19 } 20 }
mapper:
<?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="cn.yideng.tx.dao.AccountDao">
<!-- 轉入 -->
<update id="moveIn" >
update tbl_account
set balance = balance + #{money }
where id= #{id,jdbcType=INTEGER}
</update>
<!-- 轉出 -->
<update id="moveOut" >
update tbl_account
set balance = balance - #{money }
where id= #{id,jdbcType=INTEGER}
</update>
</mapper>
4、 在application.properties配置文件中添加對mapper文件的掃描
1 mybatis.typeAliasesPackage: cn.yideng.*.entity 2 mybatis.mapperLocations: classpath:mapper/*.xml
5、 在啟動類中添加對mapper包掃描@MapperScan
1 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 2 @EnableAutoConfiguration 3 @MapperScan("cn.yideng.*.dao") 4 public class DemoApplication { 5 6 public static void main(String[] args) { 7 SpringApplication.run(DemoApplication.class, args); 8 } 9 }
6、 瀏覽器測試 http://localhost:8080/account/transfer , 測試顯示 轉賬成功,看看數據庫的數據,andy余額是150, lucy余額350,都是對的,如下圖所示。

7,接着我們修改service,在實現類里,轉出之后拋個異常,代碼如下
1 @Service 2 public class AccountServiceImpl implements AccountService{ 3 4 @Autowired 5 private AccountDao accountDao; 6 7 public void transfer(int outter, int inner, Integer money) { 8 9 accountDao.moveOut(outter, money); //轉出 10 int i = 1/0; // 拋出異常 11 accountDao.moveIn(inner, money); //轉入 12 13 } 14 }
8,把數據庫的數據恢復成最初的 andy-200, lucy-300, 然后啟動類測試,瀏覽器輸入 http://localhost:8080/account/transfer , 測試顯示 轉賬失敗,看看數據庫的數據,andy余額是150, lucy余額300,如下圖所示。

相當於轉出成功,轉入沒有成功,這是不對的,應該都成功,或者都不成功。
9, 我們接着在service實現類上添加@Transactional 注解,聲明一個事務,如下
1 @Service 2 public class AccountServiceImpl implements AccountService{ 3 4 @Autowired 5 private AccountDao accountDao; 6 7 @Transactional 8 public void transfer(int outter, int inner, Integer money) { 9 10 accountDao.moveOut(outter, money); //轉出 11 int i = 1/0; // 拋出異常 12 accountDao.moveIn(inner, money); //轉入 13 14 } 15 }
10,再把數據庫的數據恢復成最初的 andy-200, lucy-300, 然后啟動類測試,瀏覽器輸入 http://localhost:8080/account/transfer , 測試顯示 轉賬失敗,看看數據庫的數據,andy余額是200, lucy余額300,如下圖所示。

說明轉出和轉入都沒有成功,這才是合乎邏輯的。
springboot 開啟事物很簡單,只需要加注解@Transactional 和 @EnableAutoConfiguration,聲明事務就可以了,
