mybatis foreach遍历LIST读到数据为null的问题
作者:xshxxm 时间:2021-05-24 20:15:27
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>
来源:https://blog.csdn.net/xshxxm/article/details/88873473
标签:mybatis,foreach,LIST,null
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java服务限流算法的6种实现
2022-04-03 04:52:51
![](https://img.aspxhome.com/file/2023/3/62993_0s.jpg)
IDEA远程管理docker镜像及容器服务的实现
2022-01-07 16:26:48
![](https://img.aspxhome.com/file/2023/9/63439_0s.png)
Java中JDBC连接数据库详解
2023-08-10 16:57:18
![](https://img.aspxhome.com/file/2023/2/58162_0s.png)
java生成验证码图片的方法
2023-12-09 08:57:15
Spring AOP底层原理及代理模式
2023-05-05 14:19:38
C#使用linq查询大数据集的方法
2023-05-24 16:52:17
Java java.sql.Timestamp时间戳案例详解
2023-11-10 13:50:47
java 设计模式之单例模式
2021-10-07 22:00:46
教你怎么用Java开发扫雷游戏
2023-07-22 09:49:26
![](https://img.aspxhome.com/file/2023/0/63480_0s.png)
Android使用Gradle依赖配置compile、implementation与api的区别介绍
2023-09-27 18:11:40
Spring Boot从Controller层进行单元测试的实现
2023-07-21 03:07:10
JavaWeb实现多文件上传及zip打包下载
2023-11-16 16:41:25
java并发编程包JUC线程同步CyclicBarrier语法示例
2023-01-18 07:44:58
![](https://img.aspxhome.com/file/2023/7/63687_0s.png)
Java多线程编程中使用Condition类操作锁的方法详解
2023-10-19 13:30:55
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
2022-05-24 17:14:13
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
java识别一篇文章中某单词出现个数的方法
2022-04-24 06:18:40
java String类常量池分析及"equals"和"==”区别详细介绍
2021-11-28 01:54:48
浅谈SpringBoot中的@Conditional注解的使用
2021-12-07 16:47:04
![](https://img.aspxhome.com/file/2023/8/61798_0s.png)
java中的实体类时间格式化
2022-05-18 02:18:19
![](https://img.aspxhome.com/file/2023/3/63393_0s.png)
Java中synchronized锁的深入理解
2023-08-18 01:36:55
![](https://img.aspxhome.com/file/2023/1/64771_0s.webp)