標簽0為null 無效的 踩坑" type="hidden"/>

mybatis 標簽0為null 無效的 踩坑


 

采過一個坑,寫篇日志來記錄下

<if test="orderStatus != null and orderStatus !=''">
and t.is_ship=#{orderStatus}
</if>

 

當狀態值設置為0時,操作完了,數據庫沒反應,沒有設置為0 
把狀態用1和2表示,不使用0,一切正常,問題消失了。 

MyBatis的表達式是用OGNL處理的。OGNL表達式的規則如下

  1. Interpreting Objects as Booleans
  2. Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
  3.  
  4. If the object is a Boolean, its value is extracted and returned;
  5.  
  6. If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;
  7.  
  8. If the object is a Character, its boolean value is true if and only if its char value is non-zero;
  9.  
  10. Otherwise, its boolean value is true if and only if it is non-null.

 

根據這一條If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false

  

state !=''  傳入0時,表達式的值為false;所以不執行。 
解決辦法,把這個刪掉就可以了。

<if test="state != null">state = #{state }</if>
<if test="orderStatus != null ">
and t.is_ship=#{orderStatus}
</if>
 

本來只有String類型的才會進行這樣的判斷——xx!= '',其實是自己碼代碼時只顧自制粘貼,自己給自己埋了個坑。

這里有必要再提一個“坑”,如果類似於String str =”A”; 這樣的寫法時時,根據第三條規則,OGNL將會識別為Java 中的 char類型,顯然String 類型與char類型做==運算會返回false,從而導致表達式不成立。解決方法很簡單,修改為

<if test='str!= null and str == "A"'>

 

 


免責聲明!

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



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