导读
本文通过MyBatis一个低版本的bug(3.4.5之前的版本)入手,分析MyBatis的一次完整的查询流程,从配置文件的解析到一个查询的完整执行过程详细解读MyBatis的一次查询流程,通过本文可以详细了解MyBatis的一次查询过程。在平时的代码编写中,发现了MyBatis一个低版本的bug(3.4.5之前的版本),由于现在很多工程中的版本都是低于3.4.5的,因此在这里用一个简单的例子复现问题,并且从源码角度分析MyBatis一次查询的流程,让大家了解MyBatis的查询原理。
1 问题现象
1.1 场景问题复现
如下图所示,在示例Mapper中,下面提供了一个方法queryStudents,从student表中查询出符合查询条件的数据,入参可以为student_name或者student_name的集合,示例中参数只传入的是studentName的List集合
List<string> studentNames = new LinkedList<>();
studentNames.add("lct");
studentNames.add("lct2");
condition.setStudentNames(studentNames);
</string>
<select id="queryStudents" parametertype="mybatis.StudentCondition" resultmap="resultMap">
select * from student
<where>
<if test="studentNames != null and studentNames.size > 0 ">
AND student_name IN
<foreach collection="studentNames" item="studentName" open="(" separator="," close=")">
#{studentName, jdbcType=VARCHAR}
</foreach>
</if>
<if test="studentName != null and studentName != '' ">
AND student_name = #{studentName, jdbcType=VARCHAR}
</if>
</where>
</select>
期望运行的结果是
select * from student WHERE student_name IN ( 'lct' , 'lct2' )
但是实际上运行的结果是
==> Preparing: select * from student WHERE student_name IN ( ? , ? ) AND student_name = ?
==> Parameters: lct(String), lct2(String), lct2(String)
Original: https://www.cnblogs.com/Jcloud/p/16744140.html
Author: 京东云开发者
Title: 源码学习之MyBatis的底层查询原理
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/561994/
转载文章受原作者版权保护。转载请注明原作者出处!