【问题标题】:Spring JPA count where group bySpring JPA count where group by
【发布时间】:2022-01-06 10:40:41
【问题描述】:

我想在 JPA 中转换一个 sql 查询。

SELECT status, count(*)
FROM abc
WHERE type='XXX'
GROUP BY status

我需要一个带有 sql 的 JPARepository 中的东西。

@Repository
public interface ABCRepository extends JpaRepository<abc, Long> {

   long countByStatusAndType(final A type, final B status);
}

有可能吗?

【问题讨论】:

    标签: java spring-boot jpa spring-data-jpa spring-data


    【解决方案1】:

    首先,创建包含参数statuscount的类用于处理结果,然后在存储库中创建带有查询的方法

    @Query("SELECT status, count(*) as count FROM abc WHERE type=:type GROUP BY status")
    List<CustomResultClass> countByStatus(String type);
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多