我正在使用Spring的Pageable对列进行排序。
下面是一个工作示例:
Pageable pageable = PageRequest.of(0, countOfBookData.intValue(), getSortingDirection(sortingOrder), sortingField);
其中sortingOrder = ASC,sortingField = bookName
这是查询
@Query("SELECT bs FROM BookSummary bs WHERE bs.bookId=:bookId")
List<Books> getBookDetails(@Param("bookId") Integer bookId, Pageable pageable)
但是当我需要对Custom自定义查询执行这种排序时,我陷入了困境。 所以我不知道如何在下面的自定义查询中使用Pageable进行排序:
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();
}
答案 0 :(得分:0)
与第一个自定义查询相同,但使用Projection,例如:
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);