在我的项目中,我有一个扩展 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
希望得到一些帮助...