• mybatis中如何编写一个自定义插件?


    转自:

    mybatis中如何编写一个自定义插件?

    下文笔者讲述mybatis中编写自定义插件的方法分享,如下所示

    Mybatis自定义插件主要借助Mybatis四大对象:
       (Executor、StatementHandler 、ParameterHandler 、ResultSetHandler)进行拦截 
    
    Executor:拦截执行器的方法(log记录) 
    StatementHandler:拦截Sql语法构建的处理 
    ParameterHandler:拦截参数的处理 
    ResultSetHandler:拦截结果集的处理 
    

    例:
    Mybatis实现自定义插件的示例分享

    Mybatis自定义插件必须实现Interceptor接口

    public interface Interceptor {
        Object intercept(Invocation invocation) throws Throwable;
        Object plugin(Object target);
        void setProperties(Properties properties);
    }
    intercept方法:拦截器具体处理逻辑方法 
    plugin方法:根据签名signatureMap生成动态代理对象 
    setProperties方法:设置Properties属性
    
    自定义插件demo
    
    // 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 {
      Object target = invocation.getTarget(); //被代理对象
      Method method = invocation.getMethod(); //代理方法
      Object[] args = invocation.getArgs(); //方法参数
      // do something ...... 方法拦截前执行代码块
      Object result = invocation.proceed();
      // do something .......方法拦截后执行代码块
      return result;
      }
      public Object plugin(Object target) {
        return Plugin.wrap(target, this);
      }
      public void setProperties(Properties properties) {
      }
    }
    一个@Intercepts
    可以配置多个@Signature,
                @Signature中的参数定义如下
    type:表示拦截的类,这里是Executor的实现类
    method:表示拦截的方法,这里是拦截Executor的update方法
    args:表示方法参数
  • 相关阅读:
    当Synchronized遇到这玩意儿,有个大坑,要注意
    ansible fetch 模块
    Python 生成器
    【数据结构Note5】-二叉排序树 BST和平衡二叉树AVL
    Spring Cloud Stream kafka项目启动时报错
    【笔记】电商RFM模型
    Python的NumPy库(二)进阶用法
    idea启动与jar包启动中使用resource资源文件路径问题总结
    111. 二叉树的最小深度
    GDB调试方法汇总
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/128093593