MyBatis执行器Executor源码解析:SimpleExecutor、ReuseExecutor与BatchExecutor对比
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对象。
执行流程分析
- 创建StatementHandler - 根据配置创建对应的语句处理器
- 准备语句 - 调用
prepareStatement()方法 - 执行操作 - 根据操作类型调用
update()或query()方法 - 资源释放 - 在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的复用。
性能优化策略
- SQL语句匹配 - 通过SQL字符串作为键值
- 连接状态检查 - 确保缓存的Statement连接未关闭
- 智能缓存管理 - 相同的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) 专为大批量数据操作设计。
关键特性
- 批量收集 - 将多个SQL语句收集到Statement列表中
- 批量执行 - 通过
executeBatch()一次性提交 - 异常处理 - 完善的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学习之旅提供有价值的参考!
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00