mybatis的插件分析

mybatis的插件分析

 

mybatis插件回在解析配置是通过pluginAll方法将插件添加到插件链中,然后会在sqlSessionfactory.openSession()方法中将插件链绑到executor上,在执行sql的时候回拦截具体方法后,通过代理类来进行具体处理。

 

官方文档:http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins

 

通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定想要拦截的方法签名即可

官方例子

// ExamplePlugin.java
@Intercepts({@Signature(
  type= Executor.class,
  method = "update",
  args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
  public Object intercept(Invocation invocation) throws Throwable {
    return invocation.proceed();
  }
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  public void setProperties(Properties properties) {
  }
}

<!-- mybatis-config.xml -->
<plugins>
  <plugin interceptor="org.mybatis.example.ExamplePlugin">
    <property name="someProperty" value="100"/>
  </plugin>
</plugins>

上面的插件将会拦截在 Executor 实例中所有的 “update” 方法调用, 这里的 Executor 是负责执行低层映射语句的内部对象

步骤:

1、实现interceptor接口

2、在自定义plugin上配置注解@Intercepts

  注解配置:@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,RowBounds.class, ResultHandler.class }) })

  type:Executor、StatementHandler、ParameterHandler、ResultsetHandler四个接口

  method:以上四个接口中的方法

  args:以上方法中的参数

3、mybatis.xml中配置标签<plugin><>

 

 

原理思想分析可参考:https://blog.csdn.net/reliveit/article/details/50289395

 

 
 
 
 
 
 
 
posted @ 2018-06-07 12:18 犇犇丶 阅读(...) 评论(...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

巡山小妖008

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

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

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

打赏作者

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

抵扣说明:

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

余额充值