按示例查询 Spring 数据 - 在条款中?

Sac*_*rma 5 java spring query-by-example spring-data spring-data-jpa

根据 Spring Docs,我只能为 QBE 编写精确匹配。我只需要精确匹配,但需要一组值(查询的 IN 子句)。

例如

Person p = new Person();
p.setId(); // need to match among set of ids.
Example.of(p);
Run Code Online (Sandbox Code Playgroud)

这是否可以通过 QBE 实现,还是我完全走错了路?

就像是 :

Page<S> findByIdIn(List<Integer> ids, Example<S> e, Pageable p)
Run Code Online (Sandbox Code Playgroud)

两全其美?

我真正需要的是,基于多个字段的动态查询(在可能的组合中,比如 id in (1,2,4)、status=open、appointmentDate <今天)以及分页和排序。规范是脱离本机查询的唯一方法吗?

JB *_*zet 7

我只需要精确匹配,但需要一组值(查询的 IN 子句)。

所以你需要精确匹配以外的东西。您不可能在 Person 的 ID 属性中存储一组 ID。QBE 显然不是这项工作的正确工具。

您可以直接使用规范、标准 API、QueryDSL、动态组合的 JPQL 查询或任何其他解决方案,但不能使用 QBE。

  • 您可以将示例转换为谓词,然后您可以使用 https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder 与其他谓词结合使用.html (2认同)