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

SpringBoot集成Mybatis使用切面对所有Service的事务统一管理

SpringBoot集成Mybatis使用切面对所有Service的事务统一管理

package cn.com.baidu.aspect;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * CBC
 * 2023年12月2日00:53:35
 * 用于对数据源事务配置的初始化
 */
@Configuration
public class MyBatisTransaction {
    @Autowired
    private DataSource dataSource;

    /**
     * 获取事务管理器-修改信息
     * @return
     */
    @Bean("myBatisTransactionManager")
    public DataSourceTransactionManager myBatisTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 事务拦截器(设置以哪些开头的需要事务)
     */
    @Bean("myBatisTransactionAdvice")
    public TransactionInterceptor myBatisTransactionAdvice(DataSourceTransactionManager txManager) {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        /* 只读事务,不做更新操作*/
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        /*不支持事务,当前存在事务时,将挂起事务,运行方法*/
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
        /*写事务,需要更新操作 */
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requiredTx.setTimeout(5);
        Map<String, TransactionAttribute> txMap = new HashMap<>();
        txMap.put("save*", requiredTx);
        txMap.put("add*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("remove*", requiredTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("list*", readOnlyTx);
        txMap.put("query*", readOnlyTx);
        source.setNameMap(txMap);
        return new TransactionInterceptor(txManager, source);
    }

    /**
     * 切面拦截规则 参数会自动从容器中注入
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(TransactionInterceptor myBatisTransactionAdvice) {
        DefaultPointcutAdvisor pointcutAdvisor = new DefaultPointcutAdvisor();
        pointcutAdvisor.setAdvice(myBatisTransactionAdvice);
        AspectJExpressionPointcut pointCut = new AspectJExpressionPointcut();
        pointCut.setExpression("execution(* cn.com.baidu.service.*.*.*(..))");
        pointcutAdvisor.setPointcut(pointCut);
        return pointcutAdvisor;
    }
}


http://www.kler.cn/news/157197.html

相关文章:

  • xattr -r -d com.apple.quarantine是用于删除文件的扩展属性的命令
  • 五、分支和循环
  • JNPF低代码平台高效赋能开发者
  • Glide系列-活动缓存和内存缓存
  • 人工智能_机器学习060_核函数对应数学公式_数据空间错位分割_简单介绍_以及核函数总结---人工智能工作笔记0100
  • 兔子的后院奇遇:深入了解RabbitMQ中的死信队列【RabbitMQ 四】
  • 基于python电商销售数据可视化大屏全屏系统设计与实现+开题报告
  • pytorch bert实现文本分类
  • 前端开发_CSS
  • 大数据之HBase(二)
  • 《开箱元宇宙》:Madballs 解锁炫酷新境界,人物化身系列大卖
  • Linux基础操作一:连接Linux
  • 从顺序表中删除具有最小值的元素(假设唯一) 并由函数返回被删元素的值。空出的位 置由最后一个元素填补,若顺序表为空,则显示出错信息并退出运行。
  • 【shell】
  • 华为云购买参考:到底选购ECS还是CCE?
  • STL常用算法-C++
  • acwing1209.带分数暴力与优化(java版)
  • python pyaudio 录取语音数据
  • 【从零开始学习Redis | 第六篇】爆改Setnx实现分布式锁
  • Java 设计模式——备忘录模式
  • docker compose 搭建reids集群 1主2从架构
  • 【C语言】递归详解
  • 【powerjob】定时任务调度器 xxl-job和powerjob对比
  • SQL Sever 基础知识 - 数据筛选(2)
  • PCB走线要“尽量”短_笔记
  • 【数据库设计和SQL基础语法】--SQL语言概述--数据类型和约束
  • 基础堆溢出原理与DWORD SHOOT实现
  • MySQL笔记-第04章_运算符
  • Gson 自动生成适配器插件
  • cocos creator-碰撞检测