1,dao:

  1. List <HashMap <String, String>> selectSubjectNum (@Param ("startTime") String beginTime , @Param ("endTime") String endTime);

2,mapper:

  1. <select id = "selectSubjectNum" resultType = "java.util.HashMap">
  2. SELECT `subject` AS 'key', count(subject) AS 'value'
  3. FROM tb_contract
  4. where DATE_FORMAT(`create_time`, '%Y-%m-%d') BETWEEN '2022-04-19' AND '2022-04-20'
  5. group by `subject`
  6. </select>

3,* 实体类:

注意:多个map时,必须将map封装到list中,一个map对应数据库查询出来的一行; 因为从数据库出来的结果是这样的: 【(”key”,”zhangsan”),(“value”,”20”)】 【 (“key”,”0”),(“value”,”4”) 】 必须要将键和值分开;

  1. /**
  2. * 学科统计
  3. *
  4. * @return
  5. */
  6. @Override
  7. public List <SubjectInfoVo> subjectStatistics (String beginTime , String endTime) {
  8. //查询有几个学科的合同
  9. //new map
  10. List <SubjectInfoVo> list = new ArrayList();
  11. Map <String, String> map = new HashMap();
  12. List <HashMap <String, String>> mapList = reportMpper.selectSubjectNum(beginTime , endTime);
  13. if (mapList.size() != 0) {
  14. //遍历装map的list
  15. for (HashMap <String, String> hashMap : mapList) {
  16. String key = null;
  17. String value = null;
  18. //遍历单个map,取出键和值
  19. Set <Map.Entry <String, String>> entries = hashMap.entrySet();
  20. for (Map.Entry <String, String> entry : entries) {
  21. if ("key".equals(entry.getKey())) {
  22. key = String.valueOf(entry.getValue());
  23. } else if ("value".equals(entry.getKey())) {
  24. value = String.valueOf(entry.getValue());
  25. }
  26. //再封装会map中
  27. map.put(key , value);
  28. }
  29. }
  30. }
  31. System.out.println("map = " + map);
  32. //再次遍历map
  33. Set <Map.Entry <String, String>> entries = map.entrySet();
  34. for (Map.Entry <String, String> entry : entries) {
  35. SubjectInfoVo subjectInfoVo = new SubjectInfoVo();
  36. String key = entry.getKey();
  37. if (key != null){
  38. String subjectName = reportMpper.selectSubjectName(key);
  39. subjectInfoVo.setSubject(subjectName);
  40. subjectInfoVo.setNum(Integer.parseInt(entry.getValue()));
  41. //将结果封装回list
  42. list.add(subjectInfoVo);
  43. }
  44. }
  45. return list;
  46. }