6. interceptorChain

2020-01-15  本文已影响0人  元隐

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();
}

详情请参见分页插件源码

上一篇下一篇

猜你喜欢

热点阅读