Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

java - Apache CollectionUtils exist method in MyBatis XML

Getting an error while trying the below code in MyBatis XML. this is part of my select query, which pull fruit price. The fruit name should be APPLE and it should belongs to the fruit list.

<if test="@org.apache.commons.collections.CollectionUtils@exists(param.getFruitList(), n -> n.equals('APPLE'))">
     tbl.price
 </if>

Below is the error I got.

Invoking @handleDefaultException method: Caused by:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression '@org.apache.commons.collections.CollectionUtils@exists())'. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: @org.apache.commons.collections.CollectionUtils@exists

question from:https://stackoverflow.com/questions/66048416/apache-collectionutils-exist-method-in-mybatis-xml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

My understanding is that MyBatis's expression parser for XML is unable to handle lambdas/method references. You can try and create a helper method, something like

public static boolean helper(List<String> l) {
    return CollectionUtils.exists(l, "APPLE"::equals);
}

then refer it in the XML:

<if test="@some.package.Utils@helper(param.getFruitList())">
     tbl.price
</if>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...