首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SeedStack存储库中的分页

SeedStack存储库中的分页
EN

Stack Overflow用户
提问于 2019-09-18 12:59:31
回答 1查看 221关注 0票数 0

在我的SeedStack项目中,我使用以下规范查询从聚合中检索数据:

更新:修复代码示例,以显示正确的返回类型方法

代码语言:javascript
复制
public interface ProductRepository extends Repository<Product, ProductId> {
    default Stream<Product> discontinuedProducts() {
        return get(getSpecificationBuilder().of(Product.class)
                .property("discontinued").equalTo(true)
                .build()
        );
    }
}

为了避免潜在的内存不足错误,我想使用分页来拆分在某些存储库查询中检索到的数据。

此外,我想使用与字符串数据存储库中存在的分页特性非常相似的特性。我希望保留结果的某些元素(而不是所有元素),并将它们按“页面”处理,而不将所有数据结果保存在Collection中。

春季寻呼与排序: https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html

但是,我阅读了SeedStack文档,没有找到这个特性。

存储库( SeedStack: http://seedstack.org/docs/business/repositories/ )

因此,我想知道使用SeedStack对查询结果进行分页的最佳方法是什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 17:22:11

SeedStack以几种不同的方式提供了分页的帮助,但遗憾的是,这方面缺乏文档。

在我们详细讨论之前,请注意,您的discontinuedProducts()方法应该返回一个Stream<Product>而不是一个List<Product>,因为存储库的get方法返回一个流。

使用SeedStack进行分页的主要方法是使用插入存储库之上的分页器DSL来提供分页。

示例1(直接对流进行分页):

代码语言:javascript
复制
public class SomeResource {
    @Inject
    private Paginator paginator;
    @Inject
    private ProductRepository productRepository;
    @Inject
    private FluentAssembler fluentAssembler;

    public void someMethod() {
        Page<Product> page = paginator.paginate(productRepository.discontinuedProducts())
                .byPage(1)
                .ofSize(10)
                .all();
    }
}

示例2(用规范对存储库进行分页):

代码语言:javascript
复制
public class SomeResource {
    @Inject
    private Paginator paginator;
    @Inject
    private ProductRepository productRepository;
    @Inject
    private SpecificationBuilder specificationBuilder;

    public void someMethod() {
        Page<Product> page = paginator.paginate(productRepository)
                .byPage(1)
                .ofSize(10)
                .matching(specificationBuilder.of(Product.class)
                    .property("discontinued").equalTo(true)
                    .build());
    }
}

示例3(在混合文件中添加DTO映射):

代码语言:javascript
复制
public class SomeResource {
    @Inject
    private Paginator paginator;
    @Inject
    private ProductRepository productRepository;
    @Inject
    private SpecificationBuilder specificationBuilder;
    @Inject
    private FluentAssembler fluentAssembler;

    public void someMethod() {
        Page<ProductDto> page = fluentAssembler.assemble(
                paginator.paginate(productRepository)
                    .byPage(1)
                    .ofSize(10)
                    .matching(specificationBuilder.of(Product.class)
                        .property("discontinued").equalTo(true)
                        .build()))
                .toPageOf(ProductDto.class)
    }
}

在最后一个示例中,SeedStack将:

  • 将分页谓词添加到自定义规范中,
  • 将结果规范转换为正确的持久性查询,
  • 把数据拿出来放到页面里,
  • 使用正确的汇编程序将域聚合的页转换为DTO的页。

请注意,如果要一次又一次地重用相同的规范,那么将其转换为类(如DiscontinuedProductSpec)或在存储库中创建方法来构建它(如buildDiscontinuedProductSpec())可能是有用的。

还有其他几种组合这些工具的方法,请检查 分页器 FluentAssembler 存储库E 131的Javadoc。您还可以查看 分页集成测试 以获得更多的示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57993405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档