我想在运行时查询添加排序列。我已经使用查询注释为构建查询。我已经使用参数“sortCol”查询中添加排序列。但不知何故,查询注释无法识别“sortCol”帕尔马,因此不使用它。不给任何错误也是如此。
SELECT
*
FROM
request r
WHERE
r.asset_type IN (
?, ?, ?
)
AND r.request_type IN (
?, ?
)
AND r.status IN (
?, ?, ?, ?
)
AND r.assign_to = ?
AND (
r.request_custom_id LIKE ?
OR r.project_name LIKE ?
OR r.asset_type LIKE ?
OR r.request_type LIKE ?
)
ORDER BY
? limit ?"
在此之后,通过列名的顺序缺失,这是我补充道。在运行时:
@Query(value ="SELECT * FROM request r WHERE r.asset_type IN (:assetType) AND r.request_type IN (:requestType) AND r.status IN (:statusList) AND r.assign_to = :assignTo AND ( r.request_custom_id LIKE %:searchTerm% OR r.project_name LIKE %:searchTerm1% OR r.asset_type LIKE %:searchTerm2% OR r.request_type LIKE %:searchTerm3% ) ORDER BY :sort",nativeQuery = true)
Page<Request> findRequestdata(Pageable pageable,
@Param("assetType") List<String> assetType,
@Param("requestType") List<String> requestType,
@Param("statusList") List<Integer> statusList,
@Param("assignTo") Long assignTo,
@Param("searchTerm") String searchTerm,
@Param("searchTerm1") String searchTerm1,
@Param("searchTerm2") String searchTerm2,
@Param("searchTerm3") String searchTerm3,
@Param("sort") String sortCol);
所有帕尔马正确设置除“sortCol”。请建议我在哪里丢失任何东西。
您已经传递了一个可分页对象的方法。
所以,当你创建的页面还可以添加排序:
Pageable sortedByName = PageRequest.of(0, 3, Sort.by("name"));
findRequestdata(sortedByName, ...)
在这里阅读更多:https://www.baeldung.com/spring-data-jpa-pagination-sorting