6. interceptorChain

mybatis 的interceptorChain是给扩展用的, 常用的比如分页扩展插件. mybatis提供的rowbounds实质上是采用 fetchSize, rs.next 这种方式. 其性能很差. (实际上就是游标, .next() 一点点向下滚动)

其采用了反射,一个典型的Interceptor

@Intercepts({
        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class }),
        @Signature(type = Executor.class, method = "close", args = { boolean.class }) })
public class MyBatisInterceptor implements Interceptor {

    private Integer value;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        //do something
        
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        //do something
    }

}

比如分页插件可以通过增强以下接口实现

public interface StatementHandler {
    <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException;

    BoundSql getBoundSql();
}

详情请参见分页插件源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近在看OkHttp源码时,看到了Interceptor的使用,觉得还是很巧妙的设计,所以就提炼出来,已被不时之需...
    Rocky1982阅读 2,516评论 2 5
  • 转载自:https://www.cnblogs.com/easypass/archive/2010/12/08/1...
    SkTj阅读 554评论 0 1
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,636评论 0 4
  • 一. Java基础部分.................................................
    wy_sure阅读 3,837评论 0 11
  • 《献给命运的紫罗兰》是刘心武先生写的一本散文集,里面还收录了他的两册散文《人生非梦总难醒》和《献给命运的紫罗兰》,...
    维绮微雨阅读 885评论 0 4