Spring JPA Specification多条件查询
基础格式
Specification<Model> specification = (Specification<Model>) (root, query, criteriaBuilder) -> {
query.distinct(true);
List<Predicate> predicates = new ArrayList<>();
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
}
AND OR混合使用
Predicate and = criteriaBuilder.and(criteriaBuilder.equal(root.get("name"), name));
Predicate or = criteriaBuilder.or(criteriaBuilder.equal(root.get("name"), name));
predicates.add(criteriaBuilder.and(and));
predicates.add(criteriaBuilder.and(or));
关联查询
Join<Model, Associcate> ass = root.join("model", JoinType.INNER);
Predicate assPre = criteriaBuilder.like(ass.get("name"), "%" + name + "%");
In查询
SetJoin<Project, User> users = root.join(root.getModel().getSet("participants", User.class), JoinType.LEFT);
Predicate set = criteriaBuilder.or(users.in(oneUser));