当前位置:   article > 正文

数据分表Mybatis Plus动态表名最优方案的探索_java mybits puls 新增时怎么拼分表名

java mybits puls 新增时怎么拼分表名
PRIMARY KEY (`id`)
  • 1

)

COMMENT=‘学生表

COLLATE=‘utf8_general_ci’

ENGINE=InnoDB

;




Student 实体类与student表是一一对应的关系,如果我们希望将学员表按照月份进行分表,比如:student\_202206、student\_202207、student\_202208,即产生了\*\*「一个实体类及其Mapper需要操作多个数据库分月表,这种情况在Mybatis plus下我们该如何操作数据呢?」\*\* 其实方法有很多,我将我实践中总结出的最优方案给大家进行说明。



[]( )二、动态表名处理器接口实现

----------------------------------------------------------------------



为了处理上述类似的问题,mybatis plus提供了动态表名处理器接口`TableNameHandler`,我们只需要实现这个接口,并将这个接口应用配置生效,即可实现动态表名。



> 需要注意的是:

> 

> *   在mybatis plus 3.4版本之前,动态表名处理器接口是`ITableNameHandler`, 需要配合mybatis plus分页插件一起使用才能生效。我们这里只介绍3.4版本之后的实现方式。

>     

> *   在mybatis plus 3.4.3.2 作废该的方式:dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map); 大家如果见到这种方式实现的动态表名,也是过时的实现方法,新版本中该方法已经删除。

>     



经过我一段时间的实践总结,我的实现类如下(基于mybatis plus 3.4.3.2之后的版本):



  • 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

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;

import java.util.Arrays;

import java.util.List;

/**

  • 按月份参数,组成动态表名

*/

public class MonthTableNameHandler implements TableNameHandler {

//用于记录哪些表可以使用该月份动态表名处理器(即哪些表按月分表)

private List<String> tableNames;

//构造函数,构造动态表名处理器的时候,传递tableNames参数

public MonthTableNameHandler(String ...tableNames) {

    this.tableNames = Arrays.asList(tableNames);

}



//每个请求线程维护一个month数据,避免多线程数据冲突。所以使用ThreadLocal

private static final ThreadLocal<String> MONTH_DATA = new ThreadLocal<>();

//设置请求线程的month数据

public static void setData(String month) {

    MONTH_DATA.set(month);

}

//删除当前请求线程的month数据

public static void removeData() {

    MONTH_DATA.remove();

}



//动态表名接口实现方法

@Override

public String dynamicTableName(String sql, String tableName) {

    if (this.tableNames.contains(tableName)){

        return tableName + "_" + MONTH_DATA.get();  //表名增加月份后缀

    }else{

        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

}




大家先对上面的代码有一个基础了解,看了下面的测试过程,再回头看上面的代码中的注释,就比较好理解了。表名处理器写好了之后,我们要让它生效,还需要做如下的配置。配置内容照葫芦画瓢就可以了。需要关注的部分,我都已经给大家添加了注释。



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@Configuration

@MapperScan(“com.zimug”)

public class MybatisPlusConfig {

@Bean

public MybatisPlusInterceptor mybatisPlusInterceptor() {

    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

    DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();

    dynamicTableNameInnerInterceptor.setTableNameHandler(

            //可以传多个表名参数,指定哪些表使用MonthTableNameHandler处理表名称

            new MonthTableNameHandler("student","teacher") 

    );

    //以拦截器的方式处理表名称

    interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

    //可以传递多个拦截器,即:可以传递多个表名处理器TableNameHandler

    //interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

    return interceptor;

}
  • 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

}




[]( )三、测试实现效果

-----------------------------------------------------------------



首先创建一个StudentMapper ,默认情况下StudentMapper 只能操作student表,不能操作student\_YYYYMM表。



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

最后

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行


首先创建一个StudentMapper ,默认情况下StudentMapper 只能操作student表,不能操作student_YYYYMM表。




# 最后

[外链图片转存中...(img-rkT5SYoL-1714720006109)]

[外链图片转存中...(img-qhMgvwtK-1714720006110)]

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/topics/618154847)收录**
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/830398
推荐阅读
相关标签
  

闽ICP备14008679号