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();
}
};
个人微信公众,经常更新一些实用的干货:
上一篇: 同桌狠二玩手机