【mybatis源码学习】mybatis的插件功能

1、mybatis的插件功能可拦截的目标

org.apache.ibatis.executor.parameter.ParameterHandler
org.apache.ibatis.executor.resultset.ResultSetHandler
org.apache.ibatis.executor.statement.StatementHandler
org.apache.ibatis.executor.Executor

2、Mybatis的插件功能接入步骤

一、实现接口:org.apache.ibatis.plugin.Interceptorsql

二、实现类上须要添加注解@Intercepts和@Signature 用于描述要进行拦截的类接口和方法数据库

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Intercepts {
  Signature[] value(); //要拦截的方法信息描述
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Signature {
  Class<?> type(); //要拦截的类的描述(接口)

  String method();//要拦截的方法名

  Class<?>[] args();//要拦截的方法名的参数列表的类型
}
View Code

三、将实现的Interceptor的类的对象,注入到Configuration的interceptorChain中apache

3、编写一个打印当前执行sql语句的插件案例

一、分析

依据mybatis的执行计划,StatementHandler 有一个方法 BoundSql getBoundSql(),其调用时机是向数据库申请链接,并向sql语句绑定参数时,从其内部获取sql语句。mybatis

 

故插件须要对StatementHandler进行拦截,并在其执行 getBoundSql语句时,从返回结果中获取sql语句,并打印框架

二、注意点

mybatis的插件功能使用的前提是对mybatis框架很是熟悉。ide

三、案例

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

    /**
     * 决定那个对象须要进行代理拦截
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        if(target instanceof StatementHandler){
            return Plugin.wrap(target, this);
        }
        return target;
    }

    /**
     * 执行代理的逻辑加强
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
         Object obj=invocation.proceed();
         RoutingStatementHandler handler= (RoutingStatementHandler) invocation.getTarget();
         System.out.println("current do sql=["+handler.getBoundSql().getSql()+"]");
        return obj;
    }

    /**
     * 设置当前代理的配置
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {

    }
}
View Code

 

interceptorChain
相关文章
相关标签/搜索