mybatis 拦截器添加参数的实现
以登录用户ID为例, 再拦截器中加入,在mapper.xml文件中通过 #{currentUserId}或${currentUserId} 获取参数。
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;
/**
* 全局参数拦截器
*
* @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();
}
/**
* 获取当前登录用户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 拦截器是采用的责任链模式,一般拦截器中intercept方法中最后执行 invocation.proceed() 方法,PageInterceptor 分页器并未向后传递参数而是执行了query方法, 所以需要将自定义拦截器放在PageInterceptor的后面(PS: 最后加入的拦截器最先执行)。
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;
}
}到此这篇关于mybatis 拦截器添加参数的实现的文章就介绍到这了,更多相关mybatis 拦截器添加参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解析Neatbeans(常见错误) build-impl.xml:305: Compile failed
本篇文章是对Neatbeans(常见错误) build-impl.xml:305: Compile failed的解决方法进行了详细的分析介绍,需要的朋友参考下2013-07-07
idea 在springboot中使用lombok插件的方法
这篇文章主要介绍了idea 在springboot中使用lombok的相关资料,通过代码给大家介绍在pom.xml中引入依赖的方法,本文给大家介绍的非常详细,需要的朋友可以参考下2021-08-08
Mybatis中ResultMap解决属性名和数据库字段名不一致问题
我们Pojo类的属性名和数据库中的字段名不一致的现象时有发生,本文就详细的介绍一下Mybatis中ResultMap解决属性名和数据库字段名不一致问题,感兴趣的可以了解一下2021-10-10
Java查询Elasticsearch数据根据指定id检索(in查询)、sql权限过滤、多字段匹配检索及数据排序
在Java开发中Elasticsearch(简称ES)是一个非常流行的搜索引擎,它提供了强大的全文搜索和分析功能,这篇文章主要给大家介绍了关于Java查询Elasticsearch数据根据指定id检索(in查询)、sql权限过滤、多字段匹配检索及数据排序的相关资料,需要的朋友可以参考下2024-05-05


最新评论