問題-MyBatis不識別Integer值為0的數據


問題-MyBatis不識別Integer值為0的數據

問題:使用MyBatis的過程中,發現一個值為0的數據,Mybatis所識別,最后定位才發現,是自己的寫法有問題,

 

[html]  view plain  copy
 
 print?
  1. <if test="form.passLine != null and  form.passLine != '' ">  
  2.     and is_live =  #{form.passLine,jdbcType=INTEGER}  
  3. </if>  

更正成:

 

 

[html]  view plain  copy
 
 print?
  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即可。

 

 

以下是代碼:

 

[java]  view plain  copy
 
 print?
  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>  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM