<!-- 数组遍历 -->
<select id="queryUserByIDArray" resultType="User" parameterType="int[]">
select * from user
<where>
<if test="array != null and array.length > 0">
<foreach collection="array" open="and id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
对
<where></where>
里面的东西一一分解1 .
<if test="array != null and array.length > 0">
这是对array数组进行判断
2.<foreach collection="array" open="and id in(" close=")" item="id" separator=",">
collection=“类型名称”,必须得是==“array“==才行
open和close其实就是对你where语句后面的分割 ,将其合并在一起就是and id in( xx , xx ),separator中是分隔符标志-----”,“
item 通俗理解为in(xx,xx)中的xx
<!-- 集合遍历 -->
<select id="queryUserByIDList" resultType="User" parameterType="list">
select * from user
<where>
<!-- 数组里面需要用list代替测试类里面的参数ids -->
<if test="list != null and list.size > 0">
<foreach collection="list" open="and id in(" close=")" item="o" separator=",">
#{o}
</foreach>
</if>
</where>
</select>
上面表述的是集合的遍历,几乎跟数组没有什么区别,将 parameterType和collection中的数值改为list即可
<!--
对象数组遍历
传参的数组是Objet
-->
<select id="queryUserByIDObArray" resultType="User" parameterType="Object[]">
select * from user
<where>
<if test="array != null and array.length > 0">
<foreach collection="array" open="and id in(" close=")" item="user" separator=",">
#{user.id}
</foreach>
</if>
</where>
</select>
将 parameterType和collection中的数值改为arry即可
- 为什么要用Object[]
答:因为输入的对象数组中有可能包含不同类型的数据