zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    17. 电话号码的字母组合
    12. 整数转罗马数字
    01-正则表达式基础
    前端SEO技巧
    node.js
    Vue.生命周期
    Vue小案例--过滤器的基本操作
    vue简单的计算器
    VSCode 自动刷新
    Vue.js学习
  • 原文地址:https://www.cnblogs.com/nxzblogs/p/9149980.html
Copyright © 2011-2022 走看看