mybatis-plus实现动态配置实体类表名
mybatis-plus配置文件
在MybatisPlusInterceptor下添加DynamicTableNameInnerInterceptor
@Configuration
@MapperScan(value = {"com.eternity.scrapy.modules.**.mapper*"})
public class MybatisPlusConfig {
private static ThreadLocal<String> table = new ThreadLocal<>();
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//动态设置表名
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2) {{
put("实体类默认设置的表名,按照自己需求修改", (sql, tableName) -> {
return table.get();
});
}};
dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
}
public static void setDynamicTableName(String tableName) {
table.set(tableName);
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}