首页 > 解决方案 > Spring Data JPA Query - 使用 in 子句计数

问题描述

我正在尝试使用带有方法名称解析的 spring data JPA 来实现以下查询。

select count(*) from example ex where ex.id = id and ex.status in (1, 2);

我知道 crud repo 中有一种方法用于计数,countByIdAndStatus(params)但我不确定如何将“in”子句与该方法结合起来。

谢谢!

标签: springspring-data-jpa

解决方案


您可以这样做:

long countByIdAndStatusIn(Integer id, List<Type> params);

@Query

@Query("select count(*) from Example ex where ex.id = ?1 and ex.status in (?2)")
long countByIdAndStatus(Integer id, List<Type> params);

推荐阅读