目前在spring data jpa查询还没有支持聚合查询,工作中需要得使用原生查询。
@Repository 接口查询
@Query(value = “select send_to_test_by as developerId,type,sum(spend_time_with_plan) as plantime,
sum(spend_time_with_develop) as developtime,sum(bugqty) as bugqty from b_task where release_id = ?1
group by type,send_to_test_by”, nativeQuery = true)
List<Object[]> getReleaseDeveloerReport(Long release_id);
查询结果不能是其它对象,只能是List<Object[]>
调用
@Override
public List getDeveloperTimeByRelease(Release release) {
List result = new ArrayList<>();
List<Object[]> list = taskRepository.getReleaseDeveloerReport(release.getId());
for (Object[] obj : list) {
ReleaseDeveloerReport rdr = new ReleaseDeveloerReport();
if (!ObjectUtils.isEmpty(obj[0])) {
rdr.setDeveloperId(NumberUtils.toLong(obj[0].toString()));
rdr.setDeveloper(userRepository.findOne(rdr.getDeveloperId()).getUsername());
rdr.setProgress(getTaskProgress(release, rdr.getDeveloperId(), rdr.getType()));
}
rdr.setType(obj[1].toString());
rdr.setPlantime(obj[2].toString());
rdr.setDeveloptime(obj[3].toString());
if (!ObjectUtils.isEmpty(obj[4])) {
rdr.setBugqty(NumberUtils.toInt(obj[4].toString()));
}
result.add(rdr);
}
return result;
}
调用这块其实也不复杂,关键是要处理好数组。