mybatis参数String与Integer类型的判断方式

目录
  • String与Integer类型的判断
    • 我们一般是这样写
  • 使用<if>标签判断Integer类型的坑
    • 没想到还有另外的问题
    • 注意上面的第二个条件使用的单个等号

String与Integer类型的判断

mybatis写update时,正常是set了值才会进行update操作

我们一般是这样写

<if test="sampleBatchNo != null and sampleBatchNo != ''">
SAMPLE_BATCH_NO =#{sampleBatchNo,jdbcType=VARCHAR},
</if>

1、 String类型是符合的,但是如果是Integer类型的话,mybatis会将i==0的值也认定为空字符串。

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

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

使用<if>标签判断Integer类型的坑

之前只知道如果是Integer类型,判断是否传参的时候判空就好,因为0会被认为和空字符''相等。

没想到还有另外的问题

<if test="req.type != null and req.type = 1">
</if>

注意上面的第二个条件使用的单个等号

此时不管你req.type传啥值(0啊,null啊,负数啊),甚至是不传这个属性,都会符合条件

<if test="req.type != null and req.type == 1">              
</if>

然后我改成==之后,一切恢复正常。

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

(0)

相关推荐

  • mybatis的坑-integer类型为0的数据if test失效问题

    integer类型为0的数据if test失效 mybatis的where动态判断语句if test 遇到tinyint类型为0的数据失效 发现一个mybatis的坑,有个支付表,通过状态去筛选已支付/未支付的数据,支付状态用status字段表示,status=0表示未支付,status=1表示已支付,且status类型为Integer.当选择已支付即status=1时,可以筛选成功已支付的数据列表,但是当选择未支付即status=0时,查出来的数据是未支付和已支付的所有数据. 此时就有点懵逼了

  • mybatis 返回Integer,Double,String等类型的数据操作

    在使用mybatis的过程中会遇到只返回单独数据类型的问题会用到resultType. //返回Integer <select id="getSpeedByLinkId" parameterType="java.lang.String" resultType="Integer"> SELECT speed from dws_tfc_state_speed_link_last_rt where link_id = #{linkId} &

  • Mybatis单个参数的if判断报异常There is no getter for property named 'xxx' in 'class java.lang.Integer'的解决方案

    我们都知道mybatis在进行参数判断的时候,直接可以用<if test=""></if> 就可以了,如下: 1.常规代码 <update id="update" parameterType="com.cq2022.zago.order.entity.Test" > update t_test_l <set > <if test="trnsctWayId != null"

  • Mybatis返回int或者Integer类型报错的解决办法

    会报错如下: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'com.bill.springMybatis.dao.UserDao.getUserIdByName attempted to return null from a m

  • mybatis参数String与Integer类型的判断方式

    目录 String与Integer类型的判断 我们一般是这样写 使用<if>标签判断Integer类型的坑 没想到还有另外的问题 注意上面的第二个条件使用的单个等号 String与Integer类型的判断 mybatis写update时,正常是set了值才会进行update操作 我们一般是这样写 <if test="sampleBatchNo != null and sampleBatchNo != ''"> SAMPLE_BATCH_NO =#{sampleB

  • 关于mybatis遇到Integer类型的参数时动态sql需要注意条件

    目录 mybatisInteger类型参数动态sql注意条件 例如以下拼接的动态sql mybatis的坑——Integer类型参数解析问题 有时候我们使用实体类传递参数时 因为mybatis在解析Integer类型数据时 mybatis Integer类型参数动态sql注意条件 例如以下拼接的动态sql <if test="work_status !=null  and work_status !='' ">  and T.status=#{work_status,jdb

  • MyBatis 参数类型为String时常见问题及解决方法

    1. 参数为String时的插值问题 假设有下面一Dao接口方法 public Account findByAccountType (String type)throws DaoException; 对应的Mapper.xml <select id="findByAccountType " parameterType="string" resultType="account"> select * form account <wh

  • Java 如何判断Integer类型的值是否相等

    目录 判断Integer类型的值是否相等 Integer赋值比较 赋值操作 构造函数 判断Integer类型的值是否相等 我们知道Integer是int的包装类,在jdk1.5以上,可以实现自动装箱拆箱,就是jdk里面会自动帮我们转换,不需要我们手动去强转,所以我们经常在这两种类型中随意写,平时也没什么注意 但Integer他是对象,我们知道 == 比较的是堆中的地址,但有个奇怪的事是, 如果 Integer a = 123, Integer b = 123,可以返回true,但如果Intege

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

    目录 使用Integer类型查询出现的问题 当state这个值为0的时候 mybatis判断Integer遇到的bug 场景产出 我是这样写的 使用Integer类型查询出现的问题 mapper.xml : <select id="count" parameterType="com.pinyu.system.web.page.Page" resultType="java.lang.Integer">         select co

  • Mybatis Integer类型参数值为0时得到为空的解决方法

    今日遇到的问题: 查询版本信息时,由于version是Integer类型,所以当前台选择版本为0时,变成了查询了所有的版本信息. sql片段: </if> <if test="version != null and version != '' "> AND a.version = #{version} </if> 原因: MyBatis因自身原因默认了 Integer类型数据值等于0时 为 ""(空字符串) 解决办法: 1. 某些

  • mybatis写xml时数字类型千万别用 !=‘‘(不为空串)进行判断的示例详解

    前言 最近项目内更新数据时,发现数字类型字段设置为0时不能正常的更新进数据库,我们打印了下mybatis的sql日志发现字段为0的sql没有被拼接. 样例 下面的是错误示例 ❌ <update id="update" parameterType="com.chengfengfeng.test.domain.People"> update people set <if test="age!=null and age !=''"&g

  • 深入了解MyBatis参数

    深入了解MyBatis参数 相信很多人可能都遇到过下面这些异常: "Parameter 'xxx' not found. Available parameters are [...]" "Could not get property 'xxx' from xxxClass. Cause: "The expression 'xxx' evaluated to a null value." "Error evaluating expression '

随机推荐