关于mybatis:MyBatis-if-标签的坑居然被我踩到了

6次阅读

共计 3834 个字符,预计需要花费 10 分钟才能阅读完成。

事件的起因是这样的,需要是按条件查数据而后给前端展现就行了,写的时候想着挺简略的,不就是应用 MyBatis 动静 SQL 去查问数据吗?

事实还是很残暴的,等我写完上完 UAT 后,前端同学说依据 state 查的数据与现实的数据不统一,这个 state 过后设计时只有两个值:01

/**
 * 数据状态
 */
@Range(min = 0, max = 1, message = "状态只能为 0(未解决),1(已解决)")
private Integer state;

现实状况下通过前端传递过去的值,而后进行 sql 查问就能够了:

<if test="req.state != null and req.state !=''">
            AND md.state = #{req.state}
</if>

下面的 sql 首先判断 state 不为空且 不为空字符串 时,而后增加比拟 state 字段。初步看下来 if 判断没什么问题,然而我传递进去的 req.stateInteger型的,认真查看 req.state != null 没故障,而后发现 req.state != '' 应用 Integer 与空字符串做比拟。

前端在查问的时如果没有传递 req.statereq.state != null 这里不会满足,然而前端传递了一个 0 过去的时候 req.state != '' 竟然返回的是 false 也就是说在MyBatis 的 if 语法中 0 是等于空字符串的

{"state": 0}

这样的比拟没有报错,也是有点想不通了,没方法只能去看 MyBatis 源码找出这起因。

查看 MyBatis 源码

MyBatis 其余源码的查找过程就不具体说了,这里间接找到 XMLScriptBuilder 类,找到 if 语法的解析过程,而后一步步的探索 0 == '' 的起因。XMLScriptBuilder会解析 trimif 等 MyBatis 反对的语法,它的解析原理是通过 NodeHandler 来别离解析不同的标签:

  private void initNodeHandlerMap() {nodeHandlerMap.put("trim", new TrimHandler());
    nodeHandlerMap.put("where", new WhereHandler());
    nodeHandlerMap.put("set", new SetHandler());
    nodeHandlerMap.put("foreach", new ForEachHandler());
    nodeHandlerMap.put("if", new IfHandler());
    nodeHandlerMap.put("choose", new ChooseHandler());
    nodeHandlerMap.put("when", new IfHandler());
    nodeHandlerMap.put("otherwise", new OtherwiseHandler());
    nodeHandlerMap.put("bind", new BindHandler());
  }

因为是不正解的语法是 if 标签,查看 IfHandler 就好了,其余当初略过就好。

 private class IfHandler implements NodeHandler {public IfHandler() {// Prevent Synthetic Access}

    @Override
    public void handleNode(XNode nodeToHandle, List<SqlNode> targetContents) {MixedSqlNode mixedSqlNode = parseDynamicTags(nodeToHandle);
      String test = nodeToHandle.getStringAttribute("test");
      IfSqlNode ifSqlNode = new IfSqlNode(mixedSqlNode, test);
      targetContents.add(ifSqlNode);
    }
  }

MyBatis 会将 if 标签形象成IfSqlNode

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;
  }

}

终于有一点眉头了,MyBatis 会将 if 标签的 test 属性应用 ExpressionEvaluator 测试一下是否为 true 或者为false

public class ExpressionEvaluator {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;
  }

  public Iterable<?> evaluateIterable(String expression, Object parameterObject) {Object value = OgnlCache.getValue(expression, parameterObject);
    if (value == null) {throw new BuilderException("The expression'" + expression + "'evaluated to a null value.");
    }
    if (value instanceof Iterable) {return (Iterable<?>) value;
    }
    if (value.getClass().isArray()) {// the array may be primitive, so Arrays.asList() may throw
        // a ClassCastException (issue 209).  Do the work manually
        // Curse primitives! :) (JGB)
        int size = Array.getLength(value);
        List<Object> answer = new ArrayList<Object>();
        for (int i = 0; i < size; i++) {Object o = Array.get(value, i);
            answer.add(o);
        }
        return answer;
    }
    if (value instanceof Map) {return ((Map) value).entrySet();}
    throw new BuilderException("Error evaluating expression'" + expression + "'.  Return value (" + value + ") was not iterable.");
  }

}

最初失去论断:Mybatis 应用的 Ognl 表达式 来获取 test 属性的值

最终论证

曾经晓得 MyBatis 外部是应用的 Ognl 表达式,是不是 Ognl 表达式 的引起的呢?实际一下就晓得了,先引入依赖:

<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
    <groupId>ognl</groupId>
    <artifactId>ognl</artifactId>
    <version>2.7.3</version>
</dependency>

写程序测试:

    public static void main(String[] args) {Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("state", "0");
        Object value = OgnlCache.getValue("state != null and state !=''", objectMap);
        System.out.println(value);

    }

下面程序输入的真的是true。。。

总结

真是脑袋抽筋啊,Integer还判断是否为空字符串。。。

记录此坑,心愿对大家有所帮忙。

举荐

  • 如果 MySQL 磁盘满了,会产生什么?
  • TCP 三次握手、四手挥手,这样说你能明确吧!
  • Netflix 微服务架构设计解析

欢送关注公众号:架构文摘,取得独家整顿 120G 的收费学习资源助力你的架构师学习之路!

公众号后盾回复 arch028 获取材料

正文完
 0