源码分析MyBatis对数值(int、double)类型进行test判断的误区

问题描述

在如下判断中,如果type类型为int,那么对于type != ''部分判断会出现一些问题,比如当type为0时,type != ''结果为false,当type为其他数值时type != ''结果为true

<if test="type != null and type != ''"> 
</if>

主要问题就在于当type为0时,if语句判断的结果为false,导致本期望执行条件中的语句却没有执行。

问题分析

来到解析if标签的部分进行分析

public class IfSqlNode implements SqlNode {
  private final ExpressionEvaluator evaluator;
  private final String test;
  private final SqlNode contents;

  public IfSqlNode(SqlNode contents, String test) {
    this.test = test;
    this.contents = contents;
    this.evaluator = new ExpressionEvaluator();
  }

  @Override
  public boolean apply(DynamicContext context) {
    if (evaluator.evaluateBoolean(test, context.getBindings())) {
      contents.apply(context);
      return true;
    }
    return false;
  }

}

判断逻辑在evaluator.evaluateBoolean中,test就是"type != null and type != ''"

public boolean evaluateBoolean(String expression, Object parameterObject) {
  Object value = OgnlCache.getValue(expression, parameterObject);
  if (value instanceof Boolean) {
    return (Boolean) value;
  }
  if (value instanceof Number) {
    return new BigDecimal(String.valueOf(value)).compareTo(BigDecimal.ZERO) != 0;
  }
  return value != null;
}

继续进入OgnlCache.getValue(expression, parameterObject)方法

public static Object getValue(String expression, Object root) {
  try {
    Map context = Ognl.createDefaultContext(root, MEMBER_ACCESS, CLASS_RESOLVER, null);
    return Ognl.getValue(parseExpression(expression), context, root);
  } catch (OgnlException e) {
    throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e);
  }
}

关键解析在Ognl.getValue(parseExpression(expression), context, root)方法,先看parseExpression(expression)方法

private static Object parseExpression(String expression) throws OgnlException {
  Object node = expressionCache.get(expression);
  if (node == null) {
    node = Ognl.parseExpression(expression);
    expressionCache.put(expression, node);
  }
  return node;
}

这个方法实际上就是解析了一下"type != null and type != ''"这段内容,然后返回给Ognl.getValue方法来处理

验证

经过上面的源码分析后,可以判断if中的内容主要是通过Ognl表达式来决定,所以我们就来验证Ognl表达式对于"type != null and type != ''"这个内容的判断

解析表达式

public class Main {
    public static void main(String[] args) throws OgnlException {
        System.out.println(Ognl.parseExpression("0 != null and 0 != ''"));
    }
}

在这里插入图片描述

执行解析后表达式

可以看到"0 != null and 0 != ''"先是被解析为了(0 != null) && (0 != "")这样一段内容,接着我们放入Ognl.getValue方法中再来看看结果

public class Main {
    public static void main(String[] args) throws OgnlException {
        Object value = Ognl.getValue(Ognl.parseExpression("0 != null and 0 != ''"), null);
        System.out.println(value);
    }
}

在这里插入图片描述
结果为false,换成其他值再看看结果。

public class Main {
    public static void main(String[] args) throws OgnlException {
        Object value1 = Ognl.getValue(Ognl.parseExpression("1 != null and 1 != ''"), null);
        Object value2 = Ognl.getValue(Ognl.parseExpression("2 != null and 2 != ''"), null);
        Object value3 = Ognl.getValue(Ognl.parseExpression("-1 != null and -1 != ''"), null);
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
    }
}

在这里插入图片描述
结果都为true,也就是说应该只有为0时结果才为false。

分别测试两个条件

现在我们把两个条件分开,分别进行测试

先来测试"type != null"

public class Main {
    public static void main(String[] args) throws OgnlException {
        Object value1 = Ognl.getValue(Ognl.parseExpression("1 != null"), null);
        Object value2 = Ognl.getValue(Ognl.parseExpression("2 != null"), null);
        Object value3 = Ognl.getValue(Ognl.parseExpression("-1 != null"), null);
        Object value4 = Ognl.getValue(Ognl.parseExpression("0 != null"), null);
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
        System.out.println(value4);
    }
}

在这里插入图片描述

再来测试"type != ''"

public class Main {
    public static void main(String[] args) throws OgnlException {
        Object value1 = Ognl.getValue(Ognl.parseExpression("1 != ''"), null);
        Object value2 = Ognl.getValue(Ognl.parseExpression("2 != ''"), null);
        Object value3 = Ognl.getValue(Ognl.parseExpression("-1 != ''"), null);
        Object value4 = Ognl.getValue(Ognl.parseExpression("0 != ''"), null);
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
        System.out.println(value4);
    }
}

在这里插入图片描述

测试后表明,错误的结果应该是由"type != ''"判断造成的。

查询Ognl官方文档验证

在这里插入图片描述
文档中已经说明,当if中的对象为Number类型时,将使用双精度浮点类型与0比较,如果等于0则视为false,否则视为true。

问题解决

通过前面的测试验证和官方文档说明可以看出,主要问题就是Number类型的判断,我们只需要改写成如下这样就可以了。
不过,如果type为数值类型,本就不应该在让它去和空字符串进行比较,这在Java语言中编译就无法通过。

<if test="type != null"> 
</if>
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值