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

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

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

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

更正成:

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

完美解决。

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

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

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

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

int i = 0;
i!=”。
mybatis中会返回true。也就是说,mybatis将i==0的值也认定为空字符串。
正常来说,0不为空也不是空字符串。所以,针对这个问题,我的解决办法是:如果类型为Integer类型,我就去掉 != ”的判断,只判断!=null即可。
以下是代码:

<select id="countPageByParam" resultType="java.lang.Long">  
  select count(id)  
  from lms_teacher_info  
  where 1=1  
  and city_id = #{form.cityId,jdbcType=VARCHAR}  
  AND update_time =  #{updateTime,jdbcType=VARCHAR}  
  <if test="form.period != null and form.period != '' ">  
    and period_fourweek_start = #{form.period,jdbcType=VARCHAR}  
  </if>  
  <if test="form.schoolId != null and form.schoolId != '' ">  
    and school_id = #{form.schoolId,jdbcType=VARCHAR}  
  </if>  
 <if test="form.passLine != null and  form.passLine != -1 ">  
      and is_live =  #{form.passLine,jdbcType=INTEGER}  
  </if>
  <if test="form.streetId != null and form.streetId != '' ">  
    and street_id = #{form.streetId,jdbcType=VARCHAR}  
  </if>  
  <if test="form.districtId != null and form.districtId != '' ">  
    and district_id LIKE CONCAT( #{form.districtId,jdbcType=VARCHAR} , '%')  
  </if>  
  <if test="form.keyword != null and form.keyword !='' ">  
    and (  
    teacher_name LIKE CONCAT( #{form.keyword,jdbcType=VARCHAR} , '%')  
    or teacher_id = #{form.keyword,jdbcType=VARCHAR}  
    )  
  </if>  
</select>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值