我正在尝试在服务中实现分页。使用Cassandra作为数据库 如果查询返回少量列表,我的代码工作正常。我只是过滤了数字关闭页面[硬编码为100 /不想硬编码而不是作为参数返回]和偏移值。所以对于每个请求我都不想进行查询。我确定应该有一种可能的方法来在查询本身中设置偏移值。 我的代码 -
@RequestMapping(value = "/find/customerreqbyperiod", method = RequestMethod.GET,
produces = "application/json")
@ResponseBody
public List<CustomerReq> findCustomerRequest(@RequestParam(value = "productid") final String pProductId,
@RequestParam(value = "dataperiod") final String pdataPeriod,
@RequestParam(value = "offset") final int offset)
{
List<CustomerReq> requestList = null;
try
{
requestList = requestService.findCustomerRequest(pProductId, pdataPeriod, offset);
if (requestList == null || offset > requestList.size()) return new ArrayList<CustomerRequestV2>();
int numberOfItems = 10;
int fromIndex = offset;
int toIndex = Math.min(offset + numberOfItems, requestList.size());
return requestList.subList(fromIndex, toIndex);
}
catch (final ServiceException e)
{
LOG.error(ERROR_RETRIEVING_LIST + pProductId, e);
}
return requestList;
}
@Repository -
@Query("select * from customer_request where product_id = ?0 and data_period = ?1")
List<CustomerReq> findByProductIdAnddataPeriod(String productId, String dataPeriod, int offset);
请提供您宝贵的建议。谢谢!-Saurav
答案 0 :(得分:0)
我认为你应该在这里使用Pageable
例如:
public interface ProductRepository extends JpaRepository<Product, Long> {
@Query("select cr from customer_request cr where cr.product_id = :productId and cr.data_period = :dataPeriod")
Page<CustomerReq> findByProductIdAnddataPeriod(@Param("productId") String productId, @Param("dataPeriod") String dataPeriod, Pageable pageable);
}
并使用offset:
传递PageRequest
PageRequest pageRequest = new PageRequest(pageNumber, offset);
Page<CustomerReq> customerReqsPage = findByProductIdAnddataPeriod("productId","datePeriod", pageRequest);
List<CustomerReq> customerReqs = customerReqsPage.getContent();
<强> UDP 强>: 不幸的是,Cassandra不支持它。 JIRA票证:jira.spring.io/browse/DATACASS-56