foreach遍历LIST读到数据为null

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

foreach遍历LIST读到数据为null

xshxxm   2022-05-23 我要评论

foreach遍历LIST读到数据为null

当我们在使用mybatis的时候,就避免不了批量更新,或者批量查询使用数组或者list,就避免不了使用foreach遍历,当我们在遍历的时候,数据遍历不出来,取出的值是null

解决方案

如下:只需要修改为下标取值

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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们