【问题标题】:Issue in Pagination Spring data分页 Spring 数据中的问题
【发布时间】:2016-05-30 03:36:20
【问题描述】:

我正在尝试在服务中实现分页。使用 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

【问题讨论】:

    标签: java spring pagination cassandra spring-data


    【解决方案1】:

    我想你应该在这里使用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);
    }
    

    并通过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

    【讨论】:

    • 当我尝试在我的存储库中使用可分页对象时,我收到了错误 org.springframework.dao.InvalidDataAccessApiUsageException:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory 中既不支持切片也不支持页面查询.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    • 有什么想法或解决方案吗?
    • 啊,cassandra 可能不支持它。 jira.spring.io/browse/DATACASS-56
    • 此问题的任何替代解决方案?
    • 也许你可以试试 devjavasource.com/cassandra/cassandra-paging-java-example 但目前没有针对 Spring Data 的解决方案
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多