您的位置:首页 > 其它

读mybatis源码之五:执行器Executor创建

2014-06-21 20:22 246 查看
         在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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 源码