【发布时间】:2020-11-23 20:16:16
【问题描述】:
在我的项目中,我有一个扩展 CrudRepository 的存储库。在里面我有一个自定义查询:
public interface CustomerRepository extends CrudRepository<Customer, Long> {
@Query("select * from person where firstname = :firstname")
List<Customer> findByFirstname(@Param("firstname") String firstname, Pageable pageable);
}
在我的服务类中,我尝试将列表放入可分页的对象中,例如:
... getPageableCustomer(String firstname, Pageable pageable){
// => By using "Sol" I got 90 matching entries
List<Customer> custList = customerRepository.findByFirstname(firstname, pageable);
Page<Customer> custPage = new PageImpl<Customer>(custList, pageable, custList.size());
return custPage;
}
返回值包括完整的列表“custList”。获取具有指定偏移量和大小的可分页对象的最佳方法是什么?
一种选择是使用
customer.subList(fromIndex, toIndex)
但这感觉不对。也因为加载了列表中的所有数据,而不是仅仅通过可分页参数化的大小和偏移量获取数据。
备注:如果在 Repository 中使用 Page,我会得到 org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 88
这里还有一个关于 Jira 的开放改进: https://jira.spring.io/browse/DATAJDBC-554?filter=-3
希望得到一些帮助...
【问题讨论】:
标签: spring-boot spring-data-jdbc pageable