最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java通過注解實(shí)現(xiàn)分表詳解

 更新時(shí)間:2024年11月08日 08:22:20   作者:LeoLi_4  
這篇文章主要為大家詳細(xì)介紹了java如何通過注解實(shí)現(xiàn)分表,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下

寫在前面

在業(yè)務(wù)開發(fā)中,需要根據(jù)不同的渠道存儲(chǔ)產(chǎn)品銷售信息,由于單個(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();
        // 添加分頁插件
        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通過注解實(shí)現(xiàn)分表詳解的文章就介紹到這了,更多相關(guān)java分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring快速入門實(shí)例教程

    spring快速入門實(shí)例教程

    這篇文章主要介紹了spring快速入門實(shí)例,主要分析了spring的基本配置與控制反轉(zhuǎn),對(duì)于spring的學(xué)習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • Java多線程run方法中直接調(diào)用service業(yè)務(wù)類應(yīng)注意的問題及解決

    Java多線程run方法中直接調(diào)用service業(yè)務(wù)類應(yīng)注意的問題及解決

    這篇文章主要介紹了Java多線程run方法中直接調(diào)用service業(yè)務(wù)類應(yīng)注意的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    這篇文章主要介紹了java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • servlet實(shí)現(xiàn)文件下載的步驟及說明詳解

    servlet實(shí)現(xiàn)文件下載的步驟及說明詳解

    這篇文章主要為大家詳細(xì)介紹了servlet實(shí)現(xiàn)文件下載的步驟及說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù)

    java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù)

    RedisTemplate是Spring Data Redis提供的一個(gè)用于操作Redis的模板類,本文主要介紹了java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • Eclipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)

    Eclipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)

    下面小編就為大家?guī)硪黄狤clipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 詳解Java中自定義注解的使用

    詳解Java中自定義注解的使用

    Annontation是Java5開始引入的新特征,中文名稱叫注解,它提供了一種安全的類似注釋的機(jī)制,用來將任何的信息或元數(shù)據(jù)(metadata)與程序元素(類、方法、成員變量等)進(jìn)行關(guān)聯(lián)。本文主要介紹了自定義注解的使用,希望對(duì)大家有所幫助
    2023-03-03
  • SpringBoot登錄用戶權(quán)限攔截器

    SpringBoot登錄用戶權(quán)限攔截器

    這篇文章主要介紹了SpringBoot登錄用戶權(quán)限攔截器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解)

    基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解)

    下面小編就為大家分享一篇基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 關(guān)于Controller層和Service層的類報(bào)錯(cuò)問題及解決方案

    關(guān)于Controller層和Service層的類報(bào)錯(cuò)問題及解決方案

    這篇文章主要介紹了關(guān)于Controller層和Service層的類報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評(píng)論

东乌珠穆沁旗| 万盛区| 夏河县| 卢龙县| 中宁县| 巢湖市| 鄂温| 宜宾市| 庄浪县| 济源市| 武川县| 海宁市| 民县| 汕头市| 文水县| 根河市| 吉林市| 嵊州市| 黄石市| 文成县| 宁城县| 定西市| 舒兰市| 青河县| 巴马| 曲周县| 阿坝| 凤山市| 临沂市| 多伦县| 大名县| 北宁市| 六枝特区| 读书| 祁门县| 海盐县| 临城县| 喀什市| 江津市| 上高县| 胶南市|