JavaSpring数据:按ID排序描述

JavaSpring数据:按ID排序描述,java,eclipse,spring-boot,spring-data,Java,Eclipse,Spring Boot,Spring Data,我只想让我的数据按ID子代排序,我不知道怎么做,这是我在服务层的代码 public Page<Facture> selectByPage(Pageable p) { Page<Facture> pagedResult = factureRepository.findAll(p); return pagedResult; } 公共页面按页面选择(可分页){ pagedResult=factureRepository.findAll(p); 返回pagedR

我只想让我的数据按ID子代排序,我不知道怎么做,这是我在服务层的代码

public Page<Facture> selectByPage(Pageable p) {
    Page<Facture> pagedResult = factureRepository.findAll(p);
    return pagedResult;
}
公共页面按页面选择(可分页){
pagedResult=factureRepository.findAll(p);
返回pagedResult;
}
您可以使用
排序(…).descending()
按字段降序排序

public Page<Facture> selectByPage(Pageable p) {
    // Here, replace "id" with your field name that refers to id.
    Pageable pSort = PageRequest.of(p.getPageNumber(), p.getPageSize(), Sort.by("id").descending());
    
    Page<Facture> pagedResult = factureRepository.findAll(pSort);
    return pagedResult;
}
公共页面按页面选择(可分页){
//在这里,将“id”替换为引用id的字段名。
Pageable pSort=PageRequest.of(p.getPageNumber(),p.getPageSize(),Sort.by(“id”).descending());
pagedResult=factureRepository.findAll(pSort);
返回pagedResult;
}

一个带有数据JPA规范的简单示例:

Sort sort = Sort.by(Sort.Direction.DESC, "id");
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
Specification specification = simpleSearchSpecification(bcpConsumerVo, bcpUserVo);
Page<BcpConsumer> bcpConsumers = bcpConsumerRepository.findAll(specification, pageable);
更多信息请点击这里
 private Specification simpleSearchSpecification(BcpConsumerVo bcpConsumerVo, BcpUserVo bcpUserVo) {
        return (Specification<BcpConsumer>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>(6);
            List<Predicate> orList = new ArrayList<>();
            if (Boolean.FALSE.equals(bcpUserVo.isAdmin())) {
                predicateList.add(criteriaBuilder.equal(root.get("owner"), bcpUserVo.getId()));
            }
            if (!StringUtils.isEmpty(bcpConsumerVo.getName())) {
                orList.add(criteriaBuilder.like(root.get("name"), "%" + bcpConsumerVo.getName() + "%"));
                orList.add(criteriaBuilder.like(root.get("authKey"), "%" + bcpConsumerVo.getName() + "%"));
            }
            predicateList.add(criteriaBuilder.or(orList.toArray(new Predicate[0])));
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }