动态表名插件的底层是 拦截器
1,创建一个拦截器
@Configuration
public class MybatisConfiguration {@Beanpublic DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {// 准备一个Map,用于存储TableNameHandlerMap<String, TableNameHandler> map = new HashMap<>(1);// 存入一个TableNameHandler,用来替换points_board表名称// 替换方式,就是从TableInfoContext中读取保存好的动态表名map.put("points_board", (sql, tableName) -> TableInfoContext.getInfo() == null ? tableName : TableInfoContext.getInfo());return new DynamicTableNameInnerInterceptor(map);}
}
拦截器创建好了并不会生效,还需要配置到mybatisPlus的配置中:
@Configuration
@ConditionalOnClass({MybatisPlusInterceptor.class, BaseMapper.class})
public class MybatisConfig {/*** @deprecated 存在任务更新数据导致updater写入0或null的问题,暂时废弃* @see MyBatisAutoFillInterceptor 通过自定义拦截器来实现自动注入creater和updater*/// @Bean// @ConditionalOnMissingBeanpublic BaseMetaObjectHandler baseMetaObjectHandler(){return new BaseMetaObjectHandler();}//配置mybatis plus 的拦截器链//DynamicTableNameInnerInterceptor 不是所用服务都用到 ,目前只有tj-learning服务用到,// 因此加上@Autowared(required = false)注解 注入时声明非必须@Bean@ConditionalOnMissingBeanpublic MybatisPlusInterceptor mybatisPlusInterceptor(@Autowired(required = false) DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor) {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();if (dynamicTableNameInnerInterceptor != null) {interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);//动态表名拦截器插件}PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);paginationInnerInterceptor.setMaxLimit(200L);interceptor.addInnerInterceptor(paginationInnerInterceptor);//分页拦截器插件interceptor.addInnerInterceptor(new MyBatisAutoFillInterceptor());//自动填充拦截器插件return interceptor;}
}
该如何传递表名称呢?
从计算表名,到动态表名插件执行,调用TableNameHandler,都是在一个线程内完成的。要在一个线程内实现数据共享,该用什么呢?
就是ThreadLocal.
我们可以在定时任务中计算完动态表名后,将表名存入ThreadLocal,然后在插件中从ThreadLocal中读取即可:
定义一个传递表名称的工具,具体代码如下:
package com.tianji.learning.utils;public class TableInfoContext {private static final ThreadLocal<String> TL = new ThreadLocal<>();public static void setInfo(String info) {TL.set(info);}public static String getInfo() {return TL.get();}public static void remove() {TL.remove();}
}