0

(我正在使用 Micronaut)我有一个学生列表,我想按名称对其进行排序。我正在使用 Pageable 并排序为查询参数

http://localhost:8080/admin/students?page=0&size=10&sort=name,asc&status=graduated

我得到了这个结果:

  • 萨曼莎
  • 蒂姆
  • 安东尼奥
  • 大卫
  • 苏菲亚

代替:

  • 安东尼奥
  • 大卫
  • 萨曼莎
  • 苏菲亚
  • 蒂姆

我已经尝试了使用 Sort.Order 的选项,基于

PagingAndSortingRepository 如何排序不区分大小写?

Spring Data JPA:不区分大小写的 orderBy

我从我的网址中删除了排序并尝试了 Sort.Order 是否有效

Sort.Order order = new Sort.Order("name",Sort.Order.Direction.ASC, true);

http://localhost:8080/admin/students?page=0&size=10&status=graduated

但它没有,它也不做任何排序

脚步:

  1. 存储库,添加了 Sort.Order

     @Repository
     public interface StudentRepository extends PageableRepository<Student, Long> {
    
        Page<Student> findByGraduatedIsNull(Student.Status status, @Nullable Pageable p, Sort.Order sort);
    
     }
    
  2. 服务

     public Page<Student> getStudents(Student.Status status, Pageable pageable){
    
         Sort.Order order = new Sort.Order("name",Sort.Order.Direction.ASC, true);
         Page<Student> studentPage =
                         studentRepository.findByGraduatedIsNull(status, pageable, order);
         return studentPage.map(s -> studentTransform.toDto(s));
    
     }
    

可能是什么问题呢?

4

1 回答 1

0

Pageable接口支持排序,所以不确定为什么要同时传递 aPageableSort.Order参数(我不知道是否支持)。如果没有在 URL 上定义排序,则getSort()onPageable应该返回UNSORTED. 所以这可能比Sort.Order.

为什么不检查是否在请求中传递了排序,如果没有默认为您想要的排序,或者如果您不想允许传递排序,则直接覆盖它。

if (pageable.getSort == Sort.unsorted()) {
    List<Sort.Order> orders = new ArrayList<>();
    orders.add(new Sort.Order(Sort.Direction.ASC, "name").ignoreCase()); 
    pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(orders));
}
        
Page<Student> studentPage = studentRepository.findByGraduatedIsNull(status, pageable)
于 2021-02-24T14:39:30.023 回答