【问题标题】:How can I do paging and sorting using spring data with Couchbase如何使用带有 Couchbase 的 spring 数据进行分页和排序
【发布时间】:2015-06-23 05:47:24
【问题描述】:

我正在尝试使用spring-data-couchbase 进行分页和排序,但似乎org.springframework.data.couchbase.repository 只有CouchbaseRepository,它正在扩展CrudRepository<T,ID>

没有从PagingAndSortingRepository<T,ID>扩展的接口。

http://docs.spring.io/spring-data/couchbase/docs/1.0.x/api/org/springframework/data/couchbase/repository/CouchbaseRepository.html

我确实对问题设置跳过和限制实施了一个非常规的解决方案。但是想要获得总页数,这是此解决方案不可用的。 看来Page<T> 确实有.getTotalPages()

【问题讨论】:

  • 请分享您的代码

标签: java sorting spring-data paging couchbase


【解决方案1】:

以下是使分页工作的步骤。

  1. 将 spring-data-couchbase 版本更新为4.3.1 或更高版本
  2. 使用extends CouchbaseRepository<Object, Object>
  3. 构建Pagebale 对象。
  4. Page<Object> findAllById(String id, Pageable page)

就是这样

【讨论】:

    【解决方案2】:

    目前Spring Data Couchbase不支持分页,需要使用底层API,基本上是Couchbase的Java SDK的第一个版本。

    【讨论】:

      【解决方案3】:

      由于 Spring-Data-Couchbase 2.0.x 支持PagingAndSortingRepository 对数据进行分页和排序。

      接口定义:

      public interface PagingAndSortingRepository<T, ID extends Serializable>
        extends CrudRepository<T, ID> {
      
        Iterable<T> findAll(Sort sort);
      
        Page<T> findAll(Pageable pageable);
      }
      

      分页项目示例:

      public interface ProjectRepository extends PagingAndSortingRepository<Project, String> {
      
          Page<Project> findByName(String name, Pageable pageable);
      }
      

      【讨论】:

        【解决方案4】:

        4 年过去了,遗憾的是仍然没有 ReactiveCouchbasePagingAndSortingRepository。 相反,我将使用索引,以及偏移参数和计数选择的组合来解决这个问题。 https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/aggregatefun.html#countn https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/offset.html

        @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} ORDER BY name LIMIT $1 OFFSET $2;")
        Flux<Location> findPage(int limit, int offset);
        

        如果他们确实添加了它,它应该出现在此处(或在更高版本中)

        https://docs.spring.io/spring-data/couchbase/docs/4.0.x/api/index.html?org/springframework/data/couchbase/repository/package-tree.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-03
          • 2011-04-25
          • 2018-11-15
          • 2015-10-29
          • 1970-01-01
          • 2020-03-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多