MyBatis源码-解读Executor的三个实现类之BatchExecutor(批处理执行器)
文章目錄
- Pre
- Executor 執行器
- 接口繼承關系
- BatchExecutor(重用執行器)
- 入門小demo
- 源碼
- BatchExecutor VS ReuseExecutor
Pre
MyBatis源碼-深入理解MyBatis Executor的設計思想
工程部分見
MyBatis源碼- SqlSession門面模式 & selectList 源碼解析
實際中,我們都是面向SqlSession編程的,不會直接調用Executor來執行業務邏輯,這里我們僅僅是為了深入了解下Executor體系架構才這么搞的,切記。
Executor 執行器
接口繼承關系
這里我們重點看下Executor的 三個實現子類。
分別是:SimpleExecutor(簡單執行器)、ReuseExecutor(重用執行器)、BatchExecutor(批處理執行器)。
BatchExecutor(重用執行器)
BatchExecutor 僅對修改操作(包括刪除)有效哈 ,對 select操作是不起作用。
BatchExecutor 主要是用于做批量更新操作的 ,底層會調用Statement的 executeBatch()方法實現批量操作
入門小demo
@Testpublic void testBatchExecutor() throws SQLException {// 通過factory.openSession().getConnection()實例化JdbcTransaction ,用于構建BatchExecutorjdbcTransaction = new JdbcTransaction(factory.openSession().getConnection());// 實例化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");// 調用doUpdateexecutor.doUpdate(ms,map);executor.doUpdate(ms,map);// 刷新executor.doFlushStatements(false);// 提交 否則不生效executor.commit(true);}源碼
currentSql 全局變量, 非線程安全
statementList 緩存 statement
batchResultList 緩存 返回結果
BatchExecutor VS ReuseExecutor
看輸出 和 ReuseExecutor 感覺差不多,其實是有區別的
-
ReuseExecutor : 設置參數,執行,獲取返回結果,然后在設置參數,執行,獲取返回結果
-
BatchExecutor: 批量設置參數 , 執行 ,獲取返回結果。
BatchExecutor僅執行一次,ReuseExecutor 執行多次
總結
以上是生活随笔為你收集整理的MyBatis源码-解读Executor的三个实现类之BatchExecutor(批处理执行器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis源码- SqlSessio
- 下一篇: 设计模式 -结构型模式_ 装饰者模式De