Mybatis的foreach标签,使用Map传参,完成like和in的查询功能

    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>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,917评论 0 38
  • 下午,穿过人群,走在路上。突然,有人递给我一张宣传单,本来想拒绝,可看到挡住了我的视线,于是接了过来。 这是一张幼...
    阿静_beaa阅读 333评论 0 1
  • 海伦.凯勒是美国著名的女作家、教育家、慈善家、社会活动家。海伦·凯勒于1880年6月27日出生在亚拉巴马...
    六二班郭嘉伟阅读 245评论 0 0