我想使用筛选器创建可分页服务,但无法创建结果集
这是我的控制器
@GetMapping(path = "/PageFilter")
public DataResponsePagination<HistoryBankWrapper, HistoryBank> pageFilter(
@RequestParam("filter") String keyfilter ,
@RequestParam("sortField") String field ,
@RequestParam("sortOrder") String order ,
@RequestParam("page") int page,
@RequestParam("size") int size) {
return new DataResponsePagination<HistoryBankWrapper, HistoryBank>(historyBankService.findByFilter(keyfilter,field,order, page, size));
}
这是我的服务
public PaginationList<HistoryBankWrapper, HistoryBank> findByFilter(String keyfilter,String sortField,String sortOrder, int page, int size) {
Pageable paging = PageRequest.of(page, size);
Page<HistoryBank> historyPage = historyBankRepository.findAllFilter(keyfilter, sortField, sortOrder, paging);
List<HistoryBank> historyList = historyPage.getContent();
List<HistoryBankWrapper> historyWrapperList = toWrapperList(historyList);
return new PaginationList<HistoryBankWrapper, HistoryBank>(historyWrapperList, historyPage);
}
这是我存储库
@Query(value = "SELECT * FROM HISTORY_BANK WHERE :sortField LIKE '%' || :keyFilter || '%' ORDER BY :sortField :sortOrder",
countQuery = "SELECT count(*) FROM HISTORY_BANK",
nativeQuery = true)
Page<HistoryBank> findAllFilter(@Param("keyFilter") String keyfilter, @Param("sortOrder") String sortOrder,@Param("sortField") String sortField, @Param("paging") Pageable paging);
1条答案
按热度按时间fafcakar1#
抛弃你自己的方法,使用框架。使用规范来创建动态查询。
存储库应该扩展
JpaSpecificationExecutor
,然后从服务调用findAll(Specification, Pageable)
,服务应该准备构建查询所需的Specification
。然后,您的服务可以使用前面提到的
findAll
方法。您甚至可以通过修改控制器使其直接绑定到
Pageable
对象来简化此操作,这样就不必创建它。现在,您只需要创建
Specification
,然后沿着Pageable
即可。