mybatis foreach遍歷LIST讀到數(shù)據(jù)為null的問題
foreach遍歷LIST讀到數(shù)據(jù)為null
當我們在使用mybatis的時候,就避免不了批量更新,或者批量查詢使用數(shù)組或者list,就避免不了使用foreach遍歷,當我們在遍歷的時候,數(shù)據(jù)遍歷不出來,取出的值是null
解決方案
如下:只需要修改為下標取值
foreach 遍歷list中的坑
將jdbc改寫為mybatis時,傳入的條件為list使用到的標簽是<where> 、<choose>、<when>、<if>、<foreach>因為判斷l(xiāng)ist集合時判斷條件不全,導致sql執(zhí)行錯誤
下面是正確的判斷條件
<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多驗證登錄功能的實現(xiàn)(賬號密碼登錄和使用手機驗證碼登錄)
這篇文章給大家介紹springboot整合shiro多驗證登錄功能的實現(xiàn)方法,包括賬號密碼登錄和使用手機驗證碼登錄功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-07-07Java concurrency線程池之線程池原理(二)_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency線程池之線程池原理第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04