mybatis使用foreach查询不出结果也不报错的问题怎么解决

发布时间:2022-03-23 09:07:14 作者:iii
来源:亿速云 阅读:634

本篇内容介绍了“mybatis使用foreach查询不出结果也不报错的问题怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

foreach查询不出结果也不报错问题

首先,执行的时候语法没有报错,其次sql语句拿到数据库去执行能查到数据,但是在接口这边返回空输数据,查看控制台发现sql语句执行了,但是返回结果为0。此时猜想是传入参数的问题。

执行结果

此时数组是直接从参数里接收

mybatis使用foreach查询不出结果也不报错的问题怎么解决

仔细看此时的数组和普通的数组还是有差别的

mybatis使用foreach查询不出结果也不报错的问题怎么解决

但是此时执行是没有问题的,但是查不到数据

mybatis使用foreach查询不出结果也不报错的问题怎么解决

正确执行结果

mybatis使用foreach查询不出结果也不报错的问题怎么解决

此时的数组

mybatis使用foreach查询不出结果也不报错的问题怎么解决

遍历输出

mybatis使用foreach查询不出结果也不报错的问题怎么解决

所以,由此可以看出是参数的问题

正确做法

由于接收到的数组是json数组,不能直接使用,要转成普通数组即可

mybatis使用foreach查询不出结果也不报错的问题怎么解决

前端传入参数(数组即可)

mybatis使用foreach查询不出结果也不报错的问题怎么解决

使用foreach、in操作注意点

mybatis语法掌握不熟,在写foreach操作时,造成in ()错误,这种情况不符合SQL的语法,导致程序报错。

如果简单只做非空判断,这样也有可能会有问题:本来in一个空列表,应该是没有数据才对,却变成了获取全部数据!

错误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>

“mybatis使用foreach查询不出结果也不报错的问题怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. return 跳不出forEach循环
  2. 如何解决layui页面按钮点击无反应,也不报错的问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis foreach

上一篇:如何调用Web API

下一篇:flutter怎么实现头部tabTop滚动栏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》