Jpa分页和排序

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2020/03/jpa-fen-ye-he-pai-xu.html

Jpa分页和排序:

    public Page<Post> page(int page, int size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "postDate");
        Pageable pageable = PageRequest.of(page, size, sort);
        return postRepository.findAll(pageable);
    }

如果使用多个字段排序,可以如下使用:

    Sort desc = new Sort(Sort.Direction.DESC, "id", "age");
    Sort asc = new Sort(Sort.Direction.ASC, "name");
    Sort sort = desc.and(asc); // and 后的是次要排序属性
    Iterable<Person> people2 = personPageSortRepo.findAll(sort1);

发表评论