项目中常用的Stream流转换

对集合中的元素分组并将实体类转VO

Stream流常用操作

@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);
        }

}

第一个上线的接口纪念一下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值