MyBatis-Plus動(dòng)態(tài)表名的使用
MyBatis-Plus實(shí)現(xiàn)動(dòng)態(tài)表名
MyBatis實(shí)現(xiàn)方法如下現(xiàn)在要用MyBatis-Plus 實(shí)現(xiàn)
<select id="getList" resultType="com.wys.entity.User">
SELECT *
FROM ${tableName}
</select>
MyBatis-Plus官網(wǎng)說明

MyBatis-Plus版本
1、添加MyBatis-Plus依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
MyBatis-Plus配置
2、添加MyBatis-Plus配置,利用攔截器獲取到表名給替換
@Configuration
public class MybatisPlusConfig {
static List<String> tableList(){
List<String> tables = new ArrayList<>();
//偽表名 可以為任意字符串 建議設(shè)置復(fù)雜度 避免重復(fù) tables.add("C55EA8171877E962E08DFF63AA3678841");
tables.add("TestUser");
return tables;
}
//攔截器,獲取到表名給替換
@Bean
public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
String newTable = null;
for (String table : tableList()) {
newTable = RequestDataHelper.getRequestData(table);
if (table.equals(tableName) && newTable!=null){
tableName = newTable;
break;
}
}
return tableName;
});
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
}
}
如果上面的攔截器不生效可以使用下面這個(gè)http://www.dbjr.com.cn/article/280321.htm
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisPlusConfig {
static List<String> tableList(){
List<String> tables = new ArrayList<>();
//表名
tables.add("C55EA8171877E962E08DFF63AA3678841");
return tables;
}
//攔截器,獲取到表名給替換
// @Bean
// public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
// dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
// String newTable = null;
// for (String table : tableList()) {
// newTable = RequestDataHelper.getRequestData(table);
// if (table.equals(tableName) && newTable!=null){
// tableName = newTable;
// break;
// }
// }
// return tableName;
// });
// interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
// return interceptor;
// }
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addMyInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
String newTable = null;
for (String table : tableList()) {
newTable = RequestDataHelper.getRequestData(table);
if (table.equals(tableName) && newTable!=null){
tableName = newTable;
break;
}
}
return tableName;
});
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
請求參數(shù)傳遞輔助類
3、創(chuàng)建請求參數(shù)傳遞輔助類
public class RequestDataHelper {
/**
* 請求參數(shù)存取
*/
private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>();
/**
* 設(shè)置請求參數(shù)
*
* @param requestData 請求參數(shù) MAP 對象
*/
public static void setRequestData(Map<String, Object> requestData) {
REQUEST_DATA.set(requestData);
}
/**
* 獲取請求參數(shù)
*
* @param param 請求參數(shù)
* @return 請求參數(shù) MAP 對象
*/
public static <T> T getRequestData(String param) {
Map<String, Object> dataMap = getRequestData();
if (CollectionUtils.isNotEmpty(dataMap)) {
return (T) dataMap.get(param);
}
return null;
}
/**
* 獲取請求參數(shù)
*
* @return 請求參數(shù) MAP 對象
*/
public static Map<String, Object> getRequestData() {
return REQUEST_DATA.get();
}
}
使用

4、在程序中使用,注意如果實(shí)際表名與實(shí)體類與不同,可先在實(shí)體類類注明表名@TableName(“TestUser”)
@GetMapping("/listUser")
public void listUser(){
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("kfafkasfaskfasjfkasf", "user_2018");
}});
Integer age=2018;
User user=new User();
List list = userMapper.getList(user);
// User user_2019 = userMapper.findById("user_2019", 2019);
System.out.println(list);
System.out.println("-------------");
// System.out.println(user_2019);
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("kfafkasfaskfasjfkasf", "user_2019");
}});
List lis2 = userMapper.getList(user);
System.out.println(lis2);
System.out.println("-------------");
}

結(jié)果:

到此這篇關(guān)于MyBatis-Plus動(dòng)態(tài)表名的使用的文章就介紹到這了,更多相關(guān)MyBatis-Plus動(dòng)態(tài)表名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Plus動(dòng)態(tài)表名使用selectPage方法不生效問題解析與解決方案
- 解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問題
- MyBatis-Plus中如何實(shí)現(xiàn)動(dòng)態(tài)表名
- mybatis-plus動(dòng)態(tài)表名實(shí)現(xiàn)方法
- mybatis-plus動(dòng)態(tài)表名的實(shí)現(xiàn)示例
- MyBatis-Plus 動(dòng)態(tài)表名SQL解析器的實(shí)現(xiàn)
- Mybatis-Plus動(dòng)態(tài)表名的實(shí)現(xiàn)示例
相關(guān)文章
spring boot加載資源路徑配置和classpath問題解決
這篇文章主要介紹了spring boot加載資源路徑配置和classpath問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
JAVA正則表達(dá)式過濾文件的實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA正則表達(dá)式過濾文件的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠掌握理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09
SpringBoot中支持Https協(xié)議的實(shí)現(xiàn)
本文主要介紹了SpringBoot中支持Https協(xié)議的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
java日期格式化YYYY-MM-dd遇坑指南小結(jié)
本文主要介紹了java日期格式化YYYY-MM-dd遇坑指南小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡單的鏈路追蹤功能
Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡單的鏈路追蹤功能,需要的朋友可以參考下2019-10-10
深入學(xué)習(xí)SpringCloud之SpringCloud簡介
Spring Cloud是一個(gè)一站式的開發(fā)分布式系統(tǒng)的框架,為開發(fā)者提供了一系列的構(gòu)建分布式系統(tǒng)的工具集,本文給大家介紹springcloud的相關(guān)知識,感興趣的朋友跟隨一起看看吧2021-04-04
Java lambda list轉(zhuǎn)換map時(shí),把多個(gè)參數(shù)拼接作為key操作

