DefaultSqlSessionFactory

final Executor executor = configuration.newExecutor(tx, execType);

通过Configuration创建一个executor

Configuration

executor = (Executor) interceptorChain.pluginAll(executor);

动态代理executor (target)

InterceptorChain

=pluginAll(Object target) target是一个executor

for (Interceptor interceptor : interceptors/** 这个数组是插件的数组,所有插件都装到了这个数组*/) {

target = interceptor.plugin(target);一直生成,生成了一个又放进去生成,直到生成完毕,

}

interceptor(pagehelper)

@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))

生成的代理类有一个signatureMap以type = Executor.class为key,其他method,args为value

@Override

public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

Plugin.wrap(target, this)生成一个代理类

Plugin 插件接口

Plugin impl InvocationHandler

Plugin实现了InvocationHandler

=wrap()

生成代理类

if (interfaces.length > 0) {   return Proxy.newProxyInstance(       type.getClassLoader(),       interfaces,       new Plugin(target, interceptor, signatureMap)); } return target; 

=执行代理类

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

try {

Set methods = signatureMap.get(method.getDeclaringClass()); //mybatis就是通过signatureMap来判断是否要执行的

if (methods != null && methods.contains(method)) {

return interceptor.intercept(new Invocation(target, method, args));

}

return method.invoke(target, args);

} catch (Exception e) {

throw ExceptionUtil.unwrapThrowable(e);

}

}

return interceptor/** pageHelper*/.intercept(new Invocation(target, method, args));

走pagehelper的逻辑,

DefaultSqlSession

private Executor executor;

它里面有一个Executor,通过构造函数传进去。

List result = executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);

executor如果是有插件的话,这个executor已经是代理类了

Executor

有包含了一个plugin(Pagehelper)

List result = executor.query(ms, wrapCollection(parameter),

执行这一句,这里已经被它增强过了,走的是pagehelper的逻辑

也就是在生成executor的时候会生成插件代理