广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis使用Integer类型查询可能出现的问题
  • 873
分享到

mybatis使用Integer类型查询可能出现的问题

2024-04-02 19:04:59 873人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录使用Integer类型查询出现的问题当state这个值为0的时候mybatis判断Integer遇到的bug场景产出我是这样写的使用Integer类型查询出现的问题 mapper

使用Integer类型查询出现的问题

mapper.xml :

<select id="count" parameterType="com.pinyu.system.WEB.page.Page" resultType="java.lang.Integer">
        select count(m.id) from hr_push_msg_model as m
        <where>
            <if test="page.keyWord != null and page.keyword != ''">
                m.text like '%${page.keyword}%'
            </if>
            <if test="page.entity != null">
                <if test="page.entity.text != null and page.entity.text != ''">
                    and  m.text like '%${page.entity.text}%'
                </if>
                <if test="page.entity.title != null and page.entity.title != ''">
                    and  m.title like '%${page.entity.title}%'
                </if>
                <if test="page.entity.state != null and page.entity.state != ''">
                    and  m.state = #{page.entity.state}
                </if>
                <if test="page.entity.type != null and page.entity.type != ''">
                    and  m.type = #{page.entity.type}
                </if>
            </if>
        </where>
    </select>

比如:

<if test="page.entity.state != null and page.entity.state != ''">
                    and  m.state = #{page.entity.state}
                </if>

当state这个值为0的时候

mybatis为默认为空字符串"",所以如果状态这种类似的场景有0值得,查询就不要加上xxxx!=""这种。或者or xxx==0

代码示例:

1、

<if test="page.entity.state != null">
     and  m.state = #{page.entity.state}
</if>

2、

<if test="page.entity.state != null and page.entity.state != '' or page.entity.state==0">
    and  m.state = #{page.entity.state}
</if>

mybatis判断Integer遇到的bug

场景产出

需要查出状态为0的所有用户

我是这样写的

1.mapper:

BaseUser selectUserByStatus(@parm("status") Integer status);

这里传了0进去

2.sql

SELECT * FROM base_user WHERE status=0

3.xml片段

<if test="status != null and status != ''">
    status = #{status},
</if>

4.结果真正执行的sql

SELECT * FROM base_user

小结一下:

test="status != null and status != ''"这个是拿来判断String的!!!也就是说Double,BigDecimal等数字类型也会出现这样的情况

1.如果是Integer类型的话,如果变量的值是0,即 num = 0, mybatis在进行 num != '' " 的时候会认为  num 的值是空字符串;直接跳过判断了

所以如果是Integer类型只需要判断 != null 即可

2.如果String类型需要判断不等于0,则需要写name != '0'.toString(),否则会报错。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: mybatis使用Integer类型查询可能出现的问题

本文链接: https://www.lsjlt.com/news/142355.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作