问题-MyBatis不识别Integer值为0的数据
问题:使用MyBatis的过程中,发现一个值为0的数据,Mybatis所识别,最后定位才发现,是自己的写法有问题,
- <if test="form.passLine != null and form.passLine != '' ">
- and is_live = #{form.passLine,jdbcType=INTEGER}
- </if>
更正成:
- <span style="color:#FF0000;"> <if test="form.passLine != null and form.passLine != -1 ">
- and is_live = #{form.passLine,jdbcType=INTEGER}
- </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即可。
以下是代码:
- <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>
- <span style="color:#FF0000;"> <if test="form.passLine != null and form.passLine != -1 ">
- and is_live = #{form.passLine,jdbcType=INTEGER}
- </if></span>
- <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>