在Springboot中使用Pageable将整数作为字符串存储在数据库中时如何排序

问题描述 投票:0回答:1

我一直在寻找并尝试各种方法来使时间戳从最低数字到最高数字排序。我正在使用 postgres 和 spring boot 3.2.2。我遇到的问题是

startTimestamp
作为字符串存储在数据库中,我认为这就是默认排序无法正确工作的原因。我也尝试过
Order.asc("startTimeStamp")
但 postgres 也发送了类似的响应。

这是我的实体

public class YoutubeVideo {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID Id;
    private String videoId;
    private String startTimeStamp;
    private String fullYouTubeUrl;

我的服务中有这个功能,可以从 JpaRepository 中提取数据

 public Page<YoutubeVideoDto> findAllByYtVideoId(Integer pageNum, String videoId) {
                Page<YoutubeVideo> dbResponse = youtubeVideoRepository
                                .findAllByYtVideoId(PageRequest.of(pageNum, NUMBER_OF_ITEMS_PER_PAGE, Sort.by("startTimeStamp").ascending()), videoId);
                                                                

                return dbResponse.map(this::mapYoutubeVideoDto);

        };

JPA 存储库

public interface YoutubeVideoRepository extends JpaRepository<YoutubeVideo, UUID> {

    Page<YoutubeVideo> findAllByYtVideoId(Pageable pageable, String videoId);

}

我尝试使用 Sort.by("startTimeStamp").ascending 但这是我请求 API 时得到的。

{
    "content": [
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "938",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=938",
            "id": "1423cb48-2bb4-42e3-a6a9-78b5b5995198"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "620",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=620",
            "id": "d97ccc9e-5aed-47ae-a137-9ab2033b437e"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "6023",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=6023",
            "id": "73517417-6d62-4511-9e96-68a0232d6999"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "5788",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=5788",
            "id": "69960697-6162-4214-9f2f-b3f787f86989"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "5460",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=5460",
            "id": "f7446385-4802-4666-8021-ed75de826a5f"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "451",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=451",
            "id": "70ed415f-23f8-4ea3-be1a-4b6bfeb83ce0"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3979",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3979",
            "id": "51634c42-503f-4c49-9d5e-b4e3bebc3861"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3679",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3679",
            "id": "e13419be-ed68-4995-9212-65e6428f613a"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3421",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3421",
            "id": "6826b818-73ac-4410-9a61-cafa38c46f92"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3126",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3126",
            "id": "359a55e9-e2e1-4300-b576-4613b70b2a93"
        }
    ],
    "pageable": {
        "pageNumber": 0,
        "pageSize": 10,
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "totalPages": 2,
    "totalElements": 15,
    "last": false,
    "first": true,
    "size": 10,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "numberOfElements": 10,
    "empty": false
}

startTimeStamp 不是从最低到最高的顺序。

我浏览过网络,也浏览过 stackoverflow,但找不到我要找的东西。我怀疑我需要将

startTimestamp
转换为整数,然后使用默认排序器?我想需要实现自定义排序器?或者有人有我正在监督的其他解决方案吗?

谢谢你。

java spring-boot sorting
1个回答
0
投票

问题是因为时间戳存储为字符串,并且排序将按字典顺序而不是数字进行。如果您仅在此字段中存储整数,我建议将类型更改为 Integer。

但是,如果无法更改此字段的类型,您可以编写一个自定义查询,它将时间戳转换为整数以用于排序目的:

@Query("SELECT v FROM YoutubeVideo v WHERE v.videoId = :videoId ORDER BY CAST(v.startTimeStamp AS int) ASC")
Page<YoutubeVideo> findByVideoIdSortedByStartTimeStamp(String videoId, Pageable pageable);
© www.soinside.com 2019 - 2024. All rights reserved.