欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Specification排序orderby

程序员文章站 2022-05-03 09:05:46
...

废话不多说直接贴代码


        Specification<Course> sf = new Specification<Course>() {
            @Override
            public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = Lists.newArrayList();

                //isDelete
                Predicate isDeletePredicate = criteriaBuilder.equal(root.get("isDelete"), Constant.NOT_DELETE);
                predicateList.add(isDeletePredicate);

                //isShelves
                Predicate isShelvesPredicate = criteriaBuilder.equal(root.get("isShelves"),Constant.COURSE_SHELVES_YES);
                predicateList.add(isShelvesPredicate);

                //categoryId
                if(categoryId!=null){
                    Predicate predicate = criteriaBuilder.equal(root.get("category").get("id"), categoryId);
                    predicateList.add(predicate);
                }

                //isRecommend
                if(isRecommend!=null){
                    Predicate predicate = criteriaBuilder.equal(root.get("isRecommend"), isRecommend);
                    predicateList.add(predicate);
                }

                /**
                 * order By
                 */
                Order weightOrder = criteriaBuilder.desc(root.get("weight"));
                Order createDateOrder = criteriaBuilder.desc(root.get("createDate"));

                return  criteriaQuery.orderBy(weightOrder, createDateOrder).where(predicateList.toArray(new Predicate[predicateList.size()])).getRestriction();

            }
        };

个人微信公众,经常更新一些实用的干货:
Specification排序orderby