foreach标签能够对传入的list、array进行迭代
在对同一字段,使用多个不同的查询参数查询,使用这个标签有很好的效果。今天记录一下使用foreach分别对不同字段like和in的简单查询。
直接放Mapping
<select id="getChanceAnalysePC" parameterType="java.util.HashMap" resultType="java.util.HashMap">
with temp as ( select * from (select t.* ,row_number() over(partition by t.s_info_compname order by t.create_date desc) as rn from rpt_chance_analyse_datas t where t.create_date >sysdate -7) where rn=1)
select * from temp where 1=1
<if test="companyName !=null and companyName !='' ">
AND S_INFO_COMPNAME LIKE '%' || #{companyName} || '%'
</if>
<!--like部分,collection代表你的list、array在Map中的key;separator会在每次迭代加OR,注意AND和()的位置-->
<if test="productList !=null ">
AND
(
<foreach collection="productList" index="index" item="product" separator="OR" >
sort_flag like '%'||#{product}||'%'
</foreach>
)
</if>
<!--in部分,注意这个separator要用英文逗号,如果需要自己组装list,组装之前也别用中文逗号做分隔符,刚踩完-->
<if test="plateList !=null ">
AND boardname in
<foreach collection="plateList" index="index" item="plate" open="(" separator="," close=")" >
#{plate}
</foreach>
</if>
</select>