springDataJPA的Specification

@Test
    public void method17() {
        Specification spec=(root,cq,cb)->{
            Predicate like =1==0?cb.like(root.get("custName").as(String.class), "双%"):null;
            Predicate equal = 1==0?cb.equal( root.get("custLevel").as(String.class), "2"):null;
            Predicate equal2 = 1==0?cb.equal( root.get("custLevel").as(String.class), "1"):null;
          /*过滤空值*/
            List collect = Arrays.asList(new Predicate[]{like,equal,equal2}).parallelStream().filter(x->x!=null).collect(Collectors.toList());
            Predicate or = cb.or(collect.toArray(new Predicate[collect.size()]));
            return or;
        };
        List all = customerDao.findAll(spec);
        System.out.println(all);
    }

不想用一堆if去处理Predicate,所以用了三元表达式,但我觉得这样做更麻烦了,以后看看有什么更好的解决方案吧.

你可能感兴趣的:(springDataJPA的Specification)