最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
MyBatis参数条件查询传入值为0时的判断问题及解决
时间:2026-06-17 08:27:47 编辑:袖梨 来源:一聚教程网
MyBatis参数条件查询传入的值为0判断
MyBatis条件查询对字段判断是否为空一般为:

<if test="testValue!=null and testValue != ''">
and test_value = #{testValue}
</if>
如果传入参数为Integer类型且值为0时,会把0转为空串
源码真实情况
MyBatis解析的所有sqlNode节点,针对if节点会交给IfSqlNode来处理,进过层层处理,最终都会调用OgnlOps.class类的doubleValue(Object value)方法
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和""都调用该方法返回的double值都为0.0,在进行比较。
处理方法
<if test="testValue!=null and testValue!='' or 0 == testValue">
and test_value = #{testValue}
</if>
或者
<if test="testValue!=null">
and test_value = #{testValue}
</if>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:- mybatis父查询中的参数传递到子查询中使用过程
- mybatis-plus判断参数是否为空并作为查询条件方式
- mybatis-plus判断参数是否为空并作为查询条件详解
- MyBatis参数处理与查询结果映射实例详解
- MyBatis使用POJO参数动态查询教程的实现
- MyBatis Plus like参数为百分号%查询结果异常原因分析及解决