mybatis使用foreach查詢不出結果也不報錯的問題
foreach查詢不出結果也不報錯問題
首先,執(zhí)行的時候語法沒有報錯,其次sql語句拿到數(shù)據(jù)庫去執(zhí)行能查到數(shù)據(jù),但是在接口這邊返回空輸數(shù)據(jù),查看控制臺發(fā)現(xiàn)sql語句執(zhí)行了,但是返回結果為0。此時猜想是傳入?yún)?shù)的問題。
執(zhí)行結果
此時數(shù)組是直接從參數(shù)里接收

仔細看此時的數(shù)組和普通的數(shù)組還是有差別的

但是此時執(zhí)行是沒有問題的,但是查不到數(shù)據(jù)

正確執(zhí)行結果

此時的數(shù)組

遍歷輸出

所以,由此可以看出是參數(shù)的問題
正確做法
由于接收到的數(shù)組是json數(shù)組,不能直接使用,要轉成普通數(shù)組即可

前端傳入?yún)?shù)(數(shù)組即可)

使用foreach、in操作注意點
mybatis語法掌握不熟,在寫foreach操作時,造成in ()錯誤,這種情況不符合SQL的語法,導致程序報錯。
如果簡單只做非空判斷,這樣也有可能會有問題:本來in一個空列表,應該是沒有數(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>
? ? ? ? <!--判斷方式錯了,應該先用null再用size>0判斷;如果areaIds為空,查詢結果也應為空,而不是其他查詢結果。所以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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Swagger2不被SpringSecurity框架攔截的配置及說明
這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

