【问题标题】:Pagable Sort.Order ignore casePageable Sort.Order 忽略大小写
【发布时间】:2021-05-18 05:11:16
【问题描述】:

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

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

我得到了这个结果:

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

代替:

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

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

PagingAndSortingRepository how to sort case insensitive?

Spring Data JPA: case insensitive 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));
    
     }
    

可能是什么问题?

【问题讨论】:

    标签: java spring spring-boot jpa pageable


    【解决方案1】:

    Pageable 接口支持排序,所以不知道为什么要同时传递 PageableSort.Order 参数(我不知道是否支持)。如果没有在 URL 上定义排序,Pageable 上的 getSort() 应该返回 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多