簡體   English   中英

SeedStack存儲庫中的分頁

[英]Pagination in SeedStack Repositories

在我的Java項目中,使用SeedStack,我有一些查詢來使用類似的規范從聚集中檢索數據:

更新:已修復代碼示例,以顯示正確的返回類型方法

public interface ProductRepository extends Repository<Product, ProductId> {
    default Stream<Product> discontinuedProducts() {
        return get(getSpecificationBuilder().of(Product.class)
                .property("discontinued").equalTo(true)
                .build()
        );
    }
}

為了避免潛在的內存不足錯誤,我想使用分頁來拆分某些存儲庫查詢中檢索到的數據。

此外,我想使用與String Data Repositories中存在的分頁功能非常相似的功能。 我想保留結果的某些元素(而不是全部),並通過“頁面”對其進行處理,而不必將所有數據結果都保留在Collection中。

在Spring中進行分頁和排序: 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分頁查詢結果的最佳方法是什么。

SeedStack為助手提供了幾種不同的分頁方法,但是遺憾的是在這方面缺少文檔。

在進行詳細介紹之前,請注意,您的discontinuedProducts()方法應該返回Stream<Product>而不是List<Product>因為存儲庫的get方法將返回流。

使用SeedStack進行分頁的主要方法是使用Paginator DSL ,該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將:

  • 在您的自定義規范中添加分頁謂詞,
  • 將結果規范轉換為適當的持久性查詢,
  • 獲取數據並將其放在頁面中,
  • 使用正確的匯編程序將域聚合頁面轉換為DTO頁面。

請注意,如果您要一遍又一遍地重用同一規范,將其轉換為類(如DiscontinuedProductSpec )或在存儲庫中創建一個方法來構建它(如buildDiscontinuedProductSpec() )可能會很有用。

還有其他幾種組合這些工具的方法,請檢查PaginatorFluentAssemblerRepository的Javadoc。 您還可以查看分頁集成測試,以獲取更多示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM