springboot篇】六. springboot中的事务控制

springboot中的事务控制

项目准备

  1. 创建day64-springboot-04-tx
  2. 配置【springboot篇】五. springboot与mybatis-plus整合
  3. 数据库用上一章的 day63-springboot-mybatisplus
    DROP TABLE IF EXISTS `account`;
    CREATE TABLE `account` (
      `id` int(8) NOT NULL AUTO_INCREMENT,
      `name` varchar(20) DEFAULT NULL,
      `account` double(8,2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    INSERT INTO `account` VALUES ('1', 'wpj', '1000.00');
    INSERT INTO `account` VALUES ('2', 'czh', '1000.00');
    
  4. 导依赖
    <dependency>
    	<groupId>com.baomidou</groupId>
    	<artifactId>mybatis-plus-boot-starter</artifactId>
    	<version>2.3</version>
    </dependency>
    <dependency>
    	<groupId>com.wpj</groupId>
    	<artifactId>base</artifactId>
    	<version>1.0-SNAPSHOT</version>
    </dependency>
    
  5. 修改mysql驱动依赖的版本
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<scope>runtime</scope>
    	<version>5.1.44</version>
    </dependency>
    
  6. 修改application.properties为.yml结尾
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/day63-springboot-mybatisplus
        username: root
        password: 123456
    
  7. 测试(报错不管)
    package com.wpj.day64springboot04tx;
    
    import com.baomidou.mybatisplus.annotations.*;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.*;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import javax.sql.DataSource;
    
    @SpringBootTest
    class Day64Springboot04TxApplicationTests {
    
    	@Autowired
    	private DataSource dataSource;
    	@Test
    	void contextLoads() {
    		System.out.println(dataSource);
    	}
    }
    

1 测试查询功能

1.1 定义实体类Account

package com.wpj.bean;

import com.baomidou.mybatisplus.annotations.*;
import com.baomidou.mybatisplus.enums.*;
import lombok.*;

/**
 * ClassName: Account
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\20 0020 16:36
 * @since JDK 1.8
 */
@Data
@TableName("account")
public class Account {
    
    @TableId(type= IdType.AUTO)
    private Integer id;
    
    private String name;
    
    private Double account;
    
}

1.2 定义service及其impl

public interface IAccountService extends IBaseService<Account> {
    
}
@Service
public class AccountServiceImpl extends BaseServiceImpl<Account> implements IAccountService {
    
    @Autowired
    private IAccountMapper iAccountMapper;
    
    @Override
    protected BaseMapper<Account> getBaseMapper() {
        return iAccountMapper;
    }
}

1.3 定义mapper

public interface IAccountMapper extends BaseMapper<Account> {
    
}

1.4 开启包扫描


@SpringBootApplication(scanBasePackages = "com.wpj")
@MapperScan(basePackages = "com.wpj.mapper")
public class Day64Springboot04TxApplication {

	public static void main(String[] args) {
		SpringApplication.run(Day64Springboot04TxApplication.class, args);
	}

}

1.5 yml配置别名

    
mybatis-plus:
  type-aliases-package: com.wpj.bean

1.6 测试

在这里插入图片描述

2 测试事务

2.1 service定义方法及impl重写方法

制造异常

public interface IAccountService extends IBaseService<Account> {

    void transfer(String outName, String inName, Double money);

}
@Service
public class AccountServiceImpl extends BaseServiceImpl<Account> implements IAccountService {

    @Autowired
    private IAccountMapper iAccountMapper;

    @Override
    protected BaseMapper<Account> getBaseMapper() {
        return iAccountMapper;
    }

    @Override
    public void transfer(String outName, String inName, Double money) {

        iAccountMapper.out(outName, money);
        int i = 1/0;
        iAccountMapper.in(inName, money);

    }
}

2.2 service定义2个方法

public interface IAccountMapper extends BaseMapper<Account> {

    void out(@Param("outName") String outName, @Param("money") Double money);

    void in(@Param("inName") String inName, @Param("money") Double money);
}

2.3 创建mapper接口对应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="com.wpj.mapper.IAccountMapper">

    <update id="out">
        update account set account = account - #{money} where name = #{outName}
    </update>
    <update id="in">
        update account set account = account + #{money} where name = #{inName}
    </update>

</mapper>

2.4 yml配置mapper映射

mybatis-plus:
  type-aliases-package: com.wpj.bean
  mapper-locations: classpath:mapper/*.xml

2.6 配置事务

@EnableTransactionManagement	//配置事务
@SpringBootApplication(scanBasePackages = "com.wpj")
@MapperScan(basePackages = "com.wpj.mapper")
public class Day64Springboot04TxApplication {

	public static void main(String[] args) {
		SpringApplication.run(Day64Springboot04TxApplication.class, args);
	}

2.6.1 配置事务

@Transactional	// 配置事务
@Override
public void transfer(String outName, String inName, Double money) {

    iAccountMapper.out(outName, money);
    int i = 1/0;
    iAccountMapper.in(inName, money);
    
}

2.7 测试

2.8 测试完成。。。。

发布了42 篇原创文章 · 获赞 8 · 访问量 2399

猜你喜欢

转载自blog.csdn.net/TheNew_One/article/details/104053312