mybatis-plugin插件执行原理

今天主要是在看mybatis的主流程源码,其中比较感兴趣的是mybatis的plugin功能,这里主要记录下mybatis-plugin的插件功能原理。

plugin集合列表:在构建SqlSessionFactory时,通过解析配置或者plugin-bean的注入,会将所有的mybatis-plugin都收集到Configuration

对象的interceptorChain属性中。InterceptorChain类定义如下:

public class InterceptorChain {

  private final List<Interceptor> interceptors = new ArrayList<>();

  public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
} public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
} public List<Interceptor> getInterceptors() {
return Collections.unmodifiableList(interceptors);
} }

plugin作用对象Executor,ParameterHandler,ResultSetHandler,StatementHandler,这4个对象在mybatis执行sql的过程中

有不同的作用。

Executor:sql执行的具体操作对象。

ParameterHandler:sql执行前的参数处理对象。

ResultSetHandler:sql执行后的结果集处理对象。

StatementHandler:具体送到数据库执行的sql操作对象。

plugin作用原理:类似AOP,使用JDK动态代理,只不过mybatis的增强对象不是所有对象,而是上面陈列的4个对象而已。

在4个对象创建时,都会对各个对象进行判断,是否需要进行插件化。比如下面的插件:

@Intercepts({@Signature( type= Executor.class,  method = "query", args ={
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class
})})
public class ExamplePlugin implements Interceptor { // 分页 读写分离 Select 增删改 public Object intercept(Invocation invocation) throws Throwable {
System.out.println("代理");
Object[] args = invocation.getArgs();
MappedStatement ms= (MappedStatement) args[0];
// 执行下一个拦截器、直到尽头
return invocation.proceed();
}
}

该插件将会在Executor该对象创建时,使用该插件进行增强。在新开一个sqlSession时,将会创建Executor对象。跟踪到具体方法:

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 {
//简单的sql执行器对象
executor = new SimpleExecutor(this, transaction);
}
//判断mybatis的全局配置文件是否开启缓存
if (cacheEnabled) {
//把当前的简单的执行器包装成一个CachingExecutor
executor = new CachingExecutor(executor);
}
/**
* TODO:调用所有的拦截器对象plugin方法
* 插件: 责任链+ 装饰器模式(动态代理)
*/
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

我们找到interceptorChain.pluginAll方法:

public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}

发现会通过已加载的所有plugin列表中,逐个遍历去筛选出符合Executor类型的插件,再通过具体插件的interceptor.plugin方法去创建

Executor的代理对象。

public interface Interceptor {

  Object intercept(Invocation invocation) throws Throwable;

  default Object plugin(Object target) {
return Plugin.wrap(target, this);
}
default void setProperties(Properties properties) {
// NOP
}
}

再看到具体的Plugin.wrap(target, this)方法:

  public static Object wrap(Object target, Interceptor interceptor) {
// 获得interceptor配置的@Signature的type
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
// 当前代理类型
Class<?> type = target.getClass();
// 根据当前代理类型 和 @signature指定的type进行配对, 配对成功则可以代理
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}

这里我们就很清楚了,通过@Signature注解上的type、method、args属性去匹配,如果找到符合的,就会为对象创建代理对象,并返回代理对象。

责任链设计模式:因为一个增强对象可能会有多个plugin的增强逻辑,所以在执行的时候使用的是责任链设计模式。



因为Plugin.wrap()方法新建的代理对象中使用的InvocationHandler对象是Plugin本身,所以在执行方法的时候首先要调用它的invoke方法,

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
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);
}
}

