1.起因
今天接到了新需求,要从一张表中查出某一年的数据并按月返回给前端(终端),一开始想到分开每个月去数据库查询,查询结果直接赋值给返回对象即可,但这样相当于你要查12次数据库,很明显这个方案被pass掉了,然后我想查出整年的数据然后在Java中进行解决。
2.解决方案
我想到的比较简洁且快速的就是使用lambda表达式先过滤再赋值即可,这里还用到了Hutool工具类
//一月
List<ProgramPlanEntity> january = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 0).collect(Collectors.toList());
//二月
List<ProgramPlanEntity> february = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 1).collect(Collectors.toList());
//三月
List<ProgramPlanEntity> march = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 2).collect(Collectors.toList());
//四月
List<ProgramPlanEntity> april = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 3).collect(Collectors.toList());
//五月
List<ProgramPlanEntity> may = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 4).collect(Collectors.toList());
//六月
List<ProgramPlanEntity> june = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 5).collect(Collectors.toList());
//七月
List<ProgramPlanEntity> july = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 6).collect(Collectors.toList());
//八月
List<ProgramPlanEntity> august = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 7).collect(Collectors.toList());
//九月
List<ProgramPlanEntity> september = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 8).collect(Collectors.toList());
//十月
List<ProgramPlanEntity> october = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 9).collect(Collectors.toList());
//十一月
List<ProgramPlanEntity> november = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 10).collect(Collectors.toList());
//十二月
List<ProgramPlanEntity> december = programPlanEntityList.stream().filter(item -> DateUtil.month(item.getPlayTime()) == 11).collect(Collectors.toList());
programPlanYearVO.setJanuary(january);
programPlanYearVO.setFebruary(february);
programPlanYearVO.setMarch(march);
programPlanYearVO.setApril(april);
programPlanYearVO.setMay(may);
programPlanYearVO.setJune(june);
programPlanYearVO.setJuly(july);
programPlanYearVO.setAugust(august);
programPlanYearVO.setSeptember(september);
programPlanYearVO.setOctober(october);
programPlanYearVO.setNovember(november);
programPlanYearVO.setDecember(december);
VO类
@Data
public class ProgramPlanYearVO extends BaseEntity {
//一月
private List<ProgramPlanEntity> january;
//二月
private List<ProgramPlanEntity> february;
//三月
private List<ProgramPlanEntity> march;
//四月
private List<ProgramPlanEntity> april;
//五月
private List<ProgramPlanEntity> may;
//六月
private List<ProgramPlanEntity> june;
//七月
private List<ProgramPlanEntity> july;
//八月
private List<ProgramPlanEntity> august;
//九月
private List<ProgramPlanEntity> september;
//十月
private List<ProgramPlanEntity> october;
//十一月
private List<ProgramPlanEntity> november;
//十二月
private List<ProgramPlanEntity> december;
}
起码是完成了功能,如果有更好的方法欢迎提供思路,感激不尽