April 21, 2018

好用的mybatis拦截器

Using mybatis interceptor to change your sql execute action.

1. 什么是mybatis拦截器?

Mybatis采用责任链模式,通过动态代理组织多个插件(拦截器),通过这些插件可以改变Mybatis的默认行为(诸如SQL重写之类的),MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis允许使用插件来拦截的责任链节点包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)

它们分别是:

1. Executor是 Mybatis的内部执行器,它负责调用StatementHandler操作数据库,并把结果集通过 ResultSetHandler进行自动映射,另外,他还处理了二级缓存的操作。从这里可以看出,我们也是可以通过插件来实现自定义的二级缓存的。
2. StatementHandler是Mybatis直接和数据库执行sql脚本的对象。另外它也实现了Mybatis的一级缓存。这里,我们可以使用插件来实现对一级缓存的操作(禁用等等)。
3. ParameterHandler是Mybatis实现Sql入参设置的对象。插件可以改变我们Sql的参数默认设置。
4. ResultSetHandler是Mybatis把ResultSet集合映射成POJO的接口对象。我们可以定义插件对Mybatis的结果集自动映射进行修改。

通过拦截它们可以实现:

拦截执行器的方法
拦截参数的处理
拦截结果集的处理
拦截Sql语法构建的处理

2. 如何实现一个拦截器?

首先实现Interceptor接口,我们来看下这个接口的定义:

                  
package org.apache.ibatis.plugin;

import java.util.Properties;

public interface Interceptor {
    Object intercept(Invocation var1) throws Throwable;

    Object plugin(Object var1);

    void setProperties(Properties var1);
}
                  
                

这个接口只声明了三个方法。 setProperties方法是在Mybatis进行配置插件的时候可以配置自定义相关属性,即:接口实现对象的参数配置; plugin方法是插件用于封装目标对象的,通过该方法我们可以返回目标对象本身,也可以返回一个它的代理,可以决定是否要进行拦截进而决定要返回一个什么样的目标对象,官方提供了示例:return Plugin.wrap(target, this); intercept方法就是要进行拦截的时候要执行的方法;

注解拦截哪个责任链节点的哪个方法,例如拦截StatementHandler的prepare方法:

                  
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
@Slf4j
public class PermissionInterceptor implements Interceptor {
                  
                

StatementHandler节点类:

                  
package org.apache.ibatis.executor.statement;
......

public interface StatementHandler {
    Statement prepare(Connection var1, Integer var2) throws SQLException;

    void parameterize(Statement var1) throws SQLException;

    void batch(Statement var1) throws SQLException;

    int update(Statement var1) throws SQLException;

    <E> List<E> query(Statement var1, ResultHandler var2) throws SQLException;

    <E> Cursor<E> queryCursor(Statement var1) throws SQLException;

    BoundSql getBoundSql();

    ParameterHandler getParameterHandler();
}
                  
                

3. 拦截器怎么拦截责任链节点?

Mybatis的插件首先对责任链节点的对象生成动态代理,例如对StatementHandler对象生成动态代理,然后在被代理对象执行对应方法的时候,代理方法会执行,从而实现拦截StatementHandler对象的方法的效果,拦截后我们就可以为所欲为了。
那么现在我们看下Mybatis是怎么创建这四大接口对象的。

                  
...
public Executor newExecutor(Transaction transaction, ExecutorType executorType) { 
    executorType = executorType == null ? defaultExecutorType : executorType; 
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType; 
    Executor executor; 
    if (ExecutorType.BATCH == executorType) { 
        executor = new BatchExecutor(this, transaction); 
    } else if (ExecutorType.REUSE == executorType) { 
        executor = new ReuseExecutor(this, transaction); 
    } else { 
        executor = new SimpleExecutor(this, transaction); 
    } 
    if (cacheEnabled) { 
        executor = new CachingExecutor(executor); 
    } 
    executor = (Executor) interceptorChain.pluginAll(executor); 
    return executor; 
} 

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, 
    RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { 
    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler); 
    return statementHandler; 
} 

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { 
    ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql); 
    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler); 
    return parameterHandler; 
} 

public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, 
    ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) { 
    ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds); resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler); 
    return resultSetHandler; 
}
                  
                

