温馨提示×

温馨提示×

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

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

mybatis使用Integer类型查询出现的问题怎么解决

发布时间:2022-03-14 12:01:55 来源:亿速云 阅读:207 作者:iii 栏目:开发技术

本文小编为大家详细介绍“mybatis使用Integer类型查询出现的问题怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“mybatis使用Integer类型查询出现的问题怎么解决”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

使用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(),否则会报错。

读到这里,这篇“mybatis使用Integer类型查询出现的问题怎么解决”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

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

AI