news 2026/9/16 19:17:11

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/9/14 17:15:34

鸿蒙PC上Electron原生应用开发:从零到部署的实战避坑指南

鸿蒙PC上Electron原生应用开发&#xff1a;从零到部署的实战避坑指南摘要&#xff1a;本文记录了笔者将Electron应用迁移至开源鸿蒙PC平台的全过程。通过真实项目案例&#xff0c;详解Electron与鸿蒙Native API的融合方案&#xff0c;涵盖环境搭建、窗口管理、剪贴板适配等核心…

作者头像 李华
网站建设 2026/9/12 17:13:17

鸿蒙PC开发指南:从零配置Qt环境到实战部署完整流程

鸿蒙PC开发指南&#xff1a;从零配置Qt环境到实战部署完整流程摘要&#xff1a;本文将以第一人称视角记录在开源鸿蒙PC平台上从零搭建Qt开发环境到实战部署的全过程。你将获得&#xff1a;鸿蒙PC开发环境配置的保姆级教程、Qt应用迁移的核心适配方案、解决跨平台兼容性问题的实…

作者头像 李华
网站建设 2026/9/15 15:46:26

二十三种设计模式(二十二)--策略模式

策略模式(Strategy) 如果某一个程序中用到各种各样的算法, 这些算法如果预编译的程序中会非常臃肿, 此时策略模式就可以实现各种算法的灵活取用 实际需求如下: 我有一个车载道路安全应用程序, 需要FDW道路前向预警功能, TSR道路标志牌识别功能, DMS驾驶员监控功能, 分别对应三种…

作者头像 李华
网站建设 2026/9/9 16:00:38

Java SpringBoot+Vue3+MyBatis 学科竞赛管理系统源码|前后端分离+MySQL数据库

摘要 随着信息技术的快速发展&#xff0c;学科竞赛作为高校人才培养的重要环节&#xff0c;其管理效率与信息化水平直接影响竞赛的公平性和参与度。传统的学科竞赛管理多依赖人工操作&#xff0c;存在报名流程繁琐、数据统计滞后、信息共享困难等问题。为解决这些问题&#xff…

作者头像 李华
网站建设 2026/9/14 5:50:28

大数据领域数据架构的发展趋势洞察

大数据领域数据架构的发展趋势洞察&#xff1a;从传统到新兴的演进之路 摘要/引言 在当今数字化时代&#xff0c;大数据的规模和复杂性呈指数级增长&#xff0c;对数据架构提出了前所未有的挑战。传统的数据架构在应对海量、高维、实时性的数据时逐渐显得力不从心。本文旨在深…

作者头像 李华