foreach 元素中主要属性有:
- item:取出值迭代时使用的变量名;
- index:迭代位置类似于下标;
- collection:取值的对象;
- open:表示sql语句的第一个位置拼接;
- separator:迭代值时使用的拼接符;
- close:表示sql语句的最后一个位置拼接;
例1:当参数为基本类型List或者数组Array时。
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
例2:当参数为拼接字符串时,需要拆分。
<foreach collection="ids.split(',')" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
例3:单参数为对象List时。
<foreach collection="phoneList" open="(" close=")" separator="," item="phone" index="index">
#{phone.productName}
</foreach>