mybatis 攔截器添加參數(shù)的實(shí)現(xiàn)
以登錄用戶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. 攔截器配置
注:若項目中引用了 PageHelper 分頁器,此方法會失效。
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)List分組的常見方法詳解
這篇文章主要為大家詳細(xì)介紹了使用Java實(shí)現(xiàn)List分組的幾個常見方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-12-12
java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼
本篇文章主要介紹了java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼
本文主要介紹了spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
深入了解Spring中最常用的11個擴(kuò)展點(diǎn)
我們一說到spring,可能第一個想到的是?IOC(控制反轉(zhuǎn))?和?AOP(面向切面編程)。除此之外,我們在使用spring的過程中,有沒有發(fā)現(xiàn)它的擴(kuò)展能力非常強(qiáng)。今天就來跟大家一起聊聊,在Spring中最常用的11個擴(kuò)展點(diǎn)2022-09-09
Java中實(shí)現(xiàn)定時任務(wù)的兩種方法舉例詳解
這篇文章主要給大家介紹了關(guān)于Java中實(shí)現(xiàn)定時任務(wù)的兩種方法,文中總結(jié)了各種實(shí)現(xiàn)方式的優(yōu)缺點(diǎn),并給出了推薦的使用場景,通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
Spring Cloud Alibaba教程之Sentinel的使用
這篇文章主要介紹了Spring Cloud Alibaba教程之Sentinel的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法
腳本文件,通常放入/bin目錄下,編寫啟動腳本需要保證能夠識別到對應(yīng)的jar文件,其次需要保證能夠識別到/config中的配置文件信息,這篇文章主要介紹了Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法,需要的朋友可以參考下2022-12-12
Java實(shí)現(xiàn)多線程斷點(diǎn)下載
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線程斷點(diǎn)下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03

