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. 攔截器配置
注:若項(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)文章
解析Neatbeans(常見錯(cuò)誤) build-impl.xml:305: Compile failed
本篇文章是對(duì)Neatbeans(常見錯(cuò)誤) build-impl.xml:305: Compile failed的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07idea 在springboot中使用lombok插件的方法
這篇文章主要介紹了idea 在springboot中使用lombok的相關(guān)資料,通過代碼給大家介紹在pom.xml中引入依賴的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08JAVA Stack詳細(xì)介紹和示例學(xué)習(xí)
JAVA Stack是棧。它的特性是:先進(jìn)后出(FILO, First In Last Out)。2013-11-11利用Java+OpenCV實(shí)現(xiàn)拍照功能
網(wǎng)上大多是利用C語言或者Python實(shí)現(xiàn)拍照功能,本文將為大家介紹另一種方法,即在Java中調(diào)用OpenCV實(shí)現(xiàn)拍照功能,感興趣的可以了解一下2022-01-01WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox
這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox,文中示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題
我們Pojo類的屬性名和數(shù)據(jù)庫中的字段名不一致的現(xiàn)象時(shí)有發(fā)生,本文就詳細(xì)的介紹一下Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題,感興趣的可以了解一下2021-10-10Java查詢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