倒序排序 空放在最下

发布时间 2023-04-20 15:51:17作者: 你的小可爱吖

Null Last 不适用于 spring 数据 jpa

这是我正在使用的代码。

服务层代码:

Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "user_name").nullsLast());
Pageable pageable = PageRequest.of(0, 10, sort);
userRepository.findAllUsers(pageable)

 

回购层代码:

@Query(value = "Select * from users", nativeQuery = true)
Page<User> findAllUsers(Pageable pageable);

 

预期/实际输出:

Expected: Null values should come at the last
Actual: Null values are coming at the top

 

使用

Spring Boot version: **2.1.6.RELEASE**
Database Postgres version: **42.2.5**

 

我搜索了很多博客,这种方法写这个是不生效的。

可以在 application.properties 中使用以下配置

spring.jpa.properties.hibernate.order_by.default_null_ordering=last