所有的责任链节点对象都调用InterceptorChain.pluginAll()方法,该方法会把节点对象插入我们声明的所有拦截器中。

                  
public Object pluginAll(Object target) { 
    //给每一个拦截器注入责任链节点
    for (Interceptor interceptor : interceptors) {
        target = interceptor.plugin(target); 
    } 
    return target; 
}
                  
                

其中interceptor.plugin(target)方法这样实现:Plugin.wrap(target, this);

                  
/**
 * @param target        被代理节点对象 
 * @param Interceptor   拦截器
 */
public static Object wrap(Object target, Interceptor interceptor) { 
    // @Intercepts注解拦截的所有节点方法
    Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); 
    Class<?> type = target.getClass(); 
    // @Intercepts注解拦截的target类方法
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap); 
    if (interfaces.length > 0) {
        // 返回被拦截对象的动态代理,动态代理的InvocationHandler处理器为新建的Plugin对象。
        return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); 
    } 
    return target; 
}
                  
                

在代理对象执行对应方法的时候,会调用InvocationHandler处理器的invoke方法

                  
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
    try { 
        // 查看@Intercepts注解是否拦截该节点类
        Set<Method> methods = signatureMap.get(method.getDeclaringClass());
    if (methods != null && methods.contains(method)) { 
        // 执行拦截器的拦截方法
        return interceptor.intercept(new Invocation(target, method, args)); 
    } 
        return method.invoke(target, args);
    } 
    catch (Exception e) { 
        throw ExceptionUtil.unwrapThrowable(e);
    } 
}
                  
                

4. 一个使用拦截器为查询自动添加权限的例子

                  
......

@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}
)})
@Slf4j
public class PermissionInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = null;
        try {
            PermissionSql permissionSql = PermissionHelper.getPermissionSql();
            if (!SessionContext.isSuperAdmin() && null != permissionSql) {
                Executor executor = (Executor)invocation.getTarget();
                Object[] args = invocation.getArgs();
                MappedStatement ms = (MappedStatement)args[0];
                Object parameter = args[1];
                RowBounds rowBounds = (RowBounds)args[2];
                ResultHandler resultHandler = (ResultHandler)args[3];
                CacheKey cacheKey;
                BoundSql boundSql;
                if (args.length == 4) {
                    boundSql = ms.getBoundSql(parameter);
                    cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
                } else {
                    cacheKey = (CacheKey)args[4];
                    boundSql = (BoundSql)args[5];
                }

                //添加权限条件
                String sql = boundSql.getSql().replaceAll("where", "WHERE");
                String permissionCondition = "";
                List<Long> permissions = SessionContext.getCurrentUserOrganizations();
                if (!CollectionUtils.isEmpty(permissions)) {
                    String permissionField = permissionSql.getPermissionField();
                    Long[] permissionArray = permissions.toArray(new Long[permissions.size()]);
                    String permissionArrayStr = Arrays.toString(permissionArray);
                    permissionCondition = " " + permissionField + " IN(" + permissionArrayStr.substring(1, permissionArrayStr.length() - 1) + ") ";
                } else {
                    permissionCondition = " 0 = 1 ";
                }
                if (sql.contains("WHERE")) {
                    int whereIndex = permissionSql.getWhereIndex();
                    if (whereIndex == 1) {
                        sql = sql.substring(0, sql.indexOf("WHERE") + 5) + permissionCondition + "AND" + sql.substring(sql.indexOf("WHERE") + 5, sql.length());
                    } else {
                        int count = 0;
                        int idx = 0;
                        while ((idx = sql.indexOf("WHERE", idx)) != -1 && (++count) != whereIndex) {idx += 5;}
                        if (count == whereIndex) {
                            sql = sql.substring(0, idx + 5) + permissionCondition + "AND" + sql.substring(idx + 5, sql.length());
                        }
                    }
                } else {
                    sql += "\nWHERE" + permissionCondition;
                }

                //通过反射覆盖sql
                Field sqlField = boundSql.getClass().getDeclaredField("sql");
                sqlField.setAccessible(true);
                sqlField.set(boundSql, sql);

                //执行查询
                result = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
            }else {
                result = invocation.proceed();
            }
        } finally {
            PermissionHelper.clearPermissionSql();
        }
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}