java springboot完全注解开发 通过Spring提供的PlatformTransactionManager与TransactionTemplate进行事务管理/执行

首先 要求你项目本身就有数据库连接配置 这里不限制用什么框架操作数据库 因为它是Spring提供的

这里 我用的是 mybatis

然后 我application.yml中这样写

spring:
  datasource:
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/test
   username: root
   password: root

这里 我们声明要链接的数据库是 mysql
地址本地数据库 下的 test数据库
然后 用户名和密码我都没设置过 都是 root

然后 我写了一个操作sql的函数
在这里插入图片描述
这里 我们函数 操作数据库下的 staff 表 接受 两个参数
第一个是 id 告诉它要更改哪一条数据 type 如果是 0 将age字段 加1 否则就 减1

然后 我们在 config 包下写一个DataSourceConfig配置类
参考代码如下

package com.example.textm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
}

这里 也要让他能连到数据库 因为事务的主要逻辑还得仰仗这个配置类

然后 我们再在config包下创建一个TransactionConfig配置类
参考代码如下

package com.example.textm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "com.example.textm")
public class TransactionConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

简单说 提供了一个 PlatformTransactionManager 的bean 我们需要通过它来实现事务
然后 这个basePackages 的路径要根据config / DataSourceConfig 的位置来 其实就是一个引导

然后 我们测试类编写代码如下

package com.example.textm;

import com.example.textm.dao.staffDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@SpringBootTest
class TextMApplicationTests {
    @Autowired
    private staffDao staffDao;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Test
    public void transferAmount() {
        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                int age = staffDao.professionallifespan(1, 0);
                int min = staffDao.professionallifespan(999, 1);
                if (age == 1 && min == 1) {
                    // 手动提交事务
                    transactionStatus.flush();
                } else {
                    // 回滚事务
                    transactionStatus.setRollbackOnly();
                }
            }
        });
    }
}

我们条件装配了 staffDao 操作sql的 接口 和 PlatformTransactionManager 控制对象

然后 我们开启事务 然后 我们通过staffDao调用我们写的 professionallifespan函数
刚才我们也看了 professionallifespan 返回一个int类型 1表示成功 0 表示失败
然后 我们 第一个传的 id 第二个type 分别表示 id为1的数据 type0 表示 id为1数据age加1
然后 第二个 id为 999的 type 1 id为 99的 age -1
在这里插入图片描述
但我们看这个数据库表 很明显 999的数据不存在 那么 我们就看 id为1的赵敏
按道理 第二个 999执行失败 那么 min 就是 0 if的条件就会不成立 那么就会走到transactionStatus.setRollbackOnly(); 回滚事务 那么 赵敏的age也不会改变 我们运行测试类代码
在这里插入图片描述
然后 我们刷新数据库表
在这里插入图片描述
可以看到 赵敏的age没有改变
那么 我们再来试个成功的
在这里插入图片描述
这里 我将第二条的 id 换成3
从数据库看 3是黄飞鸿 那么 我们就要看 他这个逻辑 简单理解为 黄飞鸿 age 加1 赵敏 age 加1
现在的数据是
在这里插入图片描述
我们右键运行测试类代码
在这里插入图片描述
然后 回头刷新数据库 会发现 我们成功了 他们都改变了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值