MybatisPlus攔截器如何實(shí)現(xiàn)數(shù)據(jù)表分表
MybatisPlus攔截器實(shí)現(xiàn)數(shù)據(jù)表分表
很多項(xiàng)目都會(huì)存在一些數(shù)據(jù)量很大或者數(shù)據(jù)量增加很快的業(yè)務(wù)表,由于mysql的數(shù)據(jù)量達(dá)到一定量后會(huì)影響我們的查詢效率,為了避免該類問題發(fā)生,我們需要在項(xiàng)目前期設(shè)計(jì)的時(shí)候針對(duì)這兩類情況做一個(gè)分表的設(shè)計(jì)。
這里的分表指的是水平拆分(只是表名不同,其余字段都一致,主鍵id不允許重復(fù)),對(duì)某一個(gè)數(shù)字取模運(yùn)算做為拆分后表的后綴名,具體要分多少張表可通過自己實(shí)際的項(xiàng)目情況確定。
首先創(chuàng)建請(qǐng)求參數(shù)傳遞的一個(gè)輔助類
/** * 請(qǐng)求參數(shù)傳遞輔助類 */ public class RequestDataHelper { /** * 請(qǐng)求參數(shù)存取 */ private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>(); /** * 設(shè)置請(qǐng)求參數(shù) * * @param requestData 請(qǐng)求參數(shù) MAP 對(duì)象 */ public static void setRequestData (Map<String, Object> requestData) { REQUEST_DATA.set(requestData); } /** * 獲取請(qǐng)求參數(shù) * * @return 請(qǐng)求參數(shù) MAP 對(duì)象 */ public static Map<String, Object> getRequestData () { return REQUEST_DATA.get(); } }
該輔助類主要作用有:
- a.設(shè)置請(qǐng)求參數(shù)
- b.將請(qǐng)求參數(shù)存取到線程變量中
- c.獲取請(qǐng)求參數(shù)
簡單的說就是在涉及到需要分表的數(shù)據(jù)操作時(shí),將請(qǐng)求參數(shù)放入線程變量中。
然后在攔截器里面獲取這個(gè)參數(shù),做特定的處理,找到我們具體要操作的那張表。
這里的請(qǐng)求參數(shù)大家可以把它理解成跟我們分表后的表名產(chǎn)生關(guān)聯(lián)的數(shù)據(jù)。
這里面的線程變量是僅在當(dāng)前線程下可使用的數(shù)據(jù),與其他線程做隔離。
在本文中不做詳細(xì)解釋。
在涉及到分表的數(shù)據(jù)層操作前(Mybatisplus或者M(jìn)ybatis增刪改查數(shù)據(jù)前)
將請(qǐng)求參數(shù)放入線程變量
RequestDataHelper.setRequestData(Collections.singletonMap("studentId", param.getStudentId())); LambdaQueryWrapper<StudentRecordEntity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(StudentRecordEntity::getStudentId, param.getStudentId()); List<StudentRecordEntity> studentRecordEntityList = super.list(queryWrapper);
Mybatisplus 攔截器代碼
/** * Mybatis plus 攔截器 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //動(dòng)態(tài)表插件 DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> { // 獲取參數(shù)方法 if (tableName.equalsIgnoreCase("t_student_record")) { Map<String, Object> paramMap = RequestDataHelper.getRequestData(); long studentId= Long.parseLong(String.valueOf(paramMap.get("studentId"))); int mod = (int) (studentId% 16); return tableName + "_" + mod; } else { return tableName; } }); interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); return interceptor; }
這里面t_student_record表就是要拆分的表,如果有多個(gè),在if里面寫多個(gè)。
studentId就是調(diào)用的時(shí)候傳入線程變量的請(qǐng)求參數(shù),對(duì)它%16就是分了16張表,根據(jù)實(shí)際業(yè)務(wù)情況,需要分多少張就把%后面的數(shù)字改為多少。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- mybatis-plus配置攔截器實(shí)現(xiàn)sql完整打印的代碼設(shè)計(jì)
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的方法
- Mybatis-plus通過添加攔截器實(shí)現(xiàn)簡單數(shù)據(jù)權(quán)限
- MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的示例
- mybatis-plus 攔截器敏感字段加解密的實(shí)現(xiàn)
- MyBatis-Plus攔截器對(duì)敏感數(shù)據(jù)實(shí)現(xiàn)加密
- mybatis-plus攔截器、字段填充器、類型處理器、表名替換、SqlInjector(聯(lián)合主鍵處理)
- mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能
- Mybatis Plus 3.4.0分頁攔截器的用法小結(jié)
相關(guān)文章
Lost connection to MySQL server during query的解決
經(jīng)常在執(zhí)行sql語句時(shí),會(huì)發(fā)現(xiàn)這個(gè)問題,一般就是連接mysql數(shù)據(jù)庫不穩(wěn)定2008-06-06MySQL安裝出現(xiàn)The?configuration?for?MySQL?Server?8.0.28?has
這篇文章主要給大家介紹了MySQL安裝出現(xiàn)The?configuration?for?MySQL?Server?8.0.28?has?failed.?You?can...錯(cuò)誤的解決辦法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09MySQL 數(shù)據(jù)庫 like 語句通配符模糊查詢小結(jié)
這篇文章主要介紹了MySQL 數(shù)據(jù)庫 like 語句通配符模糊查詢小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10