在我的SeedStack项目中,我使用以下规范查询从聚合中检索数据:
更新:修复代码示例,以显示正确的返回类型方法
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对查询结果进行分页的最佳方法是什么。
发布于 2019-09-19 17:22:11
SeedStack以几种不同的方式提供了分页的帮助,但遗憾的是,这方面缺乏文档。
在我们详细讨论之前,请注意,您的discontinuedProducts()方法应该返回一个Stream<Product>而不是一个List<Product>,因为存储库的get方法返回一个流。
使用SeedStack进行分页的主要方法是使用插入存储库之上的分页器DSL来提供分页。
示例1(直接对流进行分页):
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(用规范对存储库进行分页):
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映射):
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将:
请注意,如果要一次又一次地重用相同的规范,那么将其转换为类(如
DiscontinuedProductSpec)或在存储库中创建方法来构建它(如buildDiscontinuedProductSpec())可能是有用的。
还有其他几种组合这些工具的方法,请检查 分页器、 FluentAssembler 和存储库E 131的Javadoc。您还可以查看 分页集成测试 以获得更多的示例。
https://stackoverflow.com/questions/57993405
复制相似问题