mybatis源码解析之基础模块-Plugin


在这里插入图片描述

MyBatis源码解析之基础模块—Plugin

前文回顾

上一章节我们一起学习了Mapper接口绑定的源码逻辑。本次我们学习MyBatis的Plugin数据源模块。

架构设计

Plugin模块所在包路径为org.apache.ibatis.plugin,对应的类架构设计图如下:

在这里插入图片描述

源码解读

Signature

Signature注解类主要定义了三个属性,通过这些属性定位对应要拦截的方法。

package org.apache.ibatis.plugin;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 拦截器方法签名注解类
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({
   })
public @interface Signature {
   
  /**
   * type为执行器类型
   */
  Class<?> type();

  /**
   * 要拦截的方法名
   */
  String method();

  /**
   * 被拦截方法的参数数组列表(切记参数列表的顺序要完全一致)
   */
  Class<?>[] args();
}

Intercepts

该注解类只有一个属性,即Signature数组列表。也即是说该注解是一个拦截组合,里面聚合了多个要拦截的方法签名。

public @interface Intercepts {
   
  /**
   * 要拦截的方法签名数组
   */
  Signature[] value();
}

用于编写的自定义拦截器实现类,其注解格式如下:

@Intercepts({
   @Signature(type= Executor.class, method = "update", args = {
   MappedStatement.class ,Object.class})})

Signature、Intercepts定义了拦截注解的标志,那mybatis是如何实现拦截的呢?咱们接着往下看。

Invocation

在Mybatis的plugin包中,定义了一个Invocation类,该类有三个属性:target、method、args。看到这三个大家应该似曾相识吧,没错就是与动态代理有关。

同时封装了反射调用的方法。

package org.apache.ibatis.plugin;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Invocation {
   

  private final Object target; //目标对象
  private final Method method; //执行方法
  private final Object[] args; //方法参数

  //构造犯法
  public Invocation(Object target, Method method, Object[] args) {
   
    this.target = target;
    this.method = method;
    this.args = args;
  }

  /*** target、method、args的get方法 ***/

  //代理执行方法
  public Object proceed() throws InvocationTargetException, IllegalAccessException {
   
    //反射调用
    return method.invoke(target, args);
  }
}

Interceptor

Interceptor为自定义的拦截器接口,用户自定义的拦截器都必须实现该接口。该接口及内部方法的具体的说明参考源码注释:

package org.apache.ibatis.plugin;

import java.util.Properties;

/**
 * @author Clinton Begin
 * 自定义拦截器的接口
 * 实现该接口的实现类,需要有注解:
 样例:
 @Intercepts({@Signature(type= Executor.class, method = "update", args = {MappedStatement.class 	     ,Object.class})})
 */
public interface Interceptor {
   

  //对需要拦截的业务进行增强操
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值