【问题标题】:Group by clause with QuerydslPredicateExecutor使用 QuerydslPredicateExecutor 进行分组
【发布时间】:2019-12-30 15:45:13
【问题描述】:

我有一个实体如下:

@Entity
public class Enterprise{
  private String sid = UUID.randomUUID().toString();
  private String name;
  private String organisationSid;
  private EnterpriseStatus status;
  @CreatedDate
  private Date dateCreated;
}

存储库:

public interface EnterpriseRepository extends 
            PagingAndSortingRepository<Enterprise, String>, 
            QuerydslPredicateExecutor<Enterprise>{}

问题: 我想根据 dateCreated 获取每日/每月/每年的记录计数。我可以在这里为组使用 QueryDSL 吗? 它让我可以创建如下的 groupBy 子句:

GroupBy.groupBy(QEnterprise.enterprise.dateCreated.month());

我现在不知道如何使用这个GroupByBuilder 来查询存储库。存储库有一个 findAll 方法,它采用 com.querydsl.core.types.Predicates,但没有采用 com.querydsl.core.group.GroupByBuilder

【问题讨论】:

    标签: java spring-data-jpa querydsl


    【解决方案1】:

    您无法在默认实现中传递 GroupBy 子句。要得到你想要的输出,首先你必须实现自定义存储库,其次你必须编写自己的方法和逻辑。

    第一步:


    写一个新的仓库接口:

    import org.springframework.data.repository.NoRepositoryBean;
    import com.querydsl.core.types.Predicate;
    import java.util.Map;
    
    @NoRepositoryBean
    public interface EnterpriseRepositoryCustom {
        Map<Integer, Long> countByMonth(Predicate predicate);
    }
    

    现在将此接口继承到您现有的EnterpriseRepository

    public interface EnterpriseRepository extends 
                PagingAndSortingRepository<Enterprise, String>, 
                QuerydslPredicateExecutor<Enterprise>,
                EnterpriseRepositoryCustom{}
    

    然后新建自定义仓库的实现类:

    import com.querydsl.core.group.GroupBy;
    import com.querydsl.core.types.Predicate;
    import org.springframework.data.jpa.repository.support.QueryDslRepositorySupport;
    import org.springframework.stereotype.Repository;
    import java.util.Map;
    
    @Repository
    public class EnterpriseRepositoryImpl extends QueryDslRepositorySupport 
                 implements EnterpriseRepositoryCustom {
        public EnterpriseRepositoryImpl() {
            super(Enterprise.class);
        }
    
        @Override
        public Map<Integer, Long> countByMonth(Predicate predicate) {
            //Have to write logic here....
        }
    }
    

    第二步:


    countByMonth方法中写下你的逻辑如下:

    public Map<Integer, Long> countByMonth(Predicate predicate) {
            Map<Integer, Long> countByMonth = getQuerydsl()
                    .createQuery()
                    .from(QEnterprise.enterprise)
                    .where(predicate)
                    .transform(GroupBy
                          .groupBy(QEnterprise.enterprise.dateCreated.month())
                          .as(QEnterprise.enterprise.count()));
           return countByMonth;
        }
    

    可选: 如果您想按月获取记录列表,则只需修改 count as -

            Map<Integer, List<Enterprise>> recordByMonth = getQuerydsl()
                    .createQuery()
                    .from(QEnterprise.enterprise)
                    .where(predicate)
                    .transform(GroupBy
                          .groupBy(QEnterprise.enterprise.dateCreated.month())
                          .as(GroupBy.list(QEnterprise.enterprise)));
    

    希望你找到答案了!!

    示例github project

    【讨论】:

      【解决方案2】:

      大家可以试试这个方法

      return new JPAQuery<>(entityManager).where(predicate).groupBy(QWarehouseItemHistory.warehouseItemHistory.warehouse).fetchCount();
      

      【讨论】:

        猜你喜欢
        • 2011-01-13
        • 1970-01-01
        • 2021-12-21
        • 2011-09-14
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多