首页
/ MyBatis执行器Executor源码解析:SimpleExecutor、ReuseExecutor与BatchExecutor对比

MyBatis执行器Executor源码解析:SimpleExecutor、ReuseExecutor与BatchExecutor对比

2026-02-06 04:52:58作者:卓炯娓

MyBatis作为Java领域最流行的持久层框架,其核心执行器Executor承担着SQL语句执行的重任。本文通过深入分析MyBatis执行器源码,对比SimpleExecutor简单执行器、ReuseExecutor重用执行器和BatchExecutor批处理执行器的实现原理与适用场景,帮助开发者更好地理解和选择适合的执行器类型。🎯

执行器架构概览

MyBatis执行器体系采用经典的模板方法模式,位于 [src/main/java/org/apache/ibatis/executor/](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/?utm_source=gitcode_repo_files) 目录下。Executor接口定义了执行器的核心方法,包括查询、更新、提交、回滚等操作。

核心接口设计

[Executor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/Executor.java?utm_source=gitcode_repo_files) 是所有执行器的顶层接口,主要方法包括:

  • update() - 执行更新操作
  • query() - 执行查询操作,支持分页和缓存
  • flushStatements() - 刷新批处理语句
  • commit()/rollback() - 事务管理
  • createCacheKey() - 创建缓存键

基类实现

[BaseExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/BaseExecutor.java?utm_source=gitcode_repo_files) 作为抽象基类,实现了执行器的公共逻辑,包括:

  • 一级缓存管理 - 本地缓存防止循环引用
  • 延迟加载机制 - 线程安全的延迟加载队列
  • 事务处理 - 基础的事务管理功能

SimpleExecutor简单执行器解析

核心实现原理

[SimpleExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java?utm_source=gitcode_repo_files) 是最基础的执行器实现,其核心思想是每次执行SQL都创建新的Statement对象

执行流程分析

  1. 创建StatementHandler - 根据配置创建对应的语句处理器
  2. 准备语句 - 调用 prepareStatement() 方法
  3. 执行操作 - 根据操作类型调用 update()query() 方法
  4. 资源释放 - 在finally块中确保Statement正确关闭

适用场景

SimpleExecutor适合单次查询场景,特别是需要立即释放数据库连接的应用环境。

ReuseExecutor重用执行器解析

缓存机制设计

[ReuseExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/ReuseExecutor.java?utm_source=gitcode_repo_files) 通过内部维护的 statementMap 实现Statement的复用。

性能优化策略

  1. SQL语句匹配 - 通过SQL字符串作为键值
  2. 连接状态检查 - 确保缓存的Statement连接未关闭
  3. 智能缓存管理 - 相同的SQL语句复用相同的Statement对象

优势对比

  • 减少对象创建 - 避免频繁创建Statement对象
  • 提升执行效率 - 复用已编译的SQL语句

BatchExecutor批处理执行器解析

批量处理架构

[BatchExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/BatchExecutor.java?utm_source=gitcode_repo_files) 专为大批量数据操作设计。

关键特性

  1. 批量收集 - 将多个SQL语句收集到Statement列表中
  2. 批量执行 - 通过 executeBatch() 一次性提交
  3. 异常处理 - 完善的BatchUpdateException处理机制

三种执行器性能对比分析

内存使用情况

执行器类型 内存占用 Statement创建频率
SimpleExecutor 每次执行
ReuseExecutor 中等 首次执行
BatchExecutor 批量收集后执行

执行效率对比

  • SimpleExecutor - 适合单次操作,资源释放及时
  • ReuseExecutor - 适合重复SQL场景,减少编译开销
  • BatchExecutor - 适合大批量插入更新,显著提升性能

实际应用选择指南

开发环境推荐

  • 测试环境 - SimpleExecutor(调试方便)
  • 开发环境 - ReuseExecutor(性能与稳定性的平衡)
  • 生产环境 - 根据业务场景选择合适执行器

配置方法

在MyBatis配置文件中通过 executorType 参数指定执行器类型:

<settings>
  <setting name="executorType" value="REUSE"/>
</settings>

源码学习建议

核心文件清单

  • [Executor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/Executor.java?utm_source=gitcode_repo_files) - 接口定义
  • [BaseExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/BaseExecutor.java?utm_source=gitcode_repo_files) - 基类实现
  • [SimpleExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java?utm_source=gitcode_repo_files) - 简单执行器
  • [ReuseExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/ReuseExecutor.java?utm_source=gitcode_repo_files) - 重用执行器
  • [BatchExecutor.java](https://gitcode.com/gh_mirrors/my/mybatis/blob/0483a1dcbe92e2d226ab045420f41a85e01d6c04/src/main/java/org/apache/ibatis/executor/BatchExecutor.java?utm_source=gitcode_repo_files) - 批处理执行器

总结与展望

通过深入分析MyBatis三种执行器的源码实现,我们可以清晰地看到每种执行器的设计理念和适用场景。SimpleExecutor简单直接,ReuseExecutor性能优化,BatchExecutor批量高效。在实际项目开发中,根据具体的业务需求和性能要求,选择合适的执行器类型是优化数据库操作性能的关键。🚀

掌握这些执行器的原理和区别,不仅能够帮助我们更好地理解MyBatis的工作机制,还能在遇到性能问题时快速定位和优化。希望本文能为你的MyBatis学习之旅提供有价值的参考!

登录后查看全文
热门项目推荐
相关项目推荐

项目优选

收起
atomcodeatomcode
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get Started
Rust
438
78
docsdocs
暂无描述
Dockerfile
690
4.46 K
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
407
326
pytorchpytorch
Ascend Extension for PyTorch
Python
549
671
kernelkernel
deepin linux kernel
C
28
16
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.59 K
925
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
955
930
communitycommunity
本项目是CANN开源社区的核心管理仓库,包含社区的治理章程、治理组织、通用操作指引及流程规范等基础信息
650
232
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.08 K
564
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
C
436
4.43 K