zoukankan      html  css  js  c++  java
  • SpringBoot整合Mybatis,并实现事务控制

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

  • 相关阅读:
    caffe-ubuntu1604-gtx850m-i7-4710hq----bvlc_reference_caffenet.caffemodel
    anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg
    caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
    caffe-ubuntu1604-gtx850m-i7-4710hq----bvlc_reference_caffenet.caffemodel
    matlab中文显示乱码:控制台上的,编辑器的,图片中的
    模式识别,计算机视觉领域,期刊
    matlab2017b linux版分享
    国内开源镜像站点汇总
    condarc文件
    conda清华源 pip 清华源ubuntu 清华镜像 R代理以及包的安装
  • 原文地址:https://www.cnblogs.com/telwanggs/p/10875538.html
Copyright © 2011-2022 走看看