当我们执行Executor的query方法时,符合if (methods != null && methods.contains(method)) {条件,这时就会去执行具体插件的增强方法,interceptor.intercept

然后再通过传递new Invocation(target, method, args)对象,在插件执行完之后,再调用invocation.proceed()去执行下一个插件逻辑。

如下是对Executor的query方法添加了2个插件的场景:

总结:如果我们的业务需要我们去编写sql插件,那我们就需要来研究下Executor,ParameterHandler,ResultSetHandler,StatementHandler这4个对象的具体跟sql相关的方法,

然后再进行修改,就可以直接起到aop的作用。

mybatis-plugin插件执行原理的更多相关文章

  1. idea使用破解版mybatis plugin插件失败,idea打不开的解决方案

    记一次错误解决方案 打开 idea.vmoptions (Help -> Edit Custom VM Options...) ,在这里进行了修改 加了破解jar包的路径,但是之前的路径中有中文 ...

  2. Mybatis Plugin插件安装破解及使用

    2018年2月更新 2018年2月份,提供一个网上比较多的一个版本V3.21版本,下载资源里面有个已整合版直接解压放入C:\Users\你的用户名\.IntelliJIdea2017.3\config ...

  3. Mybatis之plugin插件设计原理

    大多数框架,都支持插件,用户可通过编写插件来自行扩展功能,Mybatis也不例外. 我们从插件配置.插件编写.插件运行原理.插件注册与执行拦截的时机.初始化插件.分页插件的原理等六个方面展开阐述. 一 ...

  4. 简述 Mybatis 的插件运行原理,以及如何编写一个插件?

    Mybatis 仅可以编写针对 ParameterHandler.ResultSetHandler. StatementHandler.Executor 这 4 种接口的插件,Mybatis 使用 J ...

  5. 简述 Mybatis 的插件运行原理,以及如何编写一个插件。

    Mybatis 仅可以编写针对 ParameterHandler.ResultSetHandler. StatementHandler.Executor 这 4 种接口的插件,Mybatis 使用 J ...

  6. MyBatis动态代理执行原理

    前言 大家使用MyBatis都知道,不管是单独使用还是和Spring集成,我们都是使用接口定义的方式声明数据库的增删改查方法.那么我们只声明一个接口,MyBatis是如何帮我们来实现SQL呢,对吗,我 ...

  7. IDEA Mybatis plugin插件破解

    破解文件: 链接:https://pan.baidu.com/s/1J7asfLc5I0RBcoYX3_yNvQ 提取码:kjxv 使用方法: C:\Users\{你的用户名}\.IntelliJId ...

  8. 【插件】【idea】的Mybatis Plugin插件方便mapper接口方法和mapper XML文件之间来回切换

    效果 安装 这是2019.2版本的,旧版的有点不一样

  9. 【MyBatis源码分析】插件实现原理

    MyBatis插件原理----从<plugins>解析开始 本文分析一下MyBatis的插件实现原理,在此之前,如果对MyBatis插件不是很熟悉的朋友,可参看此文MyBatis7:MyB ...

随机推荐

  1. 超全selenium元素定位XPath、CSS

    说明:在HTML页面中,<p> 是一个标签,<p>hello</p> 是一个元素,元素由一个开始的标签和结束的标签组成.<font color="r ...

  2. goalng-sync/atomic原子操作

    目录 1.go已经提供了锁,为什么还需要atomic原子操作? 2.atomic原子操作为什么比mutex快? 3.CAS 4.互斥锁与原子操作区别 5.原子操作方法 5.1 atomic.AddIn ...

  3. java基础Synchronized关键字之对象锁

    java中Synchronized关键字之对象锁    当有多个线程对一个共享数据进行操作时,需要注意多线程的安全问题. 多线程的同步机制对资源进行加锁,使得在同一个时间,只有一个线程可以进行操作,同 ...

  4. Apache Dolphinscheduler3.0.0-beta-1 版本发布,新增FlinkSQL、Zeppelin任务类型

    导读:近日,Apache Dolphin Scheduler 迎来了 3.0.0-beta-1 版本的正式发布.新版本主要针对 3.0.0-alpha 进行了代码和文档的修复,并引入了部分的功能,如支 ...

  5. React性能优化,六个小技巧教你减少组件无效渲染

    壹 ❀ 引 在过去的一段时间,我一直围绕项目中体验不好或者无效渲染较为严重的组件做性能优化,多少积累了一些经验所以想着整理成一片文章,下图就是优化后的一个组件,可以对比优化前一次切换与优化后多次切换的 ...

  6. java-代码操作服务器之SSH连续发送命令

    java操作Linux服务器可以使用专用的jar包,这里介绍使用jsch操作Linux服务器 maven 依赖 <dependency> <groupId>com.jcraft ...

  7. ASP.NET CORE在docker中的健康检查(healthcheck)

    在使用docker-compose的过程中,很多程序都提供了健康检查(healthcheck)的方法,通过健康检查,应用程序能够在确保其依赖的程序都已经启动的前提下启动,减少各种错误的发生,同时,合理 ...

  8. tty的crash分析

    crash> btPID: 410629 TASK: ffff883fea379fa0 CPU: 10 COMMAND: "jupyter-lab"#0 [ffff8823c ...

  9. 使用自定义隐式转换快速创建失败Result

    系统要求方法都返回 Result 结果,通常我们会如此定义一个 Result 1 public class Result<T> 2 { 3 public virtual int Code ...

  10. 【MySQL】从入门到精通6-MySQL数据类型与官方文档

    上期:[MySQL]从入门到精通5-一对多-外键 这个是官方文档链接,是世界上最全面的MySQL教学了,所有问题都可以在这里找到解决方法. https://dev.mysql.com/doc/ htt ...