Skip to content

利用JPAL返回Dto的返回结果 #57

@zhangzhenhuajack

Description

@zhangzhenhuajack

参考:https://vladmihalcea.com/spring-jpa-dto-projection/
实际案例如下:

  1. 新建WorkflowThroughputPhaseCountDto 内容如下
@Data
public class WorkflowThroughputPhaseCountDto {
   // 注意构造方法会被JPQL使用
    public WorkflowThroughputPhaseCountDto(Long WorkflowThroughputPhaseId, ActionStatus actionStatus, Long workflowThroughputPhaseCount) {
        this.workflowTemplatePhaseId = WorkflowThroughputPhaseId;
        this.workflowThroughputPhaseCount = workflowThroughputPhaseCount;
        this.actionStatus = actionStatus;
    }

    private Long workflowTemplatePhaseId;
    private Long workflowThroughputPhaseCount;
    private ActionStatus actionStatus;
}
  1. Repository里面的写法如下,注意要用到 new Dto全路径类前面的构造方法
@Repository
public interface WorkflowThroughputPhaseRepository extends JpaRepository<WorkflowThroughputPhase, Long> {
    @Query(value = "select new com.mega.workflow.suite.service.dto.WorkflowThroughputPhaseCountDto(wt.workflowTemplatePhaseId,wt.status,count(wt.id)) " +
            "from WorkflowThroughputPhase wt where wt.workflowId = :workflowId group by wt.status,wt.workflowTemplatePhaseId")
    List<WorkflowThroughputPhaseCountDto> findCountByWorkflowId(@Param("workflowId") Long workflowId);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions