采過一個坑,寫篇日志來記錄下
<if test="orderStatus != null and orderStatus !=''">
and t.is_ship=#{orderStatus}
</if>
當狀態值設置為0時,操作完了,數據庫沒反應,沒有設置為0
把狀態用1和2表示,不使用0,一切正常,問題消失了。
MyBatis的表達式是用OGNL處理的。OGNL表達式的規則如下
-
Interpreting Objects as Booleans
-
Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
-
-
If the object is a Boolean, its value is extracted and returned;
-
-
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;
-
-
If the object is a Character, its boolean value is true if and only if its char value is non-zero;
-
-
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"'>