MyBatis query conditions to determine whether the field is empty generally:
<if test="testValue!=null and testValue != ''">
and test_value = #{testValue}
</if>
If the parameter is passed with the value type Integer 0, 0 will turn empty string
Source truth is:
All sqlNode nodes MyBatis resolved, for if the node will be treated to IfSqlNode, been to the layers of treatment, eventually calling OgnlOps.class class doubleValue (Object value) method
public static double doubleValue(Object value) throws NumberFormatException {
if (value == null) {
return 0.0D;
} else {
Class c = value.getClass();
if (c.getSuperclass() == Number.class) {
return ((Number)value).doubleValue();
} else if (c == Boolean.class) {
return ((Boolean)value).booleanValue() ? 1.0D : 0.0D;
} else if (c == Character.class) {
return (double)((Character)value).charValue();
} else {
String s = stringValue(value, true);
return s.length() == 0 ? 0.0D : Double.parseDouble(s);
}
}
}
0 and "" are calling the method returns double values are 0.0, making comparisons.
Approach:
<if test="testValue!=null and testValue!='' or 0 == testValue">
and test_value = #{testValue}
</if>
或者
<if test="testValue!=null">
and test_value = #{testValue}
</if>