• mybatis-plus 动态表名简易使用


    场景:由于有些表是分表的,需要给表名添加后缀才能正确地访问表,如sys_user_2024_01

    代码

    依赖版本

            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.4.3.4version>
                <scope>compilescope>
            dependency>
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-extensionartifactId>
                <version>3.4.3.4version>
                <scope>compilescope>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    mybatisplus配置类

    /**
     *  MybatisPlus 配置文件
     */
    @Configuration
    @MapperScan({"com.xxx.dataana.mapper"})
    public class MybatisPlusConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    
            //动态表名实现
            DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
            //可以传多个表名参数,指定哪些表使用TableNameSuffixHandler处理表名称
            dynamicTableNameInnerInterceptor.setTableNameHandler(new TableNameSuffixHandler("sys_user"));
            //以拦截器的方式处理表名称
            interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
    
            return interceptor;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    TableNameSuffixHandler

    /**
     * mybatis-plus 成动态表名控制器
     */
    public class TableNameSuffixHandler implements TableNameHandler {
        /**
         * 用于记录哪些表可以使用该动态表名处理器(即哪些表需要分表)
         */
        private final List<String> tableNames;
    
        /**
         * 构造函数,构造动态表名处理器的时候,传递tableNames参数
         * @param tableNames
         */
        public TableNameSuffixHandler(String... tableNames) {
            this.tableNames = Arrays.asList(tableNames);
        }
    
        /**
         * 每个请求线程维护一个suffix数据,避免多线程数据冲突。所以使用ThreadLocal
         */
        private static final ThreadLocal<String> TABLE_SUFFIX = new ThreadLocal<>();
    
        /**
         * 设置请求线程的表名后缀数据
         * @param tableNameSuffix
         */
        public static void setSuffix(String tableNameSuffix) {
            TABLE_SUFFIX.set(tableNameSuffix);
        }
    
        /**
         * 删除请求线程的表名后缀数据
         * 注: 使用完必须释放
         */
        public static void removeSuffix() {
            TABLE_SUFFIX.remove();
        }
    
        /**
         * 动态表名接口实现方法
         * @param sql
         * @param tableName
         * @return
         */
        @Override
        public String dynamicTableName(String sql, String tableName) {
            if (this.tableNames.contains(tableName)) {
                String suffix = TABLE_SUFFIX.get();
                if(suffix != null){
                    //表名增加后缀
                    return tableName + suffix;
                }
            }
            //表名原样返回
            return tableName;
        }
    }
    
    
    
    
    • 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    使用

        public ServiceResponse deleteRelaEventByRelaId(String anaVerId, String relaId) {
            LambdaUpdateWrapper<SysUser> luw = Wrappers.lambdaUpdate(SysUser.class)
                    .set(SysUser::getStatus, "0")
                    .set(SysUser::getUpdateTime, new Date())
                    .eq(SysUser::getId, relaId);
            TableNameSuffixHandler.setSuffix("_2024_01");
            this.update(luw);
            TableNameSuffixHandler.removeSuffix();
            return ServiceResponse.createSuccess();
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11



    参考:

  • 相关阅读:
    几种常见的垃圾回收器和垃圾回收算法
    【QT】界面布局-登陆窗口
    python 之计算矩阵乘法
    常用的函数-MySQL
    web开发
    大数据学习——Hadoop集群搭建
    替代STM32的GD32,替代KEIL的Eclipse配置---连载1
    vue+element纯手工完美模拟实现小米有品网站
    css 样式 使用变量
    ABB PU515A 3BSE032401R1 自动化控制模块
  • 原文地址:https://blog.csdn.net/withwindluo/article/details/138186160