童话述说我的结局

童话述说我的结局

一、MyBatis插件

插件是一种常见的扩展方式,大多数开源框架也都支持用户通过添加自定义插件的方式来扩展或者改变原有的功能,MyBatis中也提供的有插件,虽然叫插件,但是实际上是通过拦截器(Interceptor)实现的,在MyBatis的插件模块中涉及到责任链模式和JDK动态代理。

1. 自定义插件

首先我们来看下一个自定义的插件我们要如何来实现。https://mybatis.org/mybatis-3/zh/configuration.html#plugins

mybaits源码分析--自定义插件(七)-LMLPHP

1.1 创建Interceptor实现类

创建的拦截器必须要实现Interceptor接口,Interceptor接口的定义为

public interface Interceptor {

  // 执行拦截逻辑的方法 111
  Object intercept(Invocation invocation) throws Throwable;

  // 决定是否触发 intercept()方法
  default Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }

  // 根据配置 初始化 Intercept 对象
  default void setProperties(Properties properties) {
    // NOP
  }

}
09-03 23:03