一.问题描述
1.举个栗子
入参:userIdList=[75,76];id=null
<select id="selectUserId" resultType="java.lang.Long">
select
user_id
from
user_student_info
WHERE
deleted = 0
<if test="userIdList !=null and userIdList.size()>0">
and user_id in
<foreach collection="userIdList" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="id!=null">
and gradeId = #{id}
</if>
</select>
预期运行sql应该不会有and gradeId =
用mybatis sql log工具查看运行sql如下:【与预期不符合】
select user_student_info_id
FROM user_student_info
WHERE deleted = 0 and user_id in ( 75 , 76 ) and gradeId = 76;
将<foreach>中的item="id"改为item="item"就不会有and gradeId =
二.总结
在foreach标签item取名时避免和入参名一致,index的名字也是一样
原文地址:https://blog.csdn.net/queshuihaimian/article/details/98489239