java springboot完全注解开发 通过Spring提供的PlatformTransactionManager与TransactionTemplate进行事务管理/执行
然后 我们 第一个传的 id第二个type分别表示id为1的数据 type0 表示 id为1数据age加1。然后 我们开启事务然后我们通过staffDao调用我们写的 professionallifespan函数。但我们看这个数据库表很明显999的数据不存在那么我们就看 id为1的赵敏。然后 第二个id为 999的type 1id为99的age -1。然后 我们在 config 包下写一个Data
首先 要求你项目本身就有数据库连接配置 这里不限制用什么框架操作数据库 因为它是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
现在的数据是
我们右键运行测试类代码
然后 回头刷新数据库 会发现 我们成功了 他们都改变了
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)