java通過(guò)注解實(shí)現(xiàn)分表詳解
寫(xiě)在前面
在業(yè)務(wù)開(kāi)發(fā)中,需要根據(jù)不同的渠道存儲(chǔ)產(chǎn)品銷(xiāo)售信息,由于單個(gè)渠道數(shù)據(jù)量比較大,放在一個(gè)表中存儲(chǔ)不合適,需要針對(duì)每個(gè)渠道單獨(dú)存儲(chǔ)。
代碼實(shí)現(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表達(dá)式解析.
*/
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";
/**
* 以注解為切點(diǎn)
*/
@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的表達(dá)式上下文對(duì)象
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í)是在代碼邏輯中設(shè)置了上下面,切面邏輯處理完了之后,再把上下文內(nèi)容去除。目的是為了給mybatis攔截器使用當(dāng)前請(qǐng)求的上下文內(nèi)容。
攔截器內(nèi)部利用的是是mubatis動(dòng)態(tài)表名,內(nèi)容如下:
@Configuration
@Slf4j
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 添加分頁(yè)插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
// 動(dòng)態(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{
// 組裝動(dòng)態(tài)表名
return tableName + "_" + tableSuffix;
}
}
}
return tableName;
});
mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return mybatisPlusInterceptor;
}
}
到此這篇關(guān)于java通過(guò)注解實(shí)現(xiàn)分表詳解的文章就介紹到這了,更多相關(guān)java分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程run方法中直接調(diào)用service業(yè)務(wù)類(lèi)應(yīng)注意的問(wèn)題及解決
這篇文章主要介紹了Java多線程run方法中直接調(diào)用service業(yè)務(wù)類(lèi)應(yīng)注意的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼
這篇文章主要介紹了java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明詳解
這篇文章主要為大家詳細(xì)介紹了servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù)
RedisTemplate是Spring Data Redis提供的一個(gè)用于操作Redis的模板類(lèi),本文主要介紹了java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
Eclipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)
下面小編就為大家?guī)?lái)一篇Eclipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解)
下面小編就為大家分享一篇基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
關(guān)于Controller層和Service層的類(lèi)報(bào)錯(cuò)問(wèn)題及解決方案
這篇文章主要介紹了關(guān)于Controller層和Service層的類(lèi)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

