If startDate not null and endDate not null = beetwen pageable
If startDate is null and endDate not null = before pageable
If startDate not null and endDate is null = after pageable
我也想在請求中使用 userIdentifier,但我可能不得不將它分成 2 個請求,當為空時,當有值時
我不知道如何在這些條件下提出請求
@Query("SELECT m FROM notification m WHERE :userIdentifier is null")
Flux<Notification> findAllByUserIdentifier(Timestamp startDate, Timestamp endDate, int page,
int size);
uj5u.com熱心網友回復:
您可以嘗試像這樣短路您的查詢:
@Query("SELECT n FROM notification n WHERE "
"(:start is null or n.date >= :start) "
"and (:end is null or n.date <= :end) ")
Page<Notification> findAllByUserIdentifier(@Param("start") Timestamp startDate, @Param("end") Timestamp endDate, Pageable pageable);
在這里,我們正在檢查您想要的條件并僅在存在實際引數時使用過濾器,Pageable引數是您需要傳遞給方法以Page從資料庫中獲取 a 的物件。您可以像這樣創建該引數:PageRequest.of(page, size)page 是您要查詢的當前頁面, size 是要回傳的記錄數。
回應Page將具有以下用于提取頁面值的方法:
getTotalElements獲取查詢的總元素數getContent獲取查詢選擇的物體串列
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490378.html
