news 2026/4/18 12:24:51

springboot事务触发滚动与不滚蛋

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot事务触发滚动与不滚蛋

事务触发滚动与不滚蛋

代码:

MyBatisConfig 类

package org.example.testproduct; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.context.annotation.Bean; 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 public class MyBatisConfig { private final DataSource dataSource; public MyBatisConfig(DataSource dataSource) { this.dataSource = dataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { System.out.println("dataSource1 = " + dataSource); // 使用MyBatis-Plus提供的SqlSessionFactoryBean MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); // 移除对XML映射文件的依赖,MyBatis-Plus可以自动生成SQL return factoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { System.out.println("new DataSourceTransactionManager()"+new DataSourceTransactionManager(dataSource)); return new DataSourceTransactionManager(dataSource); } }

Order 类

package org.example.testproduct.order; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("orders") public class Order { private Long id; private String orderNo; private Integer amount; }
OrderController类
package org.example.testproduct.order; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Autowired private OrderService orderService; // 访问这个接口,事务会回滚 @GetMapping("/order/tx") public String orderWithTx() { try { orderService.createOrderWithTransaction(); } catch (Exception e) { return "Exception caught, transaction should rollback: " + e.getMessage(); } // orderService.createOrderWithTransaction(); return "ok"; } // 访问这个接口,事务不会回滚 @GetMapping("/order/notx") public String orderWithoutTx() { try { orderService.createOrderWithoutTransaction(); } catch (Exception e) { return "Exception caught, transaction will NOT rollback: " + e.getMessage(); } return "ok"; } }

OrderMapper 接口

package org.example.testproduct.order; import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface OrderMapper extends BaseMapper<Order> { }

OrderService 类

package org.example.testproduct.order; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class OrderService { @Autowired private OrderMapper orderMapper; // 这个方法有事务 @Transactional public void createOrderWithTransaction() { Order order = new Order(); order.setOrderNo("ORD-TX-" + System.currentTimeMillis()); order.setAmount(100); orderMapper.insert(order); // 制造异常 int x = 1 / 0; } // 这个方法没有事务 public void createOrderWithoutTransaction() { Order order = new Order(); order.setOrderNo("ORD-NO-" + System.currentTimeMillis()); order.setAmount(200); orderMapper.insert(order); // 制造异常 int x = 1 / 0; } }

依赖:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>4.0.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>testproduct</artifactId> <version>0.0.1-SNAPSHOT</version> <name>testproduct</name> <description>testproduct</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- MySQL JDBC 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-spring-boot3-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

运行效果:

用apifox插件检查接口。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 5:40:47

1. 数据存储结构

一、核心原理 1. 数据存储结构 // 每个 Thread 对象内部都有一个 ThreadLocalMap ThreadLocal.ThreadLocalMap threadLocals null; // ThreadLocalMap 内部使用 Entry 数组&#xff0c;Entry 继承自 WeakReference<ThreadLocal<?>> static class Entry extends…

作者头像 李华
网站建设 2026/4/18 5:35:46

三阶幻方了解-七年级上册

目录 一、三阶幻方的历史1. 起源&#xff1a;洛书&#xff08;中国上古时期&#xff09;2. 中国古代的发展3. 向世界的传播4. 现代价值 二、 现代数学中三阶幻方有哪些应用&#xff1f;1. 组合数学与离散数学2. 密码学与信息编码3. 实验设计与统计学4. 教育与思维训练5. 人工智…

作者头像 李华
网站建设 2026/4/18 1:08:41

从底层逻辑到工程实践,深入理解C语言在计算机世界中的核心地位与持久价值

在计算机语言百花齐放的今天&#xff0c;C语言依然占据着一个极其特殊且稳固的位置。它不像某些新兴语言那样语法华丽、生态庞大&#xff0c;也不以“快速开发”“低门槛”为主要卖点&#xff0c;但它却深深嵌入了现代计算机体系的骨骼之中。从操作系统、嵌入式设备到高性能计算…

作者头像 李华
网站建设 2026/4/18 8:44:16

【JavaSE】多线程之安全使用容器

不出意外这是多线程的最后一篇文章&#xff0c;主要介绍的是面试中比较常考的一个点——多线程下使用容器&#xff0c;我们开始吧~我们知道&#xff0c;在单线程环境下ArrayList、HashMap等容器使用起来非常方便&#xff0c;但在多线程环境中&#xff0c;如果多个线程同时对容器…

作者头像 李华