在sqlsession中主要是执行都是通过executor来处理的:
executor.query(ms, wrapCollection(parameter), rowBounds, handler);
executor.update(ms, wrapCollection(parameter));
执行器从哪里来呢?在
DefaultSqlSessionFactory里面openSessionFromDataSource:
final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
看configuration里面怎么构建执行器的:
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
<span style="color:#ff0000;">executor = new CachingExecutor(executor, autoCommit);</span>
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
可以看到,默认是是简单执行器,还有批量、重用执行器,下面这段话解释不用的执行器使用方式:
executor = (Executor) interceptorChain.pluginAll(executor);
是将执行器添加到拦截器中,这个很重要,为后续写插件提供时机。
具体为session指定执行器可以使用类似这种方式
sessionFactory.openSession(ExecutorType.BATCH, false);