Mybatis-Plugin解析

Posted

tags:

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

参考技术A 首先mybatisPlugin是针对于mybatis四大组件(Statementhandler、resultsethandler、parameterHandler、executor)做增强操作的。
要使用自定义plugin首先要实现Interceptor接口,如下:

首先自定义一个plugin实现接口如下:

第一个方法intercept里面执行了Invocarion对象的proceed方法,可以看出使用了反射执行了目标方法,这边有几个参数,分别有注释解释。

第二个方法plugin执行了Plugin.Wrap方法,传入的参数是被代理对象,以及当前interceptor对象。Plugin对象如下:

可以看出plugin实现了拦截器接口,对方法进行拦截,接下来看wrap方法做了什么:

这边具体看一下getAllInterfaces如何判断的:

接下来看Plugin对象实现的invoke方法,在执行newProxyInstance方法时传入了一个新建的Plugin对象,包含了目标对象,interceptor以及需要拦截的方法:

之前介绍过了,Interceptor的intercept方法执行了invocation方法的proceed方法,而proceed方法又是反射执行原方法,所以增强是在intercept方法执行的。

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的实现原理(源码分析)

玩转Git三剑客

Mybatis源码解析-mybatis-spring原理解析

Mybatis源码解析MyBatis解析全局配置文件

MyBatis源码解析 - 解析器模块