mybatis的integer类型为0的失效问题怎么解决(integer,mybatis,开发技术)

时间:2024-05-04 15:44:17 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

integer类型为0的数据if test失效

mybatis的where动态判断语句if test 遇到tinyint类型为0的数据失效

发现一个mybatis的坑,有个支付表,通过状态去筛选已支付/未支付的数据,支付状态用status字段表示,status=0表示未支付,status=1表示已支付,且status类型为Integer。当选择已支付即status=1时,可以筛选成功已支付的数据列表,但是当选择未支付即status=0时,查出来的数据是未支付和已支付的所有数据。

此时就有点懵逼了,后面debug一层层去追踪,发现status=0时,mybatis构建的sql中where条件没有把status字段拼接上去,但是status=1时,sql中可以看到where中有status字段。

经过后面找资料发现,integer类型的字段,在mybatis中的if test 条件中,会把值为0的当成false处理,因为会将integer=0的参数默认为‘’(空串),即status=0的判断结果为false,所以未支付的条件永远不可能出现,查出来的数据就是所有状态的数据。

以下图为出错时的语句:

<where><trimprefixOverrides="and"><iftest="status!=nullandstatus!=''">andstatus=#{status}</if><iftest="createdDtBegin!=null">andcreated_dt<![CDATA[>=]]>#{createdDtBegin,jdbcType=TIMESTAMP}</if><iftest="createdDtEnd!=null">andcreated_dt<![CDATA[<=]]>#{createdDtEnd,jdbcType=TIMESTAMP}</if></trim></where>

解决方式

改成 **<if test=“status != null”>and status=#{status, jdbcType=TINYINT}</if>

这样即可,即把 status != ''去掉

mybatis的integer类型为0的失效问题怎么解决

或者还有一种做法,即:

如果status为integer,<if test=“status != null and status != -1”>或者:如果status为integer,<if test=“status != null and status != &lsquo;&rsquo; or status == 0”>

mybatis if 判断 Integer 类型的坑

当POJO中的属性类型为 Integer 时,传入 0,此时在 xxxMapper.xml的 if 判断中总是不能满足条件,进而导致查询条件无效,这是因为 mybatis 针对 integer 类型的 0 进行了特殊处理,当成了 &lsquo;&rsquo; 来处理,进行如下判断即可:

payStatus 为 Integer 类型

<iftest="payStatus!=nullandpayStatus!=''orpayStatus==0">anda.pay_status=#{payStatus}</if>
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:mybatis的integer类型为0的失效问题怎么解决的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:OpenCV如何利用手势识别实现虚拟拖放效果下一篇:

7 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18