当前位置: 首页 > news >正文

springBoot集成声明式和编程式事务的方式

一、声明式事务

前提集成了mybatisplus插件

1、pom依赖

<dependencies>
    <!-- MyBatis-Plus 启动器 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.3.4</version>
    </dependency>

    <!-- PostgreSQL 驱动 -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Spring Boot JDBC 起步依赖(内含事务管理器的自动配置) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <!-- 其他依赖 -->
</dependencies>

2、数据源配置

# PostgreSQL 数据源配置
spring.datasource.url=jdbc:postgresql://localhost:5432/your_database
spring.datasource.username=postgres
spring.datasource.password=your_password
spring.datasource.driver-class-name=org.postgresql.Driver

# MyBatis-Plus 配置(可选,根据需要调整)
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3、启动事务管理

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.example.mapper")  // 扫描你的 Mapper 包路径
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

@MapperScan 不是强制性的,如果你在每个 Mapper 接口上都加了 @Mapper 注解,Spring Boot 也能扫描到它们。不过,使用 @MapperScan 更为集中管理和配置扫描包是比较推荐的做法。 

5、手动配置事务管理器(可选)

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
public class TransactionManagerConfig {

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

测试参考:测试参考代码

6、报错解决

1)循环依赖问题

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sysUserService': Bean with name 'sysUserService' has been injected into other beans [sysRoleService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.

原因:这个错误主要是由于在启用事务管理后,Spring 会对带有事务注解的 Bean 进行代理包装,而你的业务中存在循环依赖问题,导致 sysUserService 在被注入到 sysRoleService 时,注入的是原始(raw)的 Bean,而后期又被包装成代理 Bean,从而引发了循环依赖错误。

即:当启用 @EnableTransactionManagement 后,带有 @Transactional 注解的 Bean 会被代理,如果在创建过程中遇到循环依赖,Spring 无法正确处理代理后的 Bean,导致最终注入的 Bean 与预期不符。

解决方案:重构代码取消循环依赖  或者使用懒加载 方式解决。

@Service
public class SysRoleService {

    @Autowired
    @Lazy
    private SysUserService sysUserService;

    // 其他方法...
}

 

2)

7、

8、

二、编程式事务

1、

2、

3、

持续更新中.......

相关文章:

  • Python基于Django的图书馆管理系统【附源码、文档说明】
  • RangeError: Invalid array length
  • linux0.11内核源码修仙传第五章——内存初始化(主存与缓存)
  • 高颜值多端适用软件:兼具屏保功能,PC 端登录可用
  • 数据结构(队列)
  • DeepSeek-R1本地化部署(Mac)
  • 【原创】springboot+vue实验室预约管理系统设计与实现
  • 增删改查 数据下载 一键编辑 删除
  • SpringSecurity认证授权完整流程
  • 城市霓虹灯夜景拍照后期Lr调色教程,手机滤镜PS+Lightroom预设下载!
  • instr,locate是否使用索引实验
  • DeepSeek【部署 03】客户端应用ChatBox、AnythingLLM及OpenWebUI部署使用详细步骤
  • 并发编程——累加器
  • 【华三】STP端口角色与状态深度解析
  • 仿函数 greater less
  • C++关键字:typename 用于依赖名消歧器(disambiguator)
  • C++进阶知识7 封装map和set
  • 分享能在线运行C语言的网站
  • Bitbucket SSH 访问设置指南
  • Vue3实战学习(Vue环境配置、快速上手及卸载、下载安装Node.js超详细教程(2025)、npm配置淘宝镜像)(1)
  • 做网站的技术要求/网站seo优化步骤
  • 网站是否wordpress/餐饮培训
  • 门户网站开发文档/广州seo黑帽培训
  • 手机wap网站模板下载/营销型网站制作建设
  • 一键网站模块/竞价恶意点击器
  • 买一款app要多少钱/seo及网络推广招聘