我使用Spring的可分页对列进行排序。
一个工作示例如下:
Pageable pageable = PageRequest.of(0, countOfBookData.intValue(), getSortingDirection(sortingOrder), sortingField);
其中sorting order=ASC和sorting Field=bookName
这是查询
@Query("SELECT bs FROM BookSummary bs WHERE bs.bookId=:bookId")
List<Books> getBookDetails(@Param("bookId") Integer bookId, Pageable pageable)
但是当我需要在自定义我的自定义查询上执行这种排序时,我卡住了。所以我不知道如何使用Pag可分页来执行以下自定义查询的排序:
Public List<Tuple> getBookDetails(Integer bookId){
String query = "SELECT book.bookCd as bookCode, "
+ "book.name as bookName"
+ "FROM Book book WHERE book.bookId=:bookId";
return entityManager.createQuery(query , Tuple.class).setParameter("bookId", bookId).getResultList();
}
与第一个自定义查询相同,但使用投影,例如:
public interface BookDetails {
String getBookCode();
String getBookName();
}
@Query("select b.bookCd as bookCode, b.name as bookName from Book b where b.bookId = ?1")
List<BookDetails> getBookDetails(Integer bookId, Pageable pageable);
请注意,投影方法名称必须与查询中相应的别名匹配。
或者没有查询:
List<BookDetails> getAllById(Integer bookId, Pageable pageable);