【问题标题】:How to COUNT GROUP BY using JPARepository JPASpecificationExecutor with CustomSpecification如何使用 JPARepository JPASpecificationExecutor 和 CustomSpecification 来计数组
【发布时间】:2022-06-15 23:19:40
【问题描述】:

我有 jpa 存储库:

public interface MyRepository extends JPARepository<MyObject>, JPASpecificationExecutor<MyObject>{
}

和自定义规范

CustomComplexSpecification implements Specification<MyObject>{
... complex logic for object depending on input 
}

通常我会这样获取记录

myRepository.findAll(new CustomComplexSpecification(..input fields);

现在我想通过 group by 子句获取计数

SELECT field1, field2, field3, count(*) from MYTABLE

where(来自复杂规范的标准)按 field1、field2、field3 分组;

如何使用上面的 jpaRepository 和 CustomSpecification 进行 GROUP BY 和 COUNT

注意:我可以像这样完成计数

myRepository.count(new CustomComplexSpecification(..input fields);

帮我分组和计数。提前致谢。

【问题讨论】:

    标签: java sql jpa spring-data-jpa


    【解决方案1】:

    Spring Data JPA 支持重写查询 sql,如:

    public interface ResumeDao extends JpaRepository<Resume, Long>, 
        JpaSpecificationExecutor<Resume> {
    
      @Query("from Resume where id = ?1")
      public List<Resume> findByJpql(Long id);
    
      @Query(value = "select * from tb_resume where id = ?1", nativeQuery = true)
      public Resume findBySql(Long id);
    }
    

    那么你可以尝试重写一个SQL和对应的方法吗?

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 1970-01-01
      • 2016-05-14
      • 2018-07-09
      • 2014-06-01
      • 2019-08-31
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多