java通過注解實現(xiàn)分表詳解
寫在前面
在業(yè)務(wù)開發(fā)中,需要根據(jù)不同的渠道存儲產(chǎn)品銷售信息,由于單個渠道數(shù)據(jù)量比較大,放在一個表中存儲不合適,需要針對每個渠道單獨存儲。
代碼實現(xiàn)
定義注解和切面
定義注解
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface DynamicTable { /** * 需要分割的表名 * @return */ String tableName(); /** * 后綴key * @return */ String separateKey(); }
切面處理邏輯
@Aspect @Component @Slf4j public class DynamicTableAspect { /** * 用于SpEL表達式解析. */ private SpelExpressionParser parser = new SpelExpressionParser(); /** * 用于獲取方法參數(shù)定義名字. */ private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); private static final String TABLE_NAME = "tableName"; private static final String TABLE_SUFFIX = "tableSuffix"; /** * 以注解為切點 */ @Pointcut("@annotation(com.leoli04.DynamicTable)") public void dynamicTable() { } @Around("dynamicTable()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature(); Method method = methodSignature.getMethod(); if(method.isAnnotationPresent(DynamicTable.class)){ DynamicTable dynamicTable = (DynamicTable) method.getAnnotation(DynamicTable.class); String key = dynamicTable.separateKey(); // 獲取參數(shù) Object[] args = joinPoint.getArgs(); Expression expression = parser.parseExpression(key); // 使用spring的DefaultParameterNameDiscoverer獲取方法形參名數(shù)組 String[] paramNames = nameDiscoverer.getParameterNames(method); // spring的表達式上下文對象 EvaluationContext context = new StandardEvaluationContext(); // 給上下文賦值 for (int i = 0; i < args.length; i++) { context.setVariable(paramNames[i], args[i]); } RequestDataHelper.setRequestData(new HashMap<String, Object>() {{ put(TABLE_NAME, dynamicTable.tableName()); put(TABLE_SUFFIX, expression.getValue(context)); }}); Object proceed = joinPoint.proceed(); RequestDataHelper.removeRequestData(); return proceed; }else{ Object proceed = joinPoint.proceed(); return proceed; } } }
mybatis攔截器
上面代碼在處理切面邏輯中有如下代碼:
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{ put(TABLE_NAME, dynamicTable.tableName()); put(TABLE_SUFFIX, expression.getValue(context)); }}); Object proceed = joinPoint.proceed(); RequestDataHelper.removeRequestData();
這段其實是在代碼邏輯中設(shè)置了上下面,切面邏輯處理完了之后,再把上下文內(nèi)容去除。目的是為了給mybatis攔截器使用當(dāng)前請求的上下文內(nèi)容。
攔截器內(nèi)部利用的是是mubatis動態(tài)表名,內(nèi)容如下:
@Configuration @Slf4j public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); // 添加分頁插件 mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 動態(tài)表名插件 DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> { // 獲取參數(shù)方法 Map<String, Object> paramMap = RequestDataHelper.getRequestData(); if (CollectionUtils.isNotEmpty(paramMap)) { var tableNameParam = (String) paramMap.get("tableName"); paramMap.forEach((k, v) -> log.info(k + "----" + v)); if(tableNameParam.equals(tableName)){ // 獲取傳遞的參數(shù) String tableSuffix = (String) paramMap.get("tableSuffix"); if(StringUtils.isBlank(tableSuffix)){ return tableName; }else{ // 組裝動態(tài)表名 return tableName + "_" + tableSuffix; } } } return tableName; }); mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); return mybatisPlusInterceptor; } }
到此這篇關(guān)于java通過注解實現(xiàn)分表詳解的文章就介紹到這了,更多相關(guān)java分表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用EasyExcel導(dǎo)入導(dǎo)出excel
導(dǎo)入導(dǎo)出excel數(shù)據(jù)是常見的需求,今天就來看一下Java基于EasyExcel實現(xiàn)這個功能,感興趣的朋友可以了解下2021-05-05Spring context:component-scan的使用及說明
這篇文章主要介紹了Spring context:component-scan的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09關(guān)于springboot 配置date字段返回時間戳的問題
這篇文章主要介紹了springboot 配置date字段返回時間戳的問題,在springboot2.0后,spring會將Date字段自動給轉(zhuǎn)成UTC字符串了(在沒有配置的情況下),所以date需要轉(zhuǎn)換成時間戳還是yyyy-MM-dd HH:mm:ss,具體解決方法跟隨小編一起看看吧2021-07-07微服務(wù)分布式架構(gòu)實現(xiàn)日志鏈路跟蹤的方法
在現(xiàn)有的系統(tǒng)中,由于大量的其他用戶/其他線程的日志也一起輸出穿行其中導(dǎo)致很難篩選出指定請求的全部相關(guān)日志。那我們?nèi)绾蝸硖幚砟??帶著這個問題一起通過本文學(xué)習(xí)下吧2021-08-08JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能的實現(xiàn)代碼
這篇文章主要介紹了JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12