欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

以登錄用戶ID為例, 再攔截器中加入,在mapper.xml文件中通過 #{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)前登錄用戶ID
     *
     * @return 用戶ID
     */
    private String getCurrentUserId() {
        try {
            return ShiroUtils.getUserId().toString();
        } catch (Exception ignored) {
            return null;
        }
    }
}

2. 攔截器配置

注:若項(xiàng)目中引用了 PageHelper 分頁器,此方法會(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 分頁器并未向后傳遞參數(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)文章

  • Java中的?CyclicBarrier詳解

    Java中的?CyclicBarrier詳解

    這篇文章主要介紹了Java中的?CyclicBarrier詳解,CyclicBarrier沒有顯示繼承哪個(gè)父類或者實(shí)現(xiàn)哪個(gè)父接口,?所有AQS和重入鎖不是通過繼承實(shí)現(xiàn)的,而是通過組合實(shí)現(xiàn)的,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • 解析Neatbeans(常見錯(cuò)誤) build-impl.xml:305: Compile failed

    解析Neatbeans(常見錯(cuò)誤) build-impl.xml:305: Compile failed

    本篇文章是對(duì)Neatbeans(常見錯(cuò)誤) build-impl.xml:305: Compile failed的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • idea 在springboot中使用lombok插件的方法

    idea 在springboot中使用lombok插件的方法

    這篇文章主要介紹了idea 在springboot中使用lombok的相關(guān)資料,通過代碼給大家介紹在pom.xml中引入依賴的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • JAVA Stack詳細(xì)介紹和示例學(xué)習(xí)

    JAVA Stack詳細(xì)介紹和示例學(xué)習(xí)

    JAVA Stack是棧。它的特性是:先進(jìn)后出(FILO, First In Last Out)。
    2013-11-11
  • 利用Java+OpenCV實(shí)現(xiàn)拍照功能

    利用Java+OpenCV實(shí)現(xiàn)拍照功能

    網(wǎng)上大多是利用C語言或者Python實(shí)現(xiàn)拍照功能,本文將為大家介紹另一種方法,即在Java中調(diào)用OpenCV實(shí)現(xiàn)拍照功能,感興趣的可以了解一下
    2022-01-01
  • WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox

    WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox

    這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox,文中示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題

    Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題

    我們Pojo類的屬性名和數(shù)據(jù)庫中的字段名不一致的現(xiàn)象時(shí)有發(fā)生,本文就詳細(xì)的介紹一下Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題,感興趣的可以了解一下
    2021-10-10
  • Java正則表達(dá)式的基本用法和實(shí)例大全

    Java正則表達(dá)式的基本用法和實(shí)例大全

    這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式的基本用法和實(shí)例的相關(guān)資料,大家在使用Java正則表達(dá)式的時(shí)候可查閱這篇文章,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • 全面剖析java中的注解(Annotation)

    全面剖析java中的注解(Annotation)

    一個(gè)詞就可以描述注解,那就是元數(shù)據(jù),即一種描述數(shù)據(jù)的數(shù)據(jù)。所以,可以說注解就是源代碼的元數(shù)據(jù)。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java查詢Elasticsearch數(shù)據(jù)根據(jù)指定id檢索(in查詢)、sql權(quán)限過濾、多字段匹配檢索及數(shù)據(jù)排序

    Java查詢Elasticsearch數(shù)據(jù)根據(jù)指定id檢索(in查詢)、sql權(quán)限過濾、多字段匹配檢索及數(shù)據(jù)排序

    在Java開發(fā)中Elasticsearch(簡(jiǎn)稱ES)是一個(gè)非常流行的搜索引擎,它提供了強(qiáng)大的全文搜索和分析功能,這篇文章主要給大家介紹了關(guān)于Java查詢Elasticsearch數(shù)據(jù)根據(jù)指定id檢索(in查詢)、sql權(quán)限過濾、多字段匹配檢索及數(shù)據(jù)排序的相關(guān)資料,需要的朋友可以參考下
    2024-05-05

最新評(píng)論