Backend Development 13 min read

Deep Dive into MyBatis Core Components: Executor, StatementHandler, ParameterHandler, ResultSetHandler and Batch Operations

This article explains how MyBatis delegates SQL execution to four core objects—Executor, StatementHandler, ParameterHandler, and ResultSetHandler—detailing their architecture, implementations such as SimpleExecutor, ReuseExecutor, BatchExecutor, and demonstrating three common batch insertion techniques with code examples.

Architect's Guide
Architect's Guide
Architect's Guide
Deep Dive into MyBatis Core Components: Executor, StatementHandler, ParameterHandler, ResultSetHandler and Batch Operations

Before executing SQL in MyBatis, a SqlSession object is obtained, but the real work is performed by four core components: Executor , StatementHandler , ParameterHandler , and ResultSetHandler . The article first outlines the overall MyBatis layered architecture, consisting of the interface layer (SqlSession), the core processing layer (the four objects), and the support layer.

Executor is the component that actually runs SQL statements. It follows the Template Method pattern: the top‑level Executor interface defines the contract, while the abstract class BaseExecutor implements common operations (caching, transaction handling) and declares four abstract methods— doFlushStatements() , doQuery() , doQueryCursor() , and doUpdate() . The defaultExecutorType setting in MyBatis configuration can be SIMPLE , REUSE , or BATCH , which correspond to the concrete subclasses SimpleExecutor , ReuseExecutor , and BatchExecutor .

SimpleExecutor provides the most straightforward implementation without any special logic. For example, its doQuery() method simply creates a Statement and executes the query:

public List
doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { /* simple query logic */ }

ReuseExecutor improves performance by caching Statement objects. The prepareStatement method first checks a HashMap cache before creating a new Statement :

protected Statement prepareStatement(Connection connection, MappedStatement ms, Object parameterObject, BoundSql boundSql) { Statement stmt = statementCache.get(sql); if (stmt == null) { stmt = connection.createStatement(); statementCache.put(sql, stmt); } return stmt; }

BatchExecutor supports batch operations (insert, update, delete). It groups identical SQL patterns and reuses a single Statement for multiple parameter sets. The article shows a typical batch‑insert mapper method and its XML definition using the <foreach> tag:

int batchInsert(List
userAddresses);
<insert id="batchInsert"> insert into lw_user_address (address) values <foreach collection="list" item="item" separator=","> (#{item.address}) </foreach> </insert>

It also demonstrates how to open a session with ExecutorType.BATCH and how the generated SQL statements are consolidated.

Other executor types such as ClosedExecutor (used internally for lazy loading) and CachingExecutor (related to second‑level caching) are mentioned briefly.

StatementHandler is responsible for creating and configuring JDBC statements. Its hierarchy mirrors the executor hierarchy and includes RoutingStatementHandler (routing to the appropriate concrete handler), BaseStatementHandler (abstract base), SimpleStatementHandler (non‑prepared statements), PreparedStatementHandler (prepared statements with placeholders, preventing SQL injection), and CallableStatementHandler (for stored procedures).

ParameterHandler handles setting parameters on prepared statements. The default implementation DefaultParameterHandler provides two methods: one to retrieve the parameter object and another to set the parameters on the PreparedStatement .

ResultSetHandler processes the results returned by the database. The default implementation DefaultResultSetHandler maps result sets to Java objects via the handleResultSets method.

In summary, the article emphasizes that SqlSession is merely a façade; understanding the four core objects and their interactions—especially the executor implementations and batch insertion techniques—provides a solid foundation for mastering MyBatis’s execution flow and later exploring its plugin mechanism.

JavaBackend DevelopmentMyBatisORMExecutorBatch Operations
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.