mybatis foreach遍历LIST读到数据为null的问题
更新时间:2022年02月08日 10:29:42 作者:xshxxm
这篇文章主要介绍了mybatis foreach遍历LIST读到数据为null的问题,具有很好的参考价值,希望对大家有所帮助。
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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
这篇文章给大家介绍springboot整合shiro多验证登录功能的实现方法,包括账号密码登录和使用手机验证码登录功能,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧2021-07-07Java concurrency线程池之线程池原理(二)_动力节点Java学院整理
这篇文章主要为大家详细介绍了Java concurrency线程池之线程池原理第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06在SpringBoot 中从application.yml中获取自定义常量方式
这篇文章主要介绍了在SpringBoot 中从application.yml中获取自定义常量方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-04-04
最新评论