哎哟,不错哦

导航

mybatis foreach 遍历list中的坑

将jdbc改写为mybatis时,传入的条件为list使用到的标签是<where> 、<choose>、<when>、<if>、<foreach>因为判断list集合时判断条件不全,导致sql执行错误,下面是正确的判断条件

<where>
  <choose>
    <when test="unitList != null and ! unitList.isEmpty() and unitList.size() > 0">
    (tab2.id IN
      <foreach collection="unitList" item="item" index="index"
      open="(" separator="," close=")">
      #{item}
      </foreach>
      AND tab1.`status` = #{deviceStatus})
    <if test="zoonList != null and ! zoonList.isEmpty() and zoonList.size() > 0">
      OR (tab2.leaderId IN
        <foreach collection="zoonList" item="item" index="index"
        open="(" separator="," close=")">
        #{item}
        </foreach>
      AND tab1.`status` = #{deviceStatus})
    </if>
  </when>
  <when test="zoonList != null and ! zoonList.isEmpty() and zoonList.size() > 0">
    tab2.leaderId IN
    <foreach collection="zoonList" item="item" index="index"
      open="(" separator="," close=")">
      #{item}
     </foreach>
    AND tab1.`status` = #{deviceStatus}
  </when>
  </choose>
</where>

 

posted on 2018-11-14 17:49  哎哟,不错哦  阅读(2106)  评论(0编辑  收藏  举报