問題:使用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>