一、問題
1、通常在對一個字段做非空判斷時採取的方式如下,執行結果是符合預期的
<if test="name != null and name != ''">
AND name = #{name}
</if>
2、但是當這個字段的類型爲數字且值爲0時,會發現語句沒有連接,判斷失敗
<if test="status != null and status != ''">
AND status = #{status}
</if>
二、說明
當 一個數值 和 一個字符 進行比較時,都會被轉成 Double 類型再進行比較
// 字符串轉換爲 Double 的邏輯
return s.length() == 0 ? 0.0D : Double.parseDouble(s);
此時 0 和 '' 都變成 0.0 了,所以 0 != '' 的結果爲 false 的,因爲它們是相等的