Spring Data JPA使用QueryDsl自定义返回对象(配合小辣椒使用)

 /**
     * 查询项点的违占比
     *
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public BaseRecordDTO findRiskWeiZhanBi(SearchParam param) throws Exception {
        Map<String, Object> map = getAllMonth(param);
        Date from = DateUtils.str2day(map.get("min").toString());
        Date to = DateUtils.str2day(map.get("max").toString());

        QBaseRecord baseRecord = QBaseRecord.baseRecord;
        List<Long> list = queryDifferRiskListPage(baseRecord, from, to, param.getStart(), param.getLength());
        BaseRecordDTO baseRecordDTO = new BaseRecordDTO();

        List<String> names = new ArrayList<>();//项点名称
        List<Long> errcounts = new ArrayList<>();//错误数量
        List<Long> totalCounts = new ArrayList<>();//总数量
        List<Double> baiFens = new ArrayList<Double>();//百分比

        QBaseRecord qBaseRecord = QBaseRecord.baseRecord;
        //根据项点id进行查询
      List<RiskTuBiaoFenXiDTO> list1 = jpaQueryFactory.select(
                qBaseRecord.id.count().as("levelCount"),//.as("levelCount")
                qBaseRecord.level,
                qBaseRecord.riskId
        )
                .from(qBaseRecord)
                .where(
                        qBaseRecord.riskId.in(list),
                        qBaseRecord.checkTime.between(from, to)
                )
                .groupBy(
                        qBaseRecord.level,
                        qBaseRecord.riskId
                )
                .fetch()
                .stream()
                .map(tuple -> RiskTuBiaoFenXiDTO.builder()
                        .levelCode(tuple.get(qBaseRecord.level))
                        .levelCount(tuple.get(qBaseRecord.id.count().as("levelCount")))
                        .riskId(tuple.get(qBaseRecord.riskId))
                        .build()
                )
                .collect(Collectors.toList());

 下面是实体

package com.neld.tmss.domain.dto.coverreport;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class RiskTuBiaoFenXiDTO {

    private Long levelCount;

    private Integer levelCode;

    private Long riskId;
}

 

上一篇:spring data jpa 动态查询


下一篇:JPA入门总结