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

mybatis 攔截器添加參數(shù)的實(shí)現(xiàn)

 更新時(shí)間:2024年12月12日 10:27:42   作者:點(diǎn)滴1993  
本文主要介紹了MyBatis攔截器中添加參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

以登錄用戶(hù)ID為例, 再攔截器中加入,在mapper.xml文件中通過(guò) #{currentUserId}或${currentUserId} 獲取參數(shù)。

1. 攔截器示例代碼

package com.xxx.framework.interceptor;

import com.xxx.common.core.domain.BaseEntity;
import com.xxx.framework.shiro.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
 * 全局參數(shù)攔截器
 *
 * @author xm.z
 */
@Slf4j
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})}
)
public class GlobalParametersInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        Object params = args[1];
        if (params instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) params;
            baseEntity.setCurrentUserId(getCurrentUserId());
        } else if (params instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap<Object> map = (MapperMethod.ParamMap) params;
            map.put("currentUserId", getCurrentUserId());
        }
        invocation.getArgs()[1] = params;
        return invocation.proceed();
    }

    /**
     * 獲取當(dāng)前登錄用戶(hù)ID
     *
     * @return 用戶(hù)ID
     */
    private String getCurrentUserId() {
        try {
            return ShiroUtils.getUserId().toString();
        } catch (Exception ignored) {
            return null;
        }
    }
}

2. 攔截器配置

注:若項(xiàng)目中引用了 PageHelper 分頁(yè)器,此方法會(huì)失效。

package com.xxx.framework.config;

import com.xxx.framework.interceptor.GlobalParametersInterceptor;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 攔截器配置
 *
 * @author xm.z
 */
@Configuration
@ConditionalOnBean({SqlSessionFactory.class})
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MybatisInterceptorConfig {

    private final List<SqlSessionFactory> sqlSessionFactories;

    @PostConstruct
    public void addPageInterceptor() {
        GlobalParametersInterceptor globalParametersInterceptor = new GlobalParametersInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) {
            sqlSessionFactory.getConfiguration().addInterceptor(globalParametersInterceptor);
        }
    }

}

3. PageHelper攔截器配置

Mybatis 攔截器是采用的責(zé)任鏈模式,一般攔截器中intercept方法中最后執(zhí)行 invocation.proceed() 方法,PageInterceptor 分頁(yè)器并未向后傳遞參數(shù)而是執(zhí)行了query方法, 所以需要將自定義攔截器放在PageInterceptor的后面(PS: 最后加入的攔截器最先執(zhí)行)。

package com.xxx.framework.config;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.xxx.framework.interceptor.GlobalParametersInterceptor;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 攔截器配置
 *
 * @author xm.z
 */
@Configuration
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MybatisInterceptorConfig implements BeanPostProcessor {

    private final List<SqlSessionFactory> sqlSessionFactories;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof PageHelperAutoConfiguration) {
            GlobalParametersInterceptor globalParametersInterceptor = new GlobalParametersInterceptor();
            for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) {
                sqlSessionFactory.getConfiguration().addInterceptor(globalParametersInterceptor);
            }
        }
        return bean;
    }
}

到此這篇關(guān)于mybatis 攔截器添加參數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis 攔截器添加參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 啟動(dòng)Solr提示Java版本低問(wèn)題解決方案

    啟動(dòng)Solr提示Java版本低問(wèn)題解決方案

    這篇文章主要介紹了啟動(dòng)Solr提示Java版本低問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java實(shí)現(xiàn)List分組的常見(jiàn)方法詳解

    Java實(shí)現(xiàn)List分組的常見(jiàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了使用Java實(shí)現(xiàn)List分組的幾個(gè)常見(jiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-12-12
  • java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼

    java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼

    本篇文章主要介紹了java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)維護(hù)的示例代碼

    spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)維護(hù)的示例代碼

    本文主要介紹了spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)維護(hù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • java中forward轉(zhuǎn)發(fā)的使用

    java中forward轉(zhuǎn)發(fā)的使用

    在Java中,forward轉(zhuǎn)發(fā)是一種非常常見(jiàn)且重要的操作,我們將深入探討forward的概念和用法,并給出一些代碼示例來(lái)幫助讀者更好地理解,感興趣的可以了解下
    2023-11-11
  • 深入了解Spring中最常用的11個(gè)擴(kuò)展點(diǎn)

    深入了解Spring中最常用的11個(gè)擴(kuò)展點(diǎn)

    我們一說(shuō)到spring,可能第一個(gè)想到的是?IOC(控制反轉(zhuǎn))?和?AOP(面向切面編程)。除此之外,我們?cè)谑褂胹pring的過(guò)程中,有沒(méi)有發(fā)現(xiàn)它的擴(kuò)展能力非常強(qiáng)。今天就來(lái)跟大家一起聊聊,在Spring中最常用的11個(gè)擴(kuò)展點(diǎn)
    2022-09-09
  • Java中實(shí)現(xiàn)定時(shí)任務(wù)的兩種方法舉例詳解

    Java中實(shí)現(xiàn)定時(shí)任務(wù)的兩種方法舉例詳解

    這篇文章主要給大家介紹了關(guān)于Java中實(shí)現(xiàn)定時(shí)任務(wù)的兩種方法,文中總結(jié)了各種實(shí)現(xiàn)方式的優(yōu)缺點(diǎn),并給出了推薦的使用場(chǎng)景,通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • Spring Cloud Alibaba教程之Sentinel的使用

    Spring Cloud Alibaba教程之Sentinel的使用

    這篇文章主要介紹了Spring Cloud Alibaba教程之Sentinel的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Windows編寫(xiě)jar啟動(dòng)腳本和關(guān)閉腳本的操作方法

    Windows編寫(xiě)jar啟動(dòng)腳本和關(guān)閉腳本的操作方法

    腳本文件,通常放入/bin目錄下,編寫(xiě)啟動(dòng)腳本需要保證能夠識(shí)別到對(duì)應(yīng)的jar文件,其次需要保證能夠識(shí)別到/config中的配置文件信息,這篇文章主要介紹了Windows編寫(xiě)jar啟動(dòng)腳本和關(guān)閉腳本的操作方法,需要的朋友可以參考下
    2022-12-12
  • Java實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)下載

    Java實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)下載

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評(píng)論

五峰| 紫阳县| 临猗县| 瓮安县| 吉木萨尔县| 柳河县| 潜江市| 威信县| 晋江市| 锡林郭勒盟| 堆龙德庆县| 门源| 葵青区| 庆城县| 平邑县| 栖霞市| 栖霞市| 太原市| 鹿邑县| 新巴尔虎右旗| 波密县| 台南市| 邢台市| 汕尾市| 靖西县| 崇阳县| 理塘县| 呼图壁县| 元谋县| 宁晋县| 新乡县| 潞西市| 托克逊县| 仙桃市| 东乡| 循化| 临清市| 南雄市| 浪卡子县| 玉环县| 桂东县|