MyBatis源码-解读Executor的三个实现类之BatchExecutor(批处理执行器)
文章目錄
- Pre
- Executor 執(zhí)行器
- 接口繼承關(guān)系
- BatchExecutor(重用執(zhí)行器)
- 入門小demo
- 源碼
- BatchExecutor VS ReuseExecutor
Pre
MyBatis源碼-深入理解MyBatis Executor的設(shè)計(jì)思想
工程部分見
MyBatis源碼- SqlSession門面模式 & selectList 源碼解析
實(shí)際中,我們都是面向SqlSession編程的,不會(huì)直接調(diào)用Executor來執(zhí)行業(yè)務(wù)邏輯,這里我們僅僅是為了深入了解下Executor體系架構(gòu)才這么搞的,切記。
Executor 執(zhí)行器
接口繼承關(guān)系
這里我們重點(diǎn)看下Executor的 三個(gè)實(shí)現(xiàn)子類。
分別是:SimpleExecutor(簡(jiǎn)單執(zhí)行器)、ReuseExecutor(重用執(zhí)行器)、BatchExecutor(批處理執(zhí)行器)。
BatchExecutor(重用執(zhí)行器)
BatchExecutor 僅對(duì)修改操作(包括刪除)有效哈 ,對(duì) select操作是不起作用。
BatchExecutor 主要是用于做批量更新操作的 ,底層會(huì)調(diào)用Statement的 executeBatch()方法實(shí)現(xiàn)批量操作
入門小demo
@Testpublic void testBatchExecutor() throws SQLException {// 通過factory.openSession().getConnection()實(shí)例化JdbcTransaction ,用于構(gòu)建BatchExecutorjdbcTransaction = new JdbcTransaction(factory.openSession().getConnection());// 實(shí)例化BatchExecutorBatchExecutor executor = new BatchExecutor(configuration, jdbcTransaction);// 映射SQLms = configuration.getMappedStatement("com.artisan.UserMapper.updateById");Map map = new HashMap();map.put("arg0",1);map.put("arg1","222");// 調(diào)用doUpdateexecutor.doUpdate(ms,map);executor.doUpdate(ms,map);// 刷新executor.doFlushStatements(false);// 提交 否則不生效executor.commit(true);}源碼
currentSql 全局變量, 非線程安全
statementList 緩存 statement
batchResultList 緩存 返回結(jié)果
BatchExecutor VS ReuseExecutor
看輸出 和 ReuseExecutor 感覺差不多,其實(shí)是有區(qū)別的
-
ReuseExecutor : 設(shè)置參數(shù),執(zhí)行,獲取返回結(jié)果,然后在設(shè)置參數(shù),執(zhí)行,獲取返回結(jié)果
-
BatchExecutor: 批量設(shè)置參數(shù) , 執(zhí)行 ,獲取返回結(jié)果。
BatchExecutor僅執(zhí)行一次,ReuseExecutor 執(zhí)行多次
總結(jié)
以上是生活随笔為你收集整理的MyBatis源码-解读Executor的三个实现类之BatchExecutor(批处理执行器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis源码- SqlSessio
- 下一篇: JVM - 应用JVM核心参数推荐设置