1.代码
<select id="getStudents" resultType="student" parameterType="map">
select * from mybatis_test.student
<where>
<foreach collection="ids" item="id" index="index" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
2.代码讲解
- foreach标签的collection属性是对应map参数中的一个值为集合的键。
- item是集合中的每一个元素
- index是集合中的每一个元素的索引
- open,close属性的值在这个foreach语句前面和后面添加
- separator的值将集合中的元素分开
- 例如ids=[1,2,3]那么上述foreach生成的sql语句就是:
(id=1,id=2,id=3)