1. 程式人生 > >springboot-動態查詢、分頁和排序

springboot-動態查詢、分頁和排序

 /**
     * 獲取企業集合
     *
     * @return
     */
    public List<Qy> getQy(String qymc, Integer page, Integer size) {
        List<String> list = new ArrayList<>();

        Specification<Qy> specification = new Specification<Qy>() {

            @Override
            public Predicate toPredicate(Root<Qy> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(qymc)) {
                    predicates.add(criteriaBuilder.like(root.get("qymc"), "%" + qymc + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Sort sort = new Sort(Sort.Direction.ASC, "qymc");
        Pageable pageable = new PageRequest((page - 1) / size, size, sort);
        Page<Qy> qyPage = this.qyRepositpry.findAll(specification, pageable);
        List<Qy> qyList = qyPage.getContent();
        return qyList;
    }