1.主要用到了一个foreach标签实现sql条件的循环,完成类似批量的sql。
foreach是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item:表示集合中每一个元素进行迭代时的别名
index:用于表示在迭代过程中,每次迭代到的位置
collection:指定传入参数的类型
open:开始时拼接的字符串
separator:表示在每次进行迭代之间以什么符号作为分隔符
close:结束时拼接的字符串
(1)传入参数为list:
List<FshEstate> getEstateByEstateId(List<String> estateIds);
<select id="getEstateByEstateId" parameterType="java.util.List" resultType="com.model.mpublic.FshEstate">
SELECT *
FROM fsh_estate est
WHERE est.estate_id in
<foreach collection="list" item="estateId" index="index"
open="(" close=")" separator=",">
#{estateId}
</foreach>
</select>
(2)传入参数为数组:
List<FshEstate> getEstateByEstateId(String[] estateIds);
<select id="getEstateByEstateId" parameterType="java.util.List" resultType="com.model.mpublic.FshEstate">
SELECT *
FROM fsh_estate est
WHERE est.estate_id in
<foreach collection="array" item="estateId" index="index"
open="(" close=")" separator=",">
#{estateId}
</foreach>
</select>
注意:
(1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .