1,dao:
List <HashMap <String, String>> selectSubjectNum (@Param ("startTime") String beginTime , @Param ("endTime") String endTime);
2,mapper:
<select id = "selectSubjectNum" resultType = "java.util.HashMap">SELECT `subject` AS 'key', count(subject) AS 'value'FROM tb_contractwhere DATE_FORMAT(`create_time`, '%Y-%m-%d') BETWEEN '2022-04-19' AND '2022-04-20'group by `subject`</select>
3,* 实体类:
注意:多个map时,必须将map封装到list中,一个map对应数据库查询出来的一行; 因为从数据库出来的结果是这样的: 【(”key”,”zhangsan”),(“value”,”20”)】 【 (“key”,”0”),(“value”,”4”) 】 必须要将键和值分开;
/*** 学科统计** @return*/@Overridepublic List <SubjectInfoVo> subjectStatistics (String beginTime , String endTime) {//查询有几个学科的合同//new mapList <SubjectInfoVo> list = new ArrayList();Map <String, String> map = new HashMap();List <HashMap <String, String>> mapList = reportMpper.selectSubjectNum(beginTime , endTime);if (mapList.size() != 0) {//遍历装map的listfor (HashMap <String, String> hashMap : mapList) {String key = null;String value = null;//遍历单个map,取出键和值Set <Map.Entry <String, String>> entries = hashMap.entrySet();for (Map.Entry <String, String> entry : entries) {if ("key".equals(entry.getKey())) {key = String.valueOf(entry.getValue());} else if ("value".equals(entry.getKey())) {value = String.valueOf(entry.getValue());}//再封装会map中map.put(key , value);}}}System.out.println("map = " + map);//再次遍历mapSet <Map.Entry <String, String>> entries = map.entrySet();for (Map.Entry <String, String> entry : entries) {SubjectInfoVo subjectInfoVo = new SubjectInfoVo();String key = entry.getKey();if (key != null){String subjectName = reportMpper.selectSubjectName(key);subjectInfoVo.setSubject(subjectName);subjectInfoVo.setNum(Integer.parseInt(entry.getValue()));//将结果封装回listlist.add(subjectInfoVo);}}return list;}
