【JPA】JPA构建动态查询条件

展示几种动态查询条件:= 、in、like、<>

  • 请忽略我的map传值

			//分页
 			Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "createdTime"));
  
            //构建条件查询
            Specification<Student> specification = (root, query, cb) -> {

                //查询条件集合
                List<Predicate> list = new ArrayList<>();
 
                //传入集合 in
                String studentId= params.get("studentId");
                if (StringUtils.isNotBlank(studentId)) {
                    if (payStatus.contains(",")) {
                        CriteriaBuilder.In<Integer> in = cb.in(root.get("studentId"));
                        for (String os : studentId.split(",")) {
                            in.value(Integer.valueOf(os.trim()));
                        }
                        list.add(in);
                    } else {
                        Predicate predicate = cb.equal(root.get("studentId"), Integer.parseInt(studentId.trim()));
                        list.add(predicate);
                    }
                }

                //模糊查询 like
                String studentName= params.get("studentName");
                if (StringUtils.isNotBlank(studentName)) {
                    Predicate predicate = cb.like(root.get("studentName"), "%" + studentName+ "%");
                    list.add(predicate);
                }

                //精确查询 =
                String subject= params.get("subject");
                if (StringUtils.isNotBlank(subject)) {
                    Predicate predicate = cb.equal(root.get("subject"), subject);
                    list.add(predicate);
                }

                //时间范围查询 <= =>
                String startTime = params.get("startTime");
                String endTime = params.get("endTime");
                if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                    // 创建时间范围查询条件
                    Predicate startPredicate = cb.greaterThanOrEqualTo(root.get("orderTime"), Long.valueOf(startTime));
                    Predicate endPredicate = cb.lessThanOrEqualTo(root.get("orderTime"), Long.valueOf(endTime));

                    // 组合两个条件
                    Predicate rangePredicate = cb.and(startPredicate, endPredicate);
                    list.add(rangePredicate);
                } else {
                    // 单独处理开始时间或结束时间
                    if (StringUtils.isNotBlank(startTime)) {
                        Predicate predicate = cb.greaterThanOrEqualTo(root.get("orderTime"), Long.valueOf(startTime));
                        list.add(predicate);
                    }
                    if (StringUtils.isNotBlank(endTime)) {
                        Predicate predicate = cb.lessThanOrEqualTo(root.get("orderTime"), Long.valueOf(endTime));
                        list.add(predicate);
                    }
                }

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

			//查询接口
            Page<Student> all = orderRepository.findAll(specification, pageable);

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