如何在一个实体类中放入从多个sql语句中查询的数据
Posted 幻龙九头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在一个实体类中放入从多个sql语句中查询的数据相关的知识,希望对你有一定的参考价值。
查询1
public List sqlForDiagnosis(Date startDate, Date endDate, String cataloger) {
String sqlForDiagnosis = "select i.cataloger name" +
", b.STAFF_NAME staff_name"+
", count(distinct i.visit_no) mr_number" +
", count(d.diag_code) diagnosis_number" +
", count(distinct d.diag_code) diagnosis_type_number" +
" from inp_visit i" +
" left join diagnostic_category d on i.patient_id=d.patient_id and i.visit_no=d.visit_no" +
" left join staff_dict b on i.CATALOGER=b.emp_id"+
" where i.catalog_date>=to_date(\'" + DateTimeParameter.format(startDate) + "\', \'yyyy-mm-dd hh24:mi:ss\')" +
" and i.catalog_date<=to_date(\'" + DateTimeParameter.format(endDate) + "\', \'yyyy-mm-dd hh24:mi:ss\')";
if(!StringUtils.isEmpty(cataloger)){
sqlForDiagnosis += "and i.cataloger="+ cataloger;
}
sqlForDiagnosis += " group by i.cataloger,b.STAFF_NAME" +
" order by i.cataloger";
return createNativeQueryToMap(sqlForDiagnosis, new ArrayList<>());
}
查询2
public List sqlForOperation(Date startDate, Date endDate,String cataloger) {
String sqlForOperation = "select i.cataloger name" +
", b.STAFF_NAME staff_name"+
", count(distinct i.visit_no) mr_number" +
", count(o.operation_code) operation_number" +
", count(distinct o.operation_code) operation_type_number" +
" from inp_visit i" +
" left join operation o on i.patient_id=o.patient_id and i.visit_no=o.visit_no" +
" left join staff_dict b on i.CATALOGER=b.emp_id"+
" where i.catalog_date>=to_date(\'" + DateTimeParameter.format(startDate) + "\', \'yyyy-mm-dd hh24:mi:ss\')" +
" and i.catalog_date<=to_date(\'" + DateTimeParameter.format(endDate) + "\', \'yyyy-mm-dd hh24:mi:ss\')" ;
if(!StringUtils.isEmpty(cataloger)){
sqlForOperation += "and i.cataloger="+ cataloger;
}
sqlForOperation += " group by i.cataloger,b.STAFF_NAME" +
" order by i.cataloger";
return createNativeQueryToMap(sqlForOperation, new ArrayList<>());
}
插入数据
public List statisticsWorkloadForStaff(Date startDate, Date endDate,String cataloger) {
List<Map> diagnosisStatistic = inpMedicalRecordDao.sqlForDiagnosis(startDate, endDate,cataloger);
List<Map> operationStatistic = inpMedicalRecordDao.sqlForOperation(startDate, endDate,cataloger);
Map<String, StaffWorkloadStatisticVO> temp = new HashMap<>();
for (Map diagnosis : diagnosisStatistic) {
temp.put((String) diagnosis.get("name"),
new StaffWorkloadStatisticVO((String) diagnosis.get("staffName"), (BigDecimal) diagnosis.get("mrNumber")
, (BigDecimal) diagnosis.get("diagnosisNumber"), (BigDecimal) diagnosis.get("diagnosisTypeNumber")
, null, null));
}
for (Map operation : operationStatistic) {
String name = (String) operation.get("name");
StaffWorkloadStatisticVO vo = temp.get(name);
vo.setOperationNumber((BigDecimal) operation.get("operationNumber"));
vo.setOperationTypeNumber((BigDecimal) operation.get("operationTypeNumber"));
}
return new ArrayList(temp.values());
}
使用数据库别名对应参数 建立一个List<Map> 用查询数据的唯一值列为key 分多次插入对应数据 最后返回拼装好的完整数据 需要多个数据表中有相同的唯一值列以做对照
以上是关于如何在一个实体类中放入从多个sql语句中查询的数据的主要内容,如果未能解决你的问题,请参考以下文章
我可以在不创建实体类的情况下对大型 sql 使用休眠命名查询吗?
7表的列名与实体类列名不对应,如何将查询结果封装到实体类对象中