Mybatis有三種基本的Executor執(zhí)行器:
? ? ? ? ? SimpleExecutor、ReuseExecutor、BatchExecutor。
SimpleExecutor:每執(zhí)行一次update或select,就開啟一個(gè)Statement對(duì)象,用完立刻關(guān)閉Statement對(duì)象。
ReuseExecutor:執(zhí)行update或select,以sql作為key查找Statement對(duì)象,存在就使用,不存在就創(chuàng)建,用完后,不關(guān)閉Statement對(duì)象,而是放置于Map內(nèi),供下一次使用。簡言之,就是重復(fù)使用Statement對(duì)象。
BatchExecutor:執(zhí)行update(沒有select,JDBC批處理不支持select),將所有sql都添加到批處理中(addBatch()),等待統(tǒng)一執(zhí)行(executeBatch()),它緩存了多個(gè)Statement對(duì)象,每個(gè)Statement對(duì)象都是addBatch()完畢后,等待逐一執(zhí)行executeBatch()批處理。與JDBC批處理相同。
作用范圍:Executor的這些特點(diǎn),都嚴(yán)格限制在SqlSession生命周期范圍內(nèi)。
Mybatis中如何指定使用哪一種Executor執(zhí)行器?
答:在Mybatis配置文件中,可以指定默認(rèn)的ExecutorType執(zhí)行器類型,也可以手動(dòng)給DefaultSqlSessionFactory的創(chuàng)建SqlSession的方法傳遞ExecutorType類型參數(shù)。