Mybatis 拦截器实现原理

Mybatis 拦截器实现原理

标签(空格分隔): mybatis


拦截器概述

  • 像springmvc一样,mybatis也提供了拦截器实现,只是他们拦截的对象不同。
  • mybatis给Executor、StatementHandler、ResultSetHandler、ParameterHandler提供了拦截器功能,
    • Executor提供了增删改查的接口.
    • StatementHandler负责处理Mybatis与JDBC之间Statement的交互.
    • ResultSetHandler负责处理Statement执行后产生的结果集,生成结果列表.
    • ParameterHandler是Mybatis实现Sql入参设置的对象。
  • 拦截器采用了责任链模式,把请求发送者和请求处理者分开,各司其职。

立即开始

<plugins>
    <plugin interceptor="com.tamguo.demo.interceptor.StatementInterceptor"></plugin>
</plugins>

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class StatementInterceptor implements Interceptor {

    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println(" hello Statement interceptor !");
        return invocation.proceed();
    }

    public Object plugin(Object target) {
        Object wrap = Plugin.wrap(target, this);
        return wrap;
    }

    public void setProperties(Properties properties) {

    }
}

开始一个Statement拦截器,在mybatis-config.xml加入plugin配置,编写一个StatementInterceptor类。这样就已经完成了一个拦截器的编写,该类会链接StatementHandler的prepare方法。

MyBatis 加载intercetor过程

mybatis-intercetor-build

pluginElement() 是Confiration中,解析XML并把interceptorInstance加入到interceptorChain。

   private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        String interceptor = child.getStringAttribute("interceptor");
        Properties properties = child.getChildrenAsProperties();
        Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
        interceptorInstance.setProperties(properties);
        configuration.addInterceptor(interceptorInstance);
      }
    }
  }
  

到这里Confiration对象已经持有了一个InterceptorChain属性, InterceptorChain中只有一个intercetor的list集合。

sqlSessionFactory.openSession(),会创建Executor、StatementHandler、ResultSetHandler、ParameterHandler对象。

  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;
  }
  

executor = (Executor) interceptorChain.pluginAll(executor); 注意这一行代码,executor在这里已经被修改成一个代理对象。

转载于:https://www.cnblogs.com/tamguo/p/11231651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值