查询全年数据后按月进行分组 - 图1

1.起因

  1. 今天接到了新需求,要从一张表中查出某一年的数据并按月返回给前端(终端),一开始想到分开每个月去数据库查询,查询结果直接赋值给返回对象即可,但这样相当于你要查12次数据库,很明显这个方案被pass掉了,然后我想查出整年的数据然后在Java中进行解决。

2.解决方案

  1. 我想到的比较简洁且快速的就是使用lambda表达式先过滤再赋值即可,这里还用到了Hutool工具类
  1. //一月
  2. List<ProgramPlanEntity> january = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 0).collect(Collectors.toList());
  3. //二月
  4. List<ProgramPlanEntity> february = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 1).collect(Collectors.toList());
  5. //三月
  6. List<ProgramPlanEntity> march = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 2).collect(Collectors.toList());
  7. //四月
  8. List<ProgramPlanEntity> april = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 3).collect(Collectors.toList());
  9. //五月
  10. List<ProgramPlanEntity> may = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 4).collect(Collectors.toList());
  11. //六月
  12. List<ProgramPlanEntity> june = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 5).collect(Collectors.toList());
  13. //七月
  14. List<ProgramPlanEntity> july = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 6).collect(Collectors.toList());
  15. //八月
  16. List<ProgramPlanEntity> august = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 7).collect(Collectors.toList());
  17. //九月
  18. List<ProgramPlanEntity> september = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 8).collect(Collectors.toList());
  19. //十月
  20. List<ProgramPlanEntity> october = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 9).collect(Collectors.toList());
  21. //十一月
  22. List<ProgramPlanEntity> november = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 10).collect(Collectors.toList());
  23. //十二月
  24. List<ProgramPlanEntity> december = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 11).collect(Collectors.toList());
  25. programPlanYearVO.setJanuary(january);
  26. programPlanYearVO.setFebruary(february);
  27. programPlanYearVO.setMarch(march);
  28. programPlanYearVO.setApril(april);
  29. programPlanYearVO.setMay(may);
  30. programPlanYearVO.setJune(june);
  31. programPlanYearVO.setJuly(july);
  32. programPlanYearVO.setAugust(august);
  33. programPlanYearVO.setSeptember(september);
  34. programPlanYearVO.setOctober(october);
  35. programPlanYearVO.setNovember(november);
  36. programPlanYearVO.setDecember(december);

VO类

  1. @Data
  2. public class ProgramPlanYearVO extends BaseEntity {
  3. //一月
  4. private List<ProgramPlanEntity> january;
  5. //二月
  6. private List<ProgramPlanEntity> february;
  7. //三月
  8. private List<ProgramPlanEntity> march;
  9. //四月
  10. private List<ProgramPlanEntity> april;
  11. //五月
  12. private List<ProgramPlanEntity> may;
  13. //六月
  14. private List<ProgramPlanEntity> june;
  15. //七月
  16. private List<ProgramPlanEntity> july;
  17. //八月
  18. private List<ProgramPlanEntity> august;
  19. //九月
  20. private List<ProgramPlanEntity> september;
  21. //十月
  22. private List<ProgramPlanEntity> october;
  23. //十一月
  24. private List<ProgramPlanEntity> november;
  25. //十二月
  26. private List<ProgramPlanEntity> december;
  27. }

起码是完成了功能,如果有更好的方法欢迎提供思路,感激不尽