问题-MyBatis不识别Integer值为0的数据

时间:2023-03-08 20:49:11

问题-MyBatis不识别Integer值为0的数据

问题:使用MyBatis的过程中,发现一个值为0的数据,Mybatis所识别,最后定位才发现,是自己的写法有问题,

  1. <if test="form.passLine != null and  form.passLine != '' ">
  2. and is_live =  #{form.passLine,jdbcType=INTEGER}
  3. </if>

更正成:

  1. <span style="color:#FF0000;"> <if test="form.passLine != null and  form.passLine != -1 ">
  2. and is_live =  #{form.passLine,jdbcType=INTEGER}
  3. </if></span>

完美解决。

Mybatis Integer类型,值为0被认为是空字符串的解决办法

mybatis写update时,正常是set了值才会进行update操作,我们一般是这样写。

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

如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了。

int i = 0;
i!=''。
mybatis中会返回true。也就是说,mybatis将i==0的值也认定为空字符串。
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

正常来说,0不为空也不是空字符串。所以,针对这个问题,我的解决办法是:如果类型为Integer类型,我就去掉 != ”的判断,只判断!=null即可。

以下是代码:

  1. <select id="countPageByParam" resultType="java.lang.Long">
  2. select count(id)
  3. from lms_teacher_info
  4. where 1=1
  5. and city_id = #{form.cityId,jdbcType=VARCHAR}
  6. AND update_time =  #{updateTime,jdbcType=VARCHAR}
  7. <if test="form.period != null and form.period != '' ">
  8. and period_fourweek_start = #{form.period,jdbcType=VARCHAR}
  9. </if>
  10. <if test="form.schoolId != null and form.schoolId != '' ">
  11. and school_id = #{form.schoolId,jdbcType=VARCHAR}
  12. </if>
  13. <span style="color:#FF0000;"> <if test="form.passLine != null and  form.passLine != -1 ">
  14. and is_live =  #{form.passLine,jdbcType=INTEGER}
  15. </if></span>
  16. <if test="form.streetId != null and form.streetId != '' ">
  17. and street_id = #{form.streetId,jdbcType=VARCHAR}
  18. </if>
  19. <if test="form.districtId != null and form.districtId != '' ">
  20. and district_id LIKE CONCAT( #{form.districtId,jdbcType=VARCHAR} , '%')
  21. </if>
  22. <if test="form.keyword != null and form.keyword !='' ">
  23. and (
  24. teacher_name LIKE CONCAT( #{form.keyword,jdbcType=VARCHAR} , '%')
  25. or teacher_id = #{form.keyword,jdbcType=VARCHAR}
  26. )
  27. </if>
  28. </select>

相关文章