【问题标题】:Spring data r2dbc and paginationSpring数据r2dbc和分页
【发布时间】:2020-03-11 11:27:06
【问题描述】:

我正在使用新的 spring data r2dbc 模块,并且能够使用 ReactiveCrudRepository 提取数据。 现在我需要引入分页,但我无法做到。 我试过这个

public interface TestRepository extends ReactiveCrudRepository<MyEntity, Long> {
    Flux<MyEntity> findByEntityId(Long entityId, Pageable page);
}

但是当我尝试执行这个时,我得到了这个错误

org.springframework.data.repository.query.ParameterOutOfBoundsException: Invalid parameter index! You seem to have declared too little query method parameters!
    at org.springframework.data.repository.query.Parameters.getParameter(Parameters.java:237)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 

有没有办法在这个模块中使用分页?

【问题讨论】:

    标签: spring-data-r2dbc


    【解决方案1】:

    新的R2dbcEntityTemplate Spring Data R2dbc 1.2 包含这样的分页操作。

     private final R2dbcEntityTemplate template;
    
        public Flux<Post> findByTitleContains(String name) {
            return this.template.select(Post.class)
                    .matching(Query.query(where("title").like("%" + name + "%")).limit(10).offset(0))
                    .all();
        }
    
    

    Spring Data R2dbc 1.2(尚未发布)将接受 Pageable 参数,如 Repository

     public Flux<PostSummary> findByTitleLike(String title, Pageable pageable);
    

    完整的代码示例,请查看heretest codes

    【讨论】:

    • 在 ReactiveCrudRepository 中使用 Pageable 也应该适用于 1.1.4。
    【解决方案2】:

    不,目前没有办法使用隐式分页。您应该指定整个查询以使用它。

    这是一个例子:

    @Query("SELECT * FROM my_entity WHERE entity_id = :entityId OFFSET :offset LIMIT :limit")
    Flux<MyEntity> findByEntityId(Long entityId, int offset, int limit);
    

    【讨论】:

      【解决方案3】:

      我能够使用 spring-boot-starter-data-r2dbc.2.4.3 实现这一目标

      正如@Hantsy 所说,ReactiveCrudRepository 将接受 Pageable 作为查询内的参数,但这不会解决分页问题。在休眠中,您希望返回一个对象的页面,但对于 Reactive,它将是一个 Flux。

      然而,我可以通过使用 PageImpl 类和 ReactiveCrudRepository 接口中的 count 方法来实现这一点。

      例如

      public interface TestRepository extends ReactiveCrudRepository<MyEntity, Long> {
          Flux<MyEntity> findByEntityId(Long entityId, Pageable page);
      }
      
      public Mono<<Page<MyEntity>> getMyEntities(Long entityId, PageRequest request) {
          return testRepository.findByEntityId(entityId, request)
                  .collectList()
                  .zipWith(testRepository.count())
                  .flatMap(entityTuples -> 
                      new PageImpl<>(entityTuples.getT1(), request, entityTuples.getT2()));
      }
      

      【讨论】:

        【解决方案4】:

        Spring Data R2dbc 的较新版本接受 @Hantsy 提到的 Pageable,但有一个问题。

        如果您在没有任何 WHERE 子句的情况下获取所有记录,则以下操作无效:

        public interface MyEntityRepository extends ReactiveCrudRepository<MyEntity, Long> {
            Flux<MyEntity> findAll(Pageable pageable);
        }
        

        findAll() 更改为findBy() 工作正常。

        public interface MyEntityRepository extends ReactiveCrudRepository<MyEntity, Long> {
            Flux<MyEntity> findBy(Pageable pageable);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-01-23
          • 2020-11-29
          • 2023-03-15
          • 1970-01-01
          • 2019-08-31
          • 2021-02-15
          • 1970-01-01
          • 2020-03-04
          • 2015-07-24
          相关资源
          最近更新 更多