mybatis plus QueryWrapper 动态表名
mybatis plus 版本 3.4.3!!
配置
@Configuration
public class MybatisPlusConfig {
private static ThreadLocal<String> table = new ThreadLocal<>();
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
HashMap<String, TableNameHandler> map = new HashMap<>();
map.put("t_user_account", (sql, tableName) -> table.get());
//多个
map.put("t_user", (sql, tableName) -> table.get());
dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return mybatisPlusInterceptor;
}
public static void setTable(String tableName){
table.set(tableName);
}
}
测试
@Test
public void queryTest() {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("id", 1);
MybatisPlusConfig.setTable("t_user_0");
SpreadAppQqClickModel model = userMapper.selectOne(queryWrapper);
System.out.println(JSON.toJSONString(model));
}
JDBC Connection will not be managed by Spring
==> Preparing: SELECT id,name FROM t_user_0 WHERE (id = ?)
==> Parameters: 1(Integer)
<== Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a402ba3]
可以发现 表名已经动态替换成 t_user_0
问题
这个从网上找的, 但是ThreadLocal 的使用会不会造成内存泄漏
的问题?