软件编程
位置:首页>> 软件编程>> java编程>> mybatis foreach遍历LIST读到数据为null的问题

mybatis foreach遍历LIST读到数据为null的问题

作者:xshxxm  发布时间:2021-05-24 20:15:27 

标签:mybatis,foreach,LIST,null

foreach遍历LIST读到数据为null

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

mybatis foreach遍历LIST读到数据为null的问题

mybatis foreach遍历LIST读到数据为null的问题

解决方案

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

mybatis foreach遍历LIST读到数据为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>

来源:https://blog.csdn.net/xshxxm/article/details/88873473

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com