QueryDSL自定义实体映射,Predicate多条件查询

 @Override
    public List getProjectStatus(Long departId) {
        QProject qProject = QProject.project;
        Predicate predicate = qProject.isNotNull().or(qProject.isNull());
        predicate = StringUtils.isEmpty(departId) ? predicate : ExpressionUtils.and(predicate, qProject.departId.eq(departId));

        List projectStatusDTOList = jpaQueryFactory.select(
                Projections.bean(
                        ProjectStatusDTO.class,
                        qProject.status,
                        qProject.id.count().as("count")
                )
        )
                .from(qProject)
                .where(predicate)
                .groupBy(qProject.status)
                .fetch();
        return projectStatusDTOList;
    }

核心代码:

 Projections.bean(
                        ProjectStatusDTO.class,
                        qProject.status,
                        qProject.id.count().as("count")
                )

注意点:

如果实体中的字段属性和自定义的对象的属性不一样,请使用   .as("自定义对象中的属性名称") 

你可能感兴趣的:(QueryDsl,SpringDataJPA)