JPA中查询(某个String属性在某个String数组中)

req.curPage当前页码, req.pageSize每页的条目数
PageRequest pageReques = PageRequest.of(req.curPage, req.pageSize);
        Specification<Enroll> spec = new Specification<Enroll>() {
            @Override
            public Predicate toPredicate(Root<Enroll> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();

                //使用JPA查询某个字段是否在数组中
                if (placeIds.size() > 0) {
                    Path<String> path = root.get("place_id");//定义查询的字段
                    CriteriaBuilder.In<String> in = criteriaBuilder.in(path);
                    for (int i = 0; i < placeIds.size(); i++) {
                        in.value(placeIds.get(i));//存入值
                    }
                    predicateList.add(in);
                }
                if (req.condition.verify_state != 0) {
                    predicateList.add(criteriaBuilder.equal(root.get("verify_state").as(Integer.class), req.condition.verify_state));
                }
                Predicate[] pre = new Predicate[predicateList.size()];
                pre = predicateList.toArray(pre);
                return criteriaQuery.where(pre).getRestriction();
            }
        };
        Long count = enrollRepository.count(spec);
        Page<Enroll> pages = enrollRepository.findAll(spec, pageReques);
        List<Enroll> enrollList = pages.getContent(); //查询的最后结果

 

上一篇:predicate接口


下一篇:代码分析平台CodeQL学习手记(十一)