Debugging MyBatis Source Code: A Step‑by‑Step Guide to Reading Core Components
This article explains how to set up the environment, identify key MyBatis components such as SqlSession, Executor, StatementHandler, ParameterHandler, TypeHandler, and ResultSetHandler, and strategically place breakpoints to effectively read and understand MyBatis 3.5 source code within a Spring Boot 2.3.3 application.
The article begins by reminding readers to follow the author for original content and references a previous post that introduced MyBatis's six essential components. It then explains that the tutorial is based on MyBatis 3.5 and Spring Boot 2.3.3.
To start debugging, the author suggests enabling DEBUG mode, setting breakpoints on important methods, and focusing on the selectList() query method as an example.
SqlSession : Since SqlSession is an interface, the implementation classes DefaultSqlSession and SqlSessionTemplate are examined. The Spring Boot auto‑configuration injects SqlSessionTemplate, which delegates to DefaultSqlSession. The relevant bean definition is shown below:
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}Key breakpoints are placed on the constructor of SqlSessionTemplate, the selectList() method in DefaultSqlSession, and the getMapper() call.
Executor : The auto‑configuration creates a CachingExecutor as the default executor. The article highlights the query() method, which first checks the second‑level cache and, if missing, delegates to the underlying executor. The relevant code snippet is:
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
// try cache first
Cache cache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
if (ms.isUseCache() && resultHandler == null) {
ensureNoOutParams(ms, boundSql);
@SuppressWarnings("unchecked")
List<E> list = (List<E>) tcm.getObject(cache, key);
if (list == null) {
list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
tcm.putObject(cache, key, list); // issue #578 and #116
}
return list;
}
}
// no cache, call delegate directly
return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}The delegate is identified as an instance of SimpleExecutor, and breakpoints are suggested on its methods.
StatementHandler : This interface handles statement execution and parameter setting. Its main implementation is PreparedStatementHandler, where breakpoints should be added.
ParameterHandler : The interface has two methods for setting and retrieving parameters. The implementation class DefaultParameterHandler is the target for debugging.
TypeHandler : This simple interface also has two methods for converting parameters and results. Readers are encouraged to locate the appropriate type handler for the data type they are interested in and set breakpoints there.
ResultSetHandler : Responsible for processing query results, its primary implementation is DefaultResultSetHandler, where all relevant methods should be examined.
In the conclusion, the author emphasizes the importance of learning how to read source code rather than just following analyses, noting that the same approach applies to other frameworks such as Spring. Readers are invited to follow the author for more tutorials.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
