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,声明事务就可以了,

SpringBoot整合Mybatis,并实现事务控制的更多相关文章

  1. SpringBoot整合Mybatis,多数据源,事务,支持java -jar 启动.

    用了一段时间SpringBoot,之前配置MYBATIS ,在打包WAR 放到tomcat下正常,但是WAR已经过时了,现在流行直接打包JAR 丢到DOCKER 里,无奈JAR 启动的时候MAPPER ...

  2. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  3. spring与mybatis集成和事务控制

    一个. 基本介绍 本文将使用spring整合mybatis, 并加入事务管理, 以此为记, 方便以后查阅. 二. 样例 1. 代码结构图: 2. 建表语句: DROP DATABASE test; C ...

  4. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  5. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  6. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  7. SpringBoot整合Mybatis之进门篇

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  8. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例

    1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...

  9. springboot整合mybatis(SSM开发环境搭建)

    0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...

随机推荐

  1. sqlserver2016 management tool v18

    安装完sql server 2016 sp1版本后再安装管理工具v18版本,启动管理工具,启动不起来,自动退出了,没有任何反应. 解决该问题方案: 找到Microsoft.VisualStudio.S ...

  2. 配置jupyter notebook网页浏览

    上一篇博文已经介绍安装了Anaconda3:https://www.cnblogs.com/hello-wei/p/10233192.html jupyter notebook [I 11:33:11 ...

  3. mysql 5.7.24 root密码重置

    sudo mysql -u root -p 初始root密码没有,直接按回车 show databases: use mysql; update user set authentication_str ...

  4. 第十篇.1、python并发编程之多进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  5. Java并发(基础知识)—— Executor框架及线程池

    在Java并发(基础知识)—— 创建.运行以及停止一个线程中讲解了两种创建线程的方式:直接继承Thread类以及实现Runnable接口并赋给Thread,这两种创建线程的方式在线程比较少的时候是没有 ...

  6. 关于cli打包至服务器出现的BUG(样式错乱,路径出错)解决方案

    很久没来博客园了,今天给大家带来两个硬货bug,前端大牛可能不觉得是啥,但是对于没碰到过这问题的小菜鸟我来说还是很不错的 1.npm run build 至服务端的时候出现路径报错解决方案 ①.本地测 ...

  7. .htaccess防盗链方法(文件、图片)

    http标准协议中有专门的字段记录referer,一来可以追溯上一个入站地址是什么,二来对于资源文件,可以跟踪到包含显示他的网页地址是什么. 因此所有防盗链方法都是基于这个Referer字段两种方法: ...

  8. Python服务器开发二:Python网络基础

    Python服务器开发二:Python网络基础   网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. HTTP是高层协议,而TCP/IP是个协议集,包过许多的子协议.包括: ...

  9. Acwing-278-数字组合(背包)

    链接: https://www.acwing.com/problem/content/280/ 题意: 给定N个正整数A1,A2,-,AN,从中选出若干个数,使它们的和为M,求有多少种选择方案. 思路 ...

  10. Python CGI编程Ⅴ

    通过CGI程序传递 Textarea 数据 Textarea 向服务器传递多行数据,HTML代码如下: textarea.py 脚本代https://www.xuanhe.net/码如下: 修改 te ...