mybatis if 表达式计算结果有误
代码
<when test="flag != null and flag == 0">
变量flag式字符串类型 flag=‘’;
- 允许结果: true
- 期待允许结果 fasle
原因
在表达式计算逻辑中,如果数据类型的比较 空字符串和0被看作了相等
详细代码
package org.apache.ibatis.scripting.xmltags;
/**
* @author Clinton Begin
*/
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;
}
}
public class ASTEq extends ComparisonExpression {
private static final long serialVersionUID = -7129666227440957636L;
public ASTEq(int id) {
super(id);
}
public ASTEq(OgnlParser p, int id) {
super(p, id);
}
protected Object getValueBody(OgnlContext context, Object source) throws OgnlException {
Object v1 = this.children[0].getValue(context, source);
Object v2 = this.children[1].getValue(context, source);
return OgnlOps.equal(v1, v2) ? Boolean.TRUE : Boolean.FALSE;
}
public String getExpressionOperator(int index) {
return "==";
}
public String getComparisonFunction() {
return "org.apache.ibatis.ognl.OgnlOps.equal";
}
}
解决方案
<when test="flag != '' and flag == 0">