Jexl解析表达式

来源:互联网 发布:kk唱响抢红包软件 编辑:程序博客网 时间:2024/06/07 08:31

例1:

public static void main(String[] args) {    Map<String, Object> params =new HashMap<>();    params.put("id", 1122);    String sql=" select * from Table where id=${id}";    JexlContext context = new MapContext(params);    JexlEngine jexl = new JexlEngine();    UnifiedJEXL ujexl = new UnifiedJEXL(jexl);    UnifiedJEXL.Expression expr = ujexl.parse(sql);    String result = expr.evaluate(context).toString();  }


sql:

执行前: select * from Table where id=${id}

执行后: select * from Table where id=1122


例2:

public static void main(String[] args) {    Expression expr;    JexlContext ctxt = new MapContext();    JexlEngine jexl = new JexlEngine();    // 表达式    String exps = "if(c1>c2) {c3=5}";    expr = jexl.createExpression(exps);    ctxt.set("c1", 2);    ctxt.set("c2", 1);    expr.evaluate(ctxt);    System.out.println(ctxt.get("c3"));  }
ctxt:

执行前: 无c3

执行后: c3 = 5


总结:

JexlEngine  创建表达式

JexlContext  存放参数

需要jar包支持:commons-jexl-2.1.1.jar



0 0