MyBatis and MyBatis-Plus Source Code Reading: From SqlSession to Plugin Chain
Baseline: MyBatis 3.5.x, MyBatis-Plus 3.5.x, Spring Boot 3, Java 17+.
1. Native Execution Chain
Mapper proxy -> SqlSession -> Executor -> StatementHandler -> JDBC -> ResultSetHandler
SqlSession is the entry point. Executor handles queries, updates, transactions, and first-level caching. StatementHandler manages parameters and Statements. ResultSetHandler handles result mapping. MyBatis-Plus adds BaseMapper, Wrapper, automatic filling, logical deletion, pagination, and interceptors, but doesn't change JDBC transaction constraints.
2. Parameter Binding
<select id="findByStatus" resultType="Order">
select * from orders where status = #{status}
</select>
String substitution can only be used for whitelisted table names, column names, and sorting fields—user input must never be directly concatenated into SQL.
3. Executor and Caching
SimpleExecutor creates a Statement each time; ReuseExecutor reuses Statements; BatchExecutor sends batched updates; CachingExecutor handles namespace second-level caching. First-level caching lives within the SqlSession/Executor lifecycle, while second-level caching involves serialization and invalidation consistency—it shouldn't be blindly treated as a Redis cache.
4. Plugin Chain
Pagination, optimistic locking, tenant, and data permissions execute through the plugin chain. Plugin order, tenant conditions, pagination, and logical deletion must be verified with integration tests. Logical deletion doesn't mean data stops growing. When upgrading, rely on locked source code versions.