mybatis使用foreach查詢不出結(jié)果也不報錯的問題
foreach查詢不出結(jié)果也不報錯問題
首先,執(zhí)行的時候語法沒有報錯,其次sql語句拿到數(shù)據(jù)庫去執(zhí)行能查到數(shù)據(jù),但是在接口這邊返回空輸數(shù)據(jù),查看控制臺發(fā)現(xiàn)sql語句執(zhí)行了,但是返回結(jié)果為0。此時猜想是傳入?yún)?shù)的問題。
執(zhí)行結(jié)果
此時數(shù)組是直接從參數(shù)里接收
仔細(xì)看此時的數(shù)組和普通的數(shù)組還是有差別的
但是此時執(zhí)行是沒有問題的,但是查不到數(shù)據(jù)
正確執(zhí)行結(jié)果
此時的數(shù)組
遍歷輸出
所以,由此可以看出是參數(shù)的問題
正確做法
由于接收到的數(shù)組是json數(shù)組,不能直接使用,要轉(zhuǎn)成普通數(shù)組即可
前端傳入?yún)?shù)(數(shù)組即可)
使用foreach、in操作注意點
mybatis語法掌握不熟,在寫foreach操作時,造成in ()錯誤,這種情況不符合SQL的語法,導(dǎo)致程序報錯。
如果簡單只做非空判斷,這樣也有可能會有問題:本來in一個空列表,應(yīng)該是沒有數(shù)據(jù)才對,卻變成了獲取全部數(shù)據(jù)!
錯誤sql示例
<select id="getActiveCount" resultType="int" parameterType="com.missfresh.active.dto.ActiveSearchDTO"> ? ? ? ? select count(1) from ( ? ? ? ? SELECT ? ? ? ? distinct ?a.* ? ? ? ? FROM ? ? ? ? active AS a ? ? ? ? <if test="sku!='' and sku!=null"> ? ? ? ? ? ? LEFT JOIN active_promotion AS ap ON ap.active_id = a.id ? ? ? ? ? ? LEFT JOIN promotion_product AS pp ON pp.promotion_id = ap.promotion_id ? ? ? ? </if> ? ? ? ? <if test="areaIds!='' and areaIds!=null"> ? ? ? ? ? ? LEFT JOIN active_area AS aa ON aa.active_id = a.id and aa.status = 1 and aa.area_type = 0 ? ? ? ? </if> ? ? ? ? WHERE a.status <![CDATA[!= ]]> 0 ? ? ? ? <if test="id!=0 and id!=null"> ? ? ? ? ? ? AND a.id = #{id} ? ? ? ? </if> ? ? ? ? <if test="name!='' and name!=null"> ? ? ? ? ? ? AND a.name LIKE CONCAT('%',#{name},'%') ? ? ? ? </if> ? ? ? ? <if test="sku!='' and sku!=null"> ? ? ? ? ? ? AND pp.sku = #{sku} ? ? ? ? </if> ? ? ? ? <!--判斷方式錯了,應(yīng)該先用null再用size>0判斷;如果areaIds為空,查詢結(jié)果也應(yīng)為空,而不是其他查詢結(jié)果。所以sql有問題--> ? ? ? ? <if test="areaIds!='' and areaIds!=null"> ? ? ? ? ? ? and aa.area_id IN ? ? ? ? ? ? <foreach item="item" index="index" collection="areaIds" open="(" separator="," close=")"> ? ? ? ? ? ? ? ? #{item} ? ? ? ? ? ? </foreach> ? ? ? ? </if> ? ? ? ? order by a.create_time desc ) as b ? ? </select>
改正后的sql為
<select id="getActiveCount" resultType="int" parameterType="com.missfresh.active.dto.ActiveSearchDTO"> ? ? ? ? select count(1) from ( ? ? ? ? SELECT ? ? ? ? distinct ?a.* ? ? ? ? FROM ? ? ? ? active AS a ? ? ? ? <if test="sku!='' and sku!=null"> ? ? ? ? ? ? LEFT JOIN active_promotion AS ap ON ap.active_id = a.id ? ? ? ? ? ? LEFT JOIN promotion_product AS pp ON pp.promotion_id = ap.promotion_id ? ? ? ? </if> ? ? ? ? <if test="areaIds!='' and areaIds!=null"> ? ? ? ? ? ? LEFT JOIN active_area AS aa ON aa.active_id = a.id and aa.status = 1 and aa.area_type = 0 ? ? ? ? </if> ? ? ? ? WHERE a.status <![CDATA[!= ]]> 0 ? ? ? ? <if test="id!=0 and id!=null"> ? ? ? ? ? ? AND a.id = #{id} ? ? ? ? </if> ? ? ? ? <if test="name!='' and name!=null"> ? ? ? ? ? ? AND a.name LIKE CONCAT('%',#{name},'%') ? ? ? ? </if> ? ? ? ? <if test="sku!='' and sku!=null"> ? ? ? ? ? ? AND pp.sku = #{sku} ? ? ? ? </if> ? ? ? ? <if test="areaIds!=null and areaIds.size > 0"> ? ? ? ? ? ? and aa.area_id IN ? ? ? ? ? ? <foreach item="item" index="index" collection="areaIds" open="(" separator="," close=")"> ? ? ? ? ? ? ? ? #{item} ? ? ? ? ? ? </foreach> ? ? ? ? </if> ? ? ? ? <!--加入這個非真條件--> ? ? ? ? <if test="areaIds==null or areaIds.size == ?0"> ? ? ? ? and 1=0 ? ? ? ? </if> ? ? ? ? order by a.create_time desc ) as b ? ? </select>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
?Java數(shù)據(jù)結(jié)構(gòu)的十大排序
這篇文章主要介紹了?Java數(shù)據(jù)結(jié)構(gòu)的十大排序,排序算法分為比較類排序和非比較類排序,具體的內(nèi)容,需要的朋友參考下面思維導(dǎo)圖及文章介紹,希望對你有所幫助2022-01-01Swagger2不被SpringSecurity框架攔截的配置及說明
這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11java 中Excel轉(zhuǎn)shape file的實例詳解
這篇文章主要介紹了java 中Excel轉(zhuǎn)shape file的實例詳解的相關(guān)資料,希望通過本文大家能實現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09