mybatis使用Integer类型查询可能出现的问题
作者:ypp91zr 发布时间:2022-09-01 12:47:38
标签: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(),否则会报错。
来源:https://blog.csdn.net/ypp91zr/article/details/84581161
0
投稿
猜你喜欢
- 前言Kotlin一个强大之处就在于它的扩展函数,巧妙的运用这些扩展函数可以让你写出的代码更加优雅,阅读起来更加流畅,下面总结了在开发中经常用
- 本文实例为大家分享了Android实现双曲线折线图的具体代码,供大家参考,具体内容如下先看一下效果图1.先下载jar包 mpandroidc
- 一直使用的是FastJson,感觉还不错,很方便。看了一段别人的分析,觉得很有道理。为什么要使用Fastjson,其实原因不需要太多,喜欢就
- java 请求跨域问题解决方法实例详解新建Util类,在Util中添加下面方法: /* * response请求跨域公共设置
- 功能介绍功能:群聊+私发+上线提醒+下线提醒+查询在线用户文件Utils需要用maven导入下面两个包 <dependency>
- 这篇文章主要介绍了Java通过Scanner了解if...else if语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- Gson是一个Java库,用来实现Json和Java对象之间的相互转换。Gson是一个托管在https://github.com/googl
- 直接用英文逗号分隔就可以了,比如:inerface IHello { String sayHello(String name);
- springboot配置文件中属性变量引用@@这种属性应用方式是field_name=@field_value@。两个@符号是springb
- 前言本文将实现一个MyBatis的Springboot的Starter包,引用这个Starter包后,仅需要提供少量配置信息,就能够完成My
- 1、volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)&nb
- Java基于对象流实现银行系统的具体代码,供大家参考,具体内容如下系统特点:数据持久化到文件中,系统启动后,加载文件中数据到集合中,相当于做
- 本文实例为大家分享了Java打印指定年月日历的具体代码,供大家参考,具体内容如下日历如下:程序如下://打印指定年月的日历public cl
- java读写ini文件、FileOutputStream在上课让学生练习文件读写,就让他们做了一个使用文件保存账号和密码的练习,有一个比较爱
- 日志过滤对于一个网站日志,首先要对它进行过滤,删除一些不必要的信息,我们通过scala语言来实现,清洗代码如下,代码要通过别的软件打包为ja
- 本文实例讲述了java GUI编程之paint绘制操作。分享给大家供大家参考,具体如下:import java.awt.*;public c
- java.util.concurrent包中的工具实现核心都是AQS,了解ReentrantLock的实现原理,需要先分析AQS以及AQS与
- 首先给出一段代码:public class AslistMethod { public static void main(String[]
- @ApiImplicitParam作用在方法上,表示单独的请求参数参数name:参数名。value:参数的具体意义,作用。required:
- filter自定义过滤器 增加了 对验证码的校验package com.youxiong.filter;import com.y