欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis使用foreach查詢(xún)不出結(jié)果也不報(bào)錯(cuò)的問(wèn)題

 更新時(shí)間:2022年03月22日 16:59:06   作者:HYDMonster  
這篇文章主要介紹了mybatis使用foreach查詢(xún)不出結(jié)果也不報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

foreach查詢(xún)不出結(jié)果也不報(bào)錯(cuò)問(wèn)題

首先,執(zhí)行的時(shí)候語(yǔ)法沒(méi)有報(bào)錯(cuò),其次sql語(yǔ)句拿到數(shù)據(jù)庫(kù)去執(zhí)行能查到數(shù)據(jù),但是在接口這邊返回空輸數(shù)據(jù),查看控制臺(tái)發(fā)現(xiàn)sql語(yǔ)句執(zhí)行了,但是返回結(jié)果為0。此時(shí)猜想是傳入?yún)?shù)的問(wèn)題。

執(zhí)行結(jié)果

此時(shí)數(shù)組是直接從參數(shù)里接收

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

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

正確執(zhí)行結(jié)果

此時(shí)的數(shù)組

遍歷輸出

所以,由此可以看出是參數(shù)的問(wèn)題

正確做法

由于接收到的數(shù)組是json數(shù)組,不能直接使用,要轉(zhuǎn)成普通數(shù)組即可

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

使用foreach、in操作注意點(diǎn)

mybatis語(yǔ)法掌握不熟,在寫(xiě)foreach操作時(shí),造成in ()錯(cuò)誤,這種情況不符合SQL的語(yǔ)法,導(dǎo)致程序報(bào)錯(cuò)。

如果簡(jiǎn)單只做非空判斷,這樣也有可能會(huì)有問(wèn)題:本來(lái)in一個(gè)空列表,應(yīng)該是沒(méi)有數(shù)據(jù)才對(duì),卻變成了獲取全部數(shù)據(jù)!

錯(cuò)誤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>
? ? ? ? <!--判斷方式錯(cuò)了,應(yīng)該先用null再用size>0判斷;如果areaIds為空,查詢(xún)結(jié)果也應(yīng)為空,而不是其他查詢(xún)結(jié)果。所以sql有問(wèn)題-->
? ? ? ? <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>
? ? ? ? <!--加入這個(gè)非真條件-->
? ? ? ? <if test="areaIds==null or areaIds.size == ?0">
? ? ? ? and 1=0
? ? ? ? </if>
? ? ? ? order by a.create_time desc ) as b
? ? </select>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • ?Java數(shù)據(jù)結(jié)構(gòu)的十大排序

    ?Java數(shù)據(jù)結(jié)構(gòu)的十大排序

    這篇文章主要介紹了?Java數(shù)據(jù)結(jié)構(gòu)的十大排序,排序算法分為比較類(lèi)排序和非比較類(lèi)排序,具體的內(nèi)容,需要的朋友參考下面思維導(dǎo)圖及文章介紹,希望對(duì)你有所幫助
    2022-01-01
  • Spring如何使用注解的方式創(chuàng)建bean

    Spring如何使用注解的方式創(chuàng)建bean

    這篇文章主要介紹了Spring如何使用注解的方式創(chuàng)建bean,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Swagger2不被SpringSecurity框架攔截的配置及說(shuō)明

    Swagger2不被SpringSecurity框架攔截的配置及說(shuō)明

    這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決

    Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決

    這篇文章主要介紹了Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 深入理解Hibernate中的flush機(jī)制

    深入理解Hibernate中的flush機(jī)制

    這篇文章主要介紹了深入理解Hibernate中的flush機(jī)制,本文是對(duì)flush機(jī)制深入研究得出的一些結(jié)論總結(jié),需要的朋友可以參考下
    2015-01-01
  • Java實(shí)現(xiàn)購(gòu)物管理系統(tǒng)

    Java實(shí)現(xiàn)購(gòu)物管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)購(gòu)物管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • java  中Excel轉(zhuǎn)shape file的實(shí)例詳解

    java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

    這篇文章主要介紹了java 中Excel轉(zhuǎn)shape file的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文大家能實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • Prometheus pushgateway的使用詳解

    Prometheus pushgateway的使用詳解

    為了防止 pushgateway 重啟或意外掛掉,導(dǎo)致數(shù)據(jù)丟失,我們可以通過(guò) -persistence.file 和 -persistence.interval 參數(shù)將數(shù)據(jù)持久化下來(lái),接下來(lái)通過(guò)本文給大家介紹下Prometheus pushgateway的使用,感興趣的朋友一起看看吧
    2021-11-11
  • java如何實(shí)現(xiàn)數(shù)位分離

    java如何實(shí)現(xiàn)數(shù)位分離

    這篇文章主要介紹了java如何實(shí)現(xiàn)數(shù)位分離,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • mybatis 解決將數(shù)值0識(shí)別成空字符串的問(wèn)題

    mybatis 解決將數(shù)值0識(shí)別成空字符串的問(wèn)題

    這篇文章主要介紹了mybatis 解決將數(shù)值0識(shí)別成空字符串的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論