Skip to main content

MyBatis 3.5 Source Analysis Part 2: Executor and Statement Handling

· One min read
Apache Wangye
Software developer and technical writer

After a mapper method resolves its MappedStatement, MyBatis delegates database work to an Executor.

SimpleExecutor creates a statement for each operation, ReuseExecutor reuses prepared statements, and BatchExecutor batches updates. CachingExecutor decorates another executor to provide second-level cache behavior.

The executor obtains a BoundSql object containing the final SQL, parameter mappings, and additional dynamic-SQL values. A StatementHandler prepares the JDBC statement, a ParameterHandler binds values through type handlers, and a ResultSetHandler converts returned rows into Java objects.

Plugins can intercept selected methods through MyBatis proxy wrapping. Interceptors are powerful but can change ordering, parameters, SQL, and result behavior across the application, so keep them narrow and tested.

The local first-level cache belongs to a SqlSession; the optional second-level cache is associated with a mapper namespace. Updates normally clear relevant cache state. Trace one query through these components to understand pagination, generated keys, nested mappings, lazy loading, and common parameter-binding errors.

Page views: --

Total views -- · Visitors --