Mybatis if判断中使用了Ognl关键字导致报错解决方法

mybatis xml中使用OGNL解析参数,如果直接使用了关键字则会导致解析失败。
常见的关键字有:

字段    mybatis关键字
bor      (字符|)的英文 
xor      字符^的英文
and      字符&&
band     字符&
ed       字符==
neg      字符!=
lt       字符<
gt       字符>
lte      字符<=
gte      字符>=
shl      字符<<
shr      字符>> 
ushr     字符>>> 

if判断中使用了Ognl关键字导致报错 报错日志

Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'shr ! = ''and  shr !=null'. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: 'shr ! = ''and  shr !=null'[org.apache.ibatis.ognl.ParseException: Encountered " "shr"
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "shr" ...

解决方案


    public void testOngl() throws OgnlException {
        OgnlContext context = new OgnlContext();
        UserDto dto = new UserDto();
        dto.setId("TMS02000000000000000000041855112");
        dto.setShr("审核人1");
        context.setRoot(dto);

        // 报错写法
        System.out.println(Ognl.getValue("shr != null and shr != ''", context, context.getRoot()));
        System.out.println(Ognl.getValue("shr", context, context.getRoot()));

        // 正确写法
        System.out.println("#this" +        Ognl.getValue("#this['shr']", context, context.getRoot()));
        System.out.println("#context"+   Ognl.getValue("#context['shr']", context, context.getRoot()));
        System.out.println("#root"+        Ognl.getValue("#root['shr']", context, context.getRoot()));
        System.out.println("nsztId"+       Ognl.getValue("nsztId", context, context.getRoot()));

    }
posted @ 2024-05-09 10:59  原子切割员  阅读(136)  评论(0编辑  收藏  举报