文章目录
对集合中的元素分组并将实体类转VO
@Service
@Slf4j
public class ITsNewStandardServiceImpl extends ServiceImpl<TsNewStandardMapper,TsNewStandard> implements ITsNewStandardService {
@Autowired
TsNewStandardMapper tsNewStandardMapper;
@Override
public Integer saveStandard(TsNewStandard tsNewStandard) {return tsNewStandardMapper.insert(tsNewStandard);}
@Override
public Map<String,List<TsNewStandardVo>> getStandard(Long id) {
if (ObjectUtil.isNotEmpty(id)) {
//有id根据id查询
TsNewStandard tsNewStandard = tsNewStandardMapper.selectById(id);
if (ObjectUtil.isNotEmpty(tsNewStandard)) {
Map<String, List<TsNewStandardVo>> tsNewStandardMap = new HashMap<>();
ArrayList<TsNewStandardVo> tsNewStandardList = new ArrayList<>();
TsNewStandardVo tsNewStandardVo = new TsNewStandardVo();
BeanUtils.copyProperties(tsNewStandard, tsNewStandardVo);
tsNewStandardList.add(tsNewStandardVo);
tsNewStandardMap.put(tsNewStandardVo.getParentName(), tsNewStandardList);
return tsNewStandardMap;
} else {
return null;
}
} else {
//没传id查询全部
List<TsNewStandard> tsNewStandards = tsNewStandardMapper.selectList(null);
if (tsNewStandards.size() > 0) {
//转换成VO根据父节点分组
Map<String, List<TsNewStandardVo>> collect = tsNewStandards
.stream()
.map(st -> {
TsNewStandardVo tsNewStandardVo = new TsNewStandardVo();
BeanUtils.copyProperties(st, tsNewStandardVo);
return tsNewStandardVo;
})
.collect(Collectors.groupingBy(TsNewStandardVo::getParentName));
return collect;
}
}
return null;
}
}
还要把Map集合序列化
使用FastJson实现Map与前端json对象的交互
@Api(tags = "新操作规范接口")
@RestController
@RequestMapping("/newStandard")
public class TsNewStandardController {
@Autowired
private ITsNewStandardService service;
@ApiOperation(value = "获取规范信息", notes = "获取规范信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "规范id", required = true)
})
@GetMapping("/getStandard")
public Result getStandard(@RequestParam(required = false) Long id) {
Map<String, List<TsNewStandardVo>> map = service.getStandard(id);
String json= JSON.toJSONString(map);
return Result.success(map);
}
@ApiOperation(value = "保存规范列表", notes = "保存规范列表")
@PostMapping("/saveStandardList")
public Result<TsNewStandard> saveStandard(@RequestBody TsNewStandard tsNewStandard) {
Integer res= service.saveStandard(tsNewStandard);
return Result.success(res);
}
}
第一个上线的接口纪念一下