MyBATIS中的插件原理

本文详细解析了MyBatis插件机制的核心原理,包括如何实现Interceptor接口以创建插件,以及插件中intercept、plugin和setProperties三个关键方法的作用。通过XMLConfigBuilder的配置解析过程和Executor对象的动态代理生成流程,深入理解MyBatis插件的工作机制。

首先在mybatis中要使用插件你必须实现:

  • org.apache.ibatis.plugin.Interceptor接口
  • 有三个方法:
    • intercept 真个是插件真正运行的方法,
      • 它将直接覆盖掉你真实拦截对象的方法。
      • 里面有一个Invocation对象,利用它可以调用你原本要拦截的对象的方法
    • plugin    它是一个生成动态代理对象的方法,
    • setProperties 它是允许你在使用插件的时候设置参数值。

初始化:

  • XMLConfigBuilder
    • private void parseConfiguration(XNode root) 
      • pluginElement(root.evalNode("plugins"));

Executor对象的取出:

  • Configuration 类下面
    • public Executor newExecutor(Transaction transaction, ExecutorType executorType)
      • executor = (Executor) interceptorChain.pluginAll(executor);
        • public Object plugin(Object target) {
              return Plugin.wrap(target, this);
          }
          • Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);

 

 

转载于:https://my.oschina.net/u/3847203/blog/3019185

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值