带你学习Mybatis之拦截器

mybatis拦截器

mybatis拦截器也叫做插件,mybatis允许开发者自定义拦截器对SQL语句执行过程中的某一个点进行拦截。默认mybatis允许拦截Mybatis中的四大核心对象:Executor中的方法、ParameterHandler的方法、ResultSetHandler的方法以及StatementHandler中的方法

  • Executor中的update()、query()、flushStatements()、commit()、rollback()、getTransaction()、close()、isClose()方法,拦截执行器的方法

  • ParameterHandler中的getParameterObject()、setParameters()方法,拦截参数的处理

  • ResultSetHandler中的handlerResultSets()、handlerOutputParameters()方法,拦截结果集的处理

  • StatementHandler中的prepare()、parameterize()、batch()、update()、query()方法,拦截sql语句的构建

通过动态代理,为需要拦截的接口生成代理对象以实现接口方法拦截功能,当执行所拦截的方法时,就会进入拦截方法

Interceptor接口

mybatis使用拦截器需要实现Interceptor接口

public interface Interceptor {
  	// 覆盖所拦截对象的原有方法,执行拦截逻辑的方法
    Object intercept(Invocation var1) throws Throwable;
		// target为被拦截对象,作用是给被拦截对象生成一个代理对象
    default Object plugin(Object target) {
      // Plugin实现了InvocationHandler接口,wrap方法就是执行Proxy.newProxyInstance(type.getClassLoader(),interfaces,new Plugin(target, interceptor, signatureMap))
      	//使用JDK的动态代理,给target对象创建一个delegate代理对象,以此来实现方法拦截和增强功能,它会回调intercept()方法,使用当前拦截器来包装目标对象
      // 调用一次wrap方法产生一个代理对象,如果有第二个插件,会将第一个带对象传递给wrap方法,生成第二个代理对象,再调用Invocation.proceed会从最后一个代理对象的invoke方法运行到第一个代理对象的invoke方法,直至真实方法
        return Plugin.wrap(target, this);
    }
		// 根据配置初始化Interceptor对象,只在插件初始化的时候调用一次
    default void setProperties(Properties properties) {
    }
}

@Intercepts注解

自定义的拦截器除了实现Interceptor接口,还需要使用@Intercepts和@Signature注解进行标识

@Intercepts注解中指定了一个@Signature注解列表

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Intercepts {
    Signature[] value();
}

每个@Signature注解中标识了该插件需要拦截的方法信息

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
  	//拦截的对象类型
    Class<?> type();
		//拦截的方法
    String method();
		//被拦截方法的参数列表
    Class<?>[] args();
}
使用示例
@Intercepts(
        @Signature(type = StatementHandler.class,method = "prepare",args = {})
)

如何进行拦截的

就是在创建对象实例时生成代理类,采用责任链模式,由interceptorChain来进行生成代理对象并返回

// Configuration 类中的方法

// 创建ParameterHandler
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
  parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
  return parameterHandler;
}

// 创建ResultSetHandler
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) interceptorChain.pluginAll(resultSetHandler);
  return resultSetHandler;
}

// 创建StatementHandler
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) interceptorChain.pluginAll(statementHandler);
  return statementHandler;
}

// 创建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 {
    executor = new SimpleExecutor(this, transaction);
  }
  if (cacheEnabled) {
    executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);
  return executor;
}

工具类MetaObject

在编写插件的时候经常会使用到Mybatis提供的一个工具类MetaObject,可以有效地读取或修改一些重要对象的属性

// 获取对象属性值,支持OGNL
 public Object getValue(String name)
// 修改对象属性值,支持OGNL
 public void setValue(String name, Object value)

https://zhhll.icu/2020/框架/mybatis/组件分析/11.mybatis拦截器/

本文由mdnice多平台发布

  • 25
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值