背景:前端要求只要对象特定的字段值,其他字段全部不要传前端说是为了安全,我能怎么办,前端要啥我做啥呗。后端的DAO层写的通用查询方法,返回一个对象集合,然后因为对象中又包含了各种各样的属性,因为懒不想去专门写个特定的mapper接口。为了只拿到特定的属性于是想到了使用jdk8的stream流中的filter。
注意!!!steam流处理数据建议在service层实现类里面写,我这里为了演示,图方便就在controller接口层写了
数据处理前:
处理前,有多个属性
用List<8大基本数据类型>封装
@GetMapping("/selectExamStationList")
public String examStationList(@RequestParam Long examManageId, ModelMap map){
HsExamStationRelation stationRelation=new HsExamStationRelation();
//状态正常标识
stationRelation.setStatus("0");
//未删除标识
stationRelation.setDelFlag("0");
stationRelation.setExamManageId(examManageId);
List<HsExamStationRelation> relationList=hsExamStationRelationService.selectHsExamStationRelationList(stationRelation);
List<Long> list=relationList.stream()
.filter(relation->relation.getExamManageId()!=null)
.map(HsExamStationRelation::getExamManageId)
.collect(Collectors.toList());
map.put("selectedList",list);
return examStationPrefix + "/selectExamStationList";
}
调试结果:
处理后的数据只保留了我需要的examManageId值