bog*_*usu 8 spring spring-data-jpa spring-repositories
我在 Spring Repo 中有这样的东西:
findTop10ItemsByCategIdInOrderByInsertDateDesc(List ids)
Run Code Online (Sandbox Code Playgroud)
我想要前 10 个项目,其中类别 id 在按插入日期排序的 id 列表中。
另一个类似的查询:
findTop10ItemsByDomainIdAndCategIdInOrderByInsertDateDesc(List ids, @Param Integer domainId)
Run Code Online (Sandbox Code Playgroud)
在这里,我希望域 id 等于给定的参数,并且 categId 位于给定的列表中。
我设法使用 @Query 解决了它,但我想知道上述查询是否有一个单行。
谢谢
编辑
顶部工作正常。最初我有findTop10ItemsByDomainIdAndCategIdOrderByInsertDateDesc. 现在我想要来自类别 ID 列表的结果。这就是新的要求。
第二次编辑 我的查询用于查找域 id 等于给定参数且类别 id 包含在给定列表中的集合 o 结果。但我发现 HQL 不支持 setMaxResult 类型的东西作为顶部或限制。
@Query("select i from Items i where i.domainId = :domainId and i.categId in :categoryIds order by i.insertDate desc")
Run Code Online (Sandbox Code Playgroud)
这个方法的参数是(@Param("domainid") Integer domainid,List<Integer> categoryIds)但它接缝我被允许对每个参数使用 @Param 注释或根本不使用 @Param (除了 Pageable 返回;不是我的情况)
我仍然不知道如何实现这个想法:提取前 n 个元素,其中字段 a eq 到 param,字段 b 在参数集中,按另一个字段排序。
ps:抱歉标签,但没有 spring-crudrepository :)
解决您的问题的方法是:
List<MyClass> findTop10ByDomainIdAndCategIdInOrderByInsertDateDesc(Long domainId, List<Long> ids);
Run Code Online (Sandbox Code Playgroud)
Top10 将结果限制为前 10 个。
ByDomainId将结果限制为已通过的结果domainId。
And 添加另一个约束。
CategIdIn将结果限制categId为通过List.
OrderByInsertDateDesc在限制为 之前按插入日期对结果进行降序排序TOP10。我已在以下示例中测试了此查询:
List<User> findTop10ByEmailAndPropInOrderByIdDesc(String email, List<Long> props);
Run Code Online (Sandbox Code Playgroud)
在哪里User:
private Long id;
private String username;
private String password;
private String email;
private Long prop;
Run Code Online (Sandbox Code Playgroud)
目前,我建议使用 Spring Data JPA使用LocalDate或LocalDateTime存储日期。
| 归档时间: |
|
| 查看次数: |
20911 次 |
| 最近记录: |