带有示例,规范和Pageable的Spring Boot存储库

davidddp:

我需要在存储库中创建一个复杂的可分页过滤器。我已经成功地使用Example和Pageable扩展自:

extends JpaRepository <Card, Long>

继而从:

extends PagingAndSortingRepository <T, ID>, QueryByExampleExecutor <T>

现在的呼叫是这样的:

Page<Card> findByCreatedDateBetween (Example<Card> exampleCard, Pageable pageable);

问题:我需要按日期搜索,该日期必须在两个给定的日期之间。Example<Card>这是不可能指示范围。

我已经考虑过使用Specification来做到这一点,但是我不知道如何使用3个参数进行调用:

Page<Card> findByCreatedDateBetween (Specification<Card> cardSpecification, Example<Card> exampleOffer, Pageable pageable);

同样还有其他解决方案吗?

达里奥·塞德尔(Dario Seidl):

您不能在一个查询中同时使用an Example和a Specification,因为QueryByExampleExecutor它不提供这种方法。

您还可以查看SimpleJpaRepository是否对的实现感兴趣QueryByExampleExecutor在内部,实现创建一个SpecificationExample使用QueryByExamplePredicateBuilder您可能可以使用该功能Specification使用该功能构建自己Example功能,然后将其Specification与另一个功能结合使用。

ExampleSpecificationSimpleJpaRepository是私有的,所以你不能使用,但你可以复制它,然后像做

findAll(ExampleSpecification(yourExample, escapeCharacter).and(yourDateRangeSpecification), pageable);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章