mybatis-plugin的实现原理(源码分析)

Posted 八阿哥克星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-plugin的实现原理(源码分析)相关的知识,希望对你有一定的参考价值。

mybatis的plugin插件就是拦截器,可以对ParameterHandler、ResultSetHandler、StatementHandler、Executor共4个接口对象内的方法进行拦截,因此,自定义插件,必须要实现org.apache.ibatis.plugin.Interceptor接口。重写方法interceptor()

package org.apache.ibatis.plugin;

import java.util.Properties;

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

    default Object plugin(Object target) 
        return Plugin.wrap(target, this);
    

    default void setProperties(Properties properties) 
    

上图中plugin()方法,是给目标对象生成一个动态代理对象,再有代理对象执行拦截方法,调用重写的interceptor()。

@Intercepts(@Signature(
    type = StatementHandler.class,
    method = "prepare",
    args = Connection.class, Integer.class
))

另外,注解@Interceptors是必加的,目的就是明确该拦截器的要拦截的对象甚至方法,type是要拦截的类,method是方法名称,args是参数。

Interceptor拦截器注册时机是在ParameterHandler、ResultSetHandler、StatementHandler、Executor共4个接口对象创建的时候,configuration对象中:

 public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) 
        ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
        //将目标对象加到拦截器链中
        parameterHandler = (ParameterHandler)this.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 = (ResultSetHandler)this.interceptorChain.pluginAll(resultSetHandler);
        return resultSetHandler;
    

    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 = (StatementHandler)this.interceptorChain.pluginAll(statementHandler);
        return statementHandler;
    

    public Executor newExecutor(Transaction transaction) 
        return this.newExecutor(transaction, this.defaultExecutorType);
    

interceptorChain.pluginAll()方法:该方法在创建上述4个接口对象时调用,其含义为给这些接口对象注册拦截器功能,注意是注册,而不是执行拦截

 public Object pluginAll(Object target) 
        Interceptor interceptor;
        //for循环代表了只要是插件,都会以责任链的方式逐一执行
        for(Iterator var2 = this.interceptors.iterator(); var2.hasNext(); target = interceptor.plugin(target)) 
            interceptor = (Interceptor)var2.next();
        

        return target;
    

  拦截器执行时机:plugin()方法注册拦截器后,那么,在执行上述4个接口对象内的具体方法时,就会自动触发拦截器的执行,也就是插件的执行。

以上是关于mybatis-plugin的实现原理(源码分析)的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis-Plugin解析

mybatis-plugin IDEA破解 mac

ConcurrentHashMap实现原理及源码分析

Java线程池实现原理和源码分析

CopyOnWriteArrayList实现原理及源码分析

深入JVM源码分析Synchronized实现原理