• mybatis-plus的多数据源sql拦截&动态表名


    问题描述

    1、我们使用的是mybatis-plus的多数据源链接,之前使用的mybatis-plus版本是3.3.1(版本低了,用不了)
    2、在网上找的诸多的sql拦截代码,发现断点进不去,找原因后发现由于配置的多个数据源,所以有多个SqlSessionFactory,只有其中的default中有我们添加的sql拦截器,我们最终的解决方案是遍历所有的SqlSessionFactory,删除其中的MybatisPlusInterceptor,再添加我们自定义的MybatisPlusInterceptor

    mybatis-plus版本依赖

            
            
                com.baomidou
                mybatis-plus-boot-starter
                3.4.3.2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    删除所有MybatisPlusInterceptor,再添加自定义的MybatisPlusInterceptor

    @Component
    @SuppressWarnings("SpringJavaAutowiredMembersInspection")
    public class InterceptorConfig implements ApplicationListener {
    
        @Autowired(required = false)
        private List sqlSessionFactoryList;
        @Autowired(required = false)
        private MybatisPlusInterceptor mybatisPlusInterceptor;
    
    
        @Override
        @SuppressWarnings("unchecked")
        public void onApplicationEvent(ApplicationReadyEvent event) {
            if (CollectionUtils.isNotEmpty(sqlSessionFactoryList) && Objects.nonNull(mybatisPlusInterceptor)) {
                try {
                    for (SqlSessionFactory factory : sqlSessionFactoryList) {
                        Field interceptorChain = Configuration.class.getDeclaredField("interceptorChain");
                        interceptorChain.setAccessible(true);
                        InterceptorChain chain = (InterceptorChain) interceptorChain.get(factory.getConfiguration());
                        Field interceptors = InterceptorChain.class.getDeclaredField("interceptors");
                        interceptors.setAccessible(true);
                        List list = (List) interceptors.get(chain);
                        if (CollectionUtils.isNotEmpty(list)) {
                            if (list.get(list.size() - 1) != mybatisPlusInterceptor) {
                                list.removeIf(i -> i == mybatisPlusInterceptor);
                                list.add(mybatisPlusInterceptor);
                            }
                        } else {
                            list.add(mybatisPlusInterceptor);
                        }
                    }
                } catch (Exception ignored) {
                    System.out.println("========================");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    添加自定义动态表名拦截器
    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    快速解析excel
    python+allure+jenkins
    【证明】线性变换的核是一个线性空间
    Java线程安全
    NOIP2023模拟13联测34 competition
    ROS1和ROS2的区别
    C/C++轻量级并发TCP服务器框架Zinx-游戏服务器开发002:框架学习-按照三层结构模式重构测试代码+Tcp数据适配+时间轮定时器
    阿里云云原生一体化数仓 — 分析服务一体化新能力解读
    什么是UDP、TCP,怎么用UDP和TCP实现网络通信和数据传输
    spring复习03,注解配置管理bean
  • 原文地址:https://blog.csdn.net/weixin_44348734/article/details/133035985