Deep Dive into MyBatis selectList() Execution Flow with MyBatis 3.5 and Spring Boot 2.3.3
This article thoroughly examines the MyBatis selectList() execution process—including environment setup, mapper definitions, caching executors, parameter handling, statement preparation, and result set processing—by stepping through source code and debugging traces within a Spring Boot 2.3.3 context.
This third article in a MyBatis source‑code series focuses on the selectList() method, assuming MyBatis 3.5 and Spring Boot 2.3.3 as the runtime environment.
The mapper interface is defined as List<UserInfo> selectList(@Param("userIds") List<String> userIds) , with a corresponding XML mapper that enables second‑level cache, declares a select statement, and uses a <foreach> to build the IN clause.
List<UserInfo> selectList(@Param("userIds") List<String> userIds); <mapper namespace="cn.cb.demo.dao.UserMapper">
<!--开启二级缓存-->
<cache/>
<select id="selectList" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where status=1
and user_id in
<foreach collection="userIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>A simple JUnit test calls the mapper with two user IDs and prints the returned list.
@Test
void contextLoads() {
List<UserInfo> userInfos = userMapper.selectList(Arrays.asList("192", "198"));
System.out.println(userInfos);
}During debugging, Spring Boot injects SqlSessionTemplate , which forwards the call to DefaultSqlSession.selectList . The actual execution then passes through CachingExecutor.query , which first checks the second‑level cache, then delegates to BaseExecutor.query (first‑level cache), and finally to SimpleExecutor.doQuery for a database round‑trip.
The query method ultimately invokes prepareStatement(handler, ms.getStatementLog()) , bringing into play ParameterHandler and TypeHandler to set JDBC parameters. The DefaultParameterHandler.setParameters method iterates over all parameters, uses the appropriate TypeHandler to convert each value, and calls TypeHandler.setParameter .
After parameters are set, PreparedStatementHandler.query executes the SQL via JDBC and hands the ResultSet to DefaultResultSetHandler.handleResultSets . This handler obtains the result map, iterates over rows, applies TypeHandler.getResult for type conversion, and assembles the final list of UserInfo objects.
In summary, a selectList call follows the chain: SqlSessionTemplate → DefaultSqlSession → CachingExecutor → BaseExecutor → SimpleExecutor → ParameterHandler → PreparedStatementHandler → DefaultResultSetHandler , with caching, parameter preparation, and result mapping all orchestrated by MyBatis’s six core components.
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.