1、传递数值
<select id="selectStudentByConditionByForeach_array" resultType="Student">
<!-- select id,name,age,score from tb_student where id in (1,3,5) -->
select id,name,age,score
from tb_student
<if test="array.length > 0">
where id in
<foreach collection="array" item="myId" open="(" close=")" separator=",">
#{myId}
</foreach>
</if>
</select>
2、传递List
<select id="selectStudentByConditionByForeach_List" resultType="Student">
<!-- select id,name,age,score from tb_student where id in (1,3,5) -->
select id,name,age,score
from tb_student
<if test="list.size > 0">
where id in
<foreach collection="list" item="myId" open="(" close=")" separator=",">
#{myId}
</foreach>
</if>
</select>
3、
<select id="selectStudentByConditionByForeach_Genericity" resultType="Student">
<!-- select id,name,age,score from tb_student where id in (1,3,5) -->
select id,name,age,score
from tb_student
<if test="list.size > 0">
where id in
<foreach collection="list" item="stu" open="(" close=")" separator=",">
#{stu.id}
</foreach>
</if>
</select>