Spring jpa的in查询

import javax.persistence.criteria.CriteriaBuilder.In;


Specification<Order> spec = new Specification<Order>() {

    @Override
    public Predicate toPredicate(Root<Order> root,CriteriaQuery<?> query,     CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
//方式1,使用In
In<String> in = cb.in(root.get("approvalStatus").as(String.class));
Iterator<String> iterator = ((List<String>)parameters.get("approvalStatus")).iterator();
while (iterator.hasNext()) {
in.value(iterator.next());
}
list.add(in);

//方式2
Expression<String> exp = root.get("approvalStatus").as(String.class);       
list.add(exp.in((List<String>)parameters.get("approvalStatus")));

    Predicate[] p = new Predicate[list.size()];
    query.where(cb.and(list.toArray(p)));
    return query.getRestriction();
   }

}

你可能感兴趣的:(spring,jpa)