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

作者:ypp91zr 时间:2022-09-01 12:47:38 

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

来源:https://blog.csdn.net/ypp91zr/article/details/84581161

标签:mybatis,Integer,查询
0
投稿

猜你喜欢

  • 关于MVC与SpringMVC的介绍、区别、执行流程

    2023-11-28 02:25:56
  • servlet异步请求的实现

    2023-07-14 17:11:38
  • SpringBoot 将配置文件挂到 jar 包外面的操作方法

    2023-11-17 11:09:32
  • mybatis-plus分页查询的实现示例

    2023-11-25 04:57:57
  • MyBatis常用的jdbcType数据类型

    2023-09-18 19:09:35
  • Java+MySQL实现学生信息管理系统源码

    2023-11-28 04:29:31
  • 使用java springboot设计实现的图书管理系统(建议收藏)

    2023-11-25 00:54:49
  • spring中的FactoryBean代码示例

    2023-11-11 18:37:20
  • 使用Spring Boot进行单元测试详情

    2023-11-10 08:01:53
  • Spring Boot ActiveMQ连接池配置过程解析

    2023-11-08 23:08:02
  • java IO流 之 输入流 InputString()的使用

    2023-08-22 07:44:31
  • 解决SpringBoot中MultipartResolver和ServletFileUpload的冲突问题

    2023-10-22 15:28:19
  • 详解spring中的Aware接口功能

    2023-07-02 00:36:01
  • JAVASE系统实现抽卡功能

    2023-11-19 19:49:41
  • Java Thread 类和Runnable 接口详解

    2023-11-10 20:16:00
  • Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件

    2023-07-06 01:24:29
  • Java实现将图片上传到webapp路径下 路径获取方式

    2023-07-10 12:44:13
  • SSH框架网上商城项目第10战之搭建商品类基本模块

    2023-11-12 14:00:29
  • Android超清晰6.0权限申请AndPermission

    2023-08-05 10:52:26
  • 使用String类型小数值转换为Long类型

    2023-04-14 10:34:56
  • asp之家 软件编程 m.aspxhome.com