我有一个SpringBoot应用程序和一个从CrudRepository
扩展的界面
@Query("select cps from HotelPriceSummary cps where cps.price > 5 and cps.profilePercentage >= 70 ")
List<HotelPriceSummary> findMine(Pageable pageable);
我想知道是否可以从对象Pageable
中获取总页数
您可以使用返回总元素的Page<T>
接口。
长 -
getTotalElements()
返回元素的总数。
您可以在文档中找到更多信息:页面和PageImpl.
在您的示例中,它应该像这样工作:
@Query("select cps from HotelPriceSummary cps where cps.price > 5 and cps.profilePercentage >= 70 ")
Page<HotelPriceSummary> findMine(Pageable pageable);
您应该从 PagingAndSortingRepository
而不是 CrudRepository
扩展存储库。然后方法查询应该是:
@Query("select cps from HotelPriceSummary cps where cps.price > 5 and cps.profilePercentage >= 70 ")
Page<HotelPriceSummary> findMine(Pageable pageable);
之后,您可以使用来自服务(或任何您想要的内容(的查询,然后在响应中getTotalPages()
。例:
int page = 0, pageSize = 10;
Page<HostelPriceSummary> response = myRepo.findMine(PageRequest.of(page, pageSize));
int numberOfPages = response.getTotalPages();