当前位置: 首页 > 工具软件 > MVEL > 使用案例 >

MVEL表达式案例

羊舌自强
2023-12-01

MVEL表达式是一个功能强大的基于Java应用程序的表达式语言,由于业务需要,在springbbot中集成了部分功能,下面是一些测试案例:

@Test
    public void expressionTest(){
        String expression = "if (param > 0) {return \"person:[123,234,356]\"; } else if (param == -1) { return \"Minus one!\"; } else { return \"Something else!\"; }";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("param", 2);
        Object object = MVEL.eval(expression, paramMap);
        log.debug("-------------------------object:{}",object);
        System.out.println(object); // Greater than zero!
        String[] replace = object.toString().replace("person:", "").replace("[","").replace("]","").split(",");
        Arrays.asList(replace).forEach(item -> {
            log.debug("item:{}",item);
        });
        System.out.println("success");
    }

    /**
     * 基本解析表达式
     */
    @Test
    public void expressionTest1(){
        String expression ="foobar > 99";
        Map vars = new HashMap();
        vars.put("foobar",new Integer(100));
        // We know this expressionshould return a boolean.
        Boolean result = (Boolean) MVEL.eval(expression, vars);
        if (result.booleanValue()) {
            System.out.println("Itworks!");
        }
    }

    /**
     * 变量判空
     */
    @Test
    public void expressionTest2(){
        String expression = "a == empty && b == empty";

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("a", "");
        paramMap.put("b", null);
        Object object = MVEL.eval(expression, paramMap);
        System.out.println(object); // true
    }

    /**
     *
     */
    @Test
    public void expressionTest3(){
        HashMap<Object, Object> srcMap = new HashMap<>();
        srcMap.put("name","zs");
        srcMap.put("age",10);
        srcMap.put("sex","女");
        //字段映射关系
        HashMap<String, String> mapping = new HashMap<>();
        mapping.put("name","name");
        mapping.put("age","age");
        //这里先把当前年份写死为2019
        mapping.put("birthYear","2019-age");
        //目标对象
        HashMap<Object, Object> targetMap = new HashMap<>();
        //k为目标表字段,v为转换规则
        mapping.forEach((k,v)->{
            Object reValue = MVEL.eval(v,srcMap);
            System.out.println(v+":"+reValue);
            targetMap.put(k,reValue);
        });
        System.out.println("源对象"+srcMap); //源对象{sex=女, name=zs, age=10}
        System.out.println("目标对象"+targetMap); //目标对象{birthYear=2009, name=zs, age=10}
    }

    /**
     * 多语句分号隔开   返回值无需 return
     */
    @Test
    public void expressionTest4(){
        String expression = " a = 10; b = (a = a * 2) + 10;b;";
        Map<String, Object> paramMap = new HashMap<>();
        //即便map没有任何内容 ,也需要传map 不然会报错
        Object object = MVEL.eval(expression,paramMap );
        System.out.println(object); // true
    }

    /**
     * null 和nil 都表示空
     */
    @Test
    public void expressionTest5(){
        String expression = "a == null && b == nil";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("a", null);
        paramMap.put("b", null);
        Object object = MVEL.eval(expression, paramMap);
        System.out.println(object); // true
    }

    /**
     * 集合遍历
     */
    @Test
    public void expressionTest6(){
        List<Integer> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        list.add(0);
        list.add(1);
        list.add(2);
        String exp = "list[0]";
        String exp2 = "map2.list";
        map2.put("list",list);
        map.put("list",list);
        map.put("map2",map2);
        Object eval = MVEL.eval(exp2,map);
        System.out.println(eval);
    }

    /**
     * Strings as Arrays
     */
    @Test
    public void expressionTest7(){
        String foo = "My String";
        String exp = "foo[0]";
        Map<String,Object> map = new HashMap<>();
        map.put("foo",foo);
        Object eval = MVEL.eval(exp,map);
        System.out.println(eval);
    }

    /**
     *  三元声明
     */
    @Test
    public void expressionTest8(){
        String expression = "num > 0 ? 'Yes' : 'No'";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("num", 2);
        Object object = MVEL.eval(expression, paramMap);
        System.out.println(object);
    }

我是空谷有来人,谢谢大家支持!

 类似资料: