讀mybatis源碼之五:執行器Executor創建

         在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);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章