其他分享
首页 > 其他分享> > Spring Data JPA使用QueryDsl自定义返回对象(配合小辣椒使用)

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;
}

 

标签:自定义,get,JPA,List,param,qBaseRecord,new,小辣椒,riskId
来源: https://blog.51cto.com/u_13993537/2990010