问题简述
项目自定义了mybatis拦截器,用来把当前用户的组织id设置进入参,但是生成的sql并没有这个字段,说明动态sql的标签判断的组织id是空。明明设置了,为什么判断是空呢?
调试发现
进入到当前拦截器时,已经有了boundSql,也就是说在我们拦截器拦截之前就已经生成了sql,查找方法栈发现,是PageHelper注册了一个拦截器,处理了query,之后把处理后的sql给了后面的拦截器,导致我设置的组织id没有绑定进sql。那么现在的目标就是,如何让自定义的拦截器执行在PageHelper的拦截器之前。
自定义的拦截器:
@Intercepts(value = {
@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}),
@Signature(type = Executor.class, method = "update", args = {
MappedStatement.class, Object.class}),
})
public class BaseEntityInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
// get sql
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
// get parameter , this is the target object that you want to handle
Object parameter = invocation.