MyBatis-Plus中如何實現(xiàn)動態(tài)表名
MyBatis-Plus實現(xiàn)動態(tài)表名
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<>();
? ? ? ? //表名
? ? ? ? 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;
? ? }
}請求參數(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、在程序中使用,注意如果實際表名與實體類與不同,可先在實體類類注明表名@TableName("TestUser")
@Test
public void test1() {
? ? RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
? ? ? ? put("TestUser", "TestUser_" + "2022_05");
? ? }});
?? ?//表名會被動態(tài)替換成"TestUser_2022_05"
?? ?//如果實際表名與實體類與不同,可先在實體類類注明表名@TableName("TestUser")
? ? List<UserEntity> users= userDao.selectList(null);
? ? System.out.println(users);
}數(shù)據(jù)分表Mybatis Plus動態(tài)表名最優(yōu)方案
應(yīng)用場景
大家在使用Mybatis進(jìn)行開發(fā)的時候,經(jīng)常會遇到一種情況:按照月份month將數(shù)據(jù)放在不同的表里面,查詢數(shù)據(jù)的時候需要跟不同的月份month去查詢不同的表。
但是我們都知道,Mybatis是ORM持久層框架,即:實體關(guān)系映射,實體Object與數(shù)據(jù)庫表之間是存在一一對應(yīng)的映射關(guān)系。比如:
@Data
public class Student {
private Integer id;
private String stuName;
private Integer age;
}表結(jié)構(gòu)
CREATE TABLE `student` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`stu_name` VARCHAR(64) NOT NULL DEFAULT '0' COMMENT '姓名',
`age` INT(11) NOT NULL COMMENT '年齡',
PRIMARY KEY (`id`)
)
COMMENT='學(xué)生表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;Student 實體類與student表是一一對應(yīng)的關(guān)系,如果我們希望將學(xué)員表按照月份進(jìn)行分表,比如:student_202206、student_202207、student_202208,即產(chǎn)生了**「一個實體類及其Mapper需要操作多個數(shù)據(jù)庫分月表,這種情況在Mybatis plus下我們該如何操作數(shù)據(jù)呢?」** 其實方法有很多,我將我實踐中總結(jié)出的最優(yōu)方案給大家進(jìn)行說明。
動態(tài)表名處理器接口實現(xiàn)
為了處理上述類似的問題,mybatis plus提供了動態(tài)表名處理器接口TableNameHandler,我們只需要實現(xiàn)這個接口,并將這個接口應(yīng)用配置生效,即可實現(xiàn)動態(tài)表名。
需要注意的是:
- 在mybatis plus 3.4版本之前,動態(tài)表名處理器接口是ITableNameHandler, 需要配合mybatis plus分頁插件一起使用才能生效。我們這里只介紹3.4版本之后的實現(xiàn)方式。
- 在mybatis plus 3.4.3.2 作廢該的方式:dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map); 大家如果見到這種方式實現(xiàn)的動態(tài)表名,也是過時的實現(xiàn)方法,新版本中該方法已經(jīng)刪除。
經(jīng)過我一段時間的實踐總結(jié),我的實現(xiàn)類如下(基于mybatis plus 3.4.3.2之后的版本):
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import java.util.Arrays;
import java.util.List;
/**
* 按月份參數(shù),組成動態(tài)表名
*/
public class MonthTableNameHandler implements TableNameHandler {
//用于記錄哪些表可以使用該月份動態(tài)表名處理器(即哪些表按月分表)
private List<String> tableNames;
//構(gòu)造函數(shù),構(gòu)造動態(tài)表名處理器的時候,傳遞tableNames參數(shù)
public MonthTableNameHandler(String ...tableNames) {
this.tableNames = Arrays.asList(tableNames);
}
//每個請求線程維護(hù)一個month數(shù)據(jù),避免多線程數(shù)據(jù)沖突。所以使用ThreadLocal
private static final ThreadLocal<String> MONTH_DATA = new ThreadLocal<>();
//設(shè)置請求線程的month數(shù)據(jù)
public static void setData(String month) {
MONTH_DATA.set(month);
}
//刪除當(dāng)前請求線程的month數(shù)據(jù)
public static void removeData() {
MONTH_DATA.remove();
}
//動態(tài)表名接口實現(xiàn)方法
@Override
public String dynamicTableName(String sql, String tableName) {
if (this.tableNames.contains(tableName)){
return tableName + "_" + MONTH_DATA.get(); //表名增加月份后綴
}else{
return tableName; //表名原樣返回
}
}
}大家先對上面的代碼有一個基礎(chǔ)了解,看了下面的測試過程,再回頭看上面的代碼中的注釋,就比較好理解了。表名處理器寫好了之后,我們要讓它生效,還需要做如下的配置。配置內(nèi)容照葫蘆畫瓢就可以了。需要關(guān)注的部分,我都已經(jīng)給大家添加了注釋。
@Configuration
@MapperScan("com.zimug")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler(
//可以傳多個表名參數(shù),指定哪些表使用MonthTableNameHandler處理表名稱
new MonthTableNameHandler("student","teacher")
);
//以攔截器的方式處理表名稱
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
//可以傳遞多個攔截器,即:可以傳遞多個表名處理器TableNameHandler
//interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
}
}測試實現(xiàn)效果
首先創(chuàng)建一個StudentMapper ,默認(rèn)情況下StudentMapper 只能操作student表,不能操作student_YYYYMM表。
@Mapper
public interface StudentMapper extends BaseMapper<Student> {}下面我們來寫一個單元測試用例,該測試用例test函數(shù)模擬一次請求訪問的Controller或者service函數(shù)。
@SpringBootTest
class DynamicTableNameTest {
@Resource
private StudentMapper studentMapper;
@Test
void test() {
//執(zhí)行數(shù)據(jù)操作之前設(shè)置月份(實際場景下該參數(shù)從請求參數(shù)中解析)
MonthTableNameHandler.setData("202208");
studentMapper.selectById(1); //以id=2查詢student_202208這張表
//閱后即焚,將ThreadLocal當(dāng)前請求線程的數(shù)據(jù)移除
MonthTableNameHandler.removeData();
}
}當(dāng)我們執(zhí)行這個單元測試用例的時候,我們發(fā)現(xiàn)控制臺打印出如下信息,注意看SQL的部分,真的是去查詢student_202208這張表了,而不是student表。這說明我們的動態(tài)表名實現(xiàn)是成功的。

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解springboot?springsecuroty中的注銷和權(quán)限控制問題
這篇文章主要介紹了springboot-springsecuroty?注銷和權(quán)限控制,賬戶注銷需要在SecurityConfig中加入開啟注銷功能的代碼,權(quán)限控制要導(dǎo)入springsecurity和thymeleaf的整合依賴,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-03-03
Spring Boot Starter 的應(yīng)用場景與自動配置方式
本文介紹了Spring Boot Starter的使用場景,如何自定義Starter以及Spring Boot自動配置原理,Spring Boot Starter解決了依賴導(dǎo)入和配置繁瑣的問題,通過自動配置類和xxxProperties類實現(xiàn)組件的自動注入和配置,感興趣的朋友一起看看吧2025-03-03
Java自定義協(xié)議報文封裝 添加Crc32校驗的實例
下面小編就為大家分享一篇Java自定義協(xié)議報文封裝 添加Crc32校驗的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
SpringBoot整合RocketMQ實現(xiàn)發(fā)送同步消息
RocketMQ 是一款開源的分布式消息中間件,由阿里巴巴開源,它具有高可用性、高性能、低延遲等特點,廣泛應(yīng)用于阿里巴巴集團(tuán)內(nèi)部以及眾多外部企業(yè)的業(yè)務(wù)系統(tǒng)中,本文給大家介紹了SpringBoot整合RocketMQ實現(xiàn)發(fā)送同步消息,需要的朋友可以參考下2024-04-04
Spring Boot使用Spring的異步線程池的實現(xiàn)
這篇文章主要介紹了Spring Boot使用Spring的異步線程池的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

