我有一个SpringBoot应用程序,其中有一个接口继承自CrudRepository
。
@Query("select cps from HotelPriceSummary cps where cps.price > 5 and cps.profilePercentage >= 70 ")
List<HotelPriceSummary> findMine(Pageable pageable);
我想知道是否可以从对象Pageable
中获取总页数。
我有一个SpringBoot应用程序,其中有一个接口继承自CrudRepository
。
@Query("select cps from HotelPriceSummary cps where cps.price > 5 and cps.profilePercentage >= 70 ")
List<HotelPriceSummary> findMine(Pageable 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();