Spring 中Bean实体之间属性值的拷贝

项目开发中常常会用到属性赋值操作,特别是电商系统订单商品详情信息列表,数据库实体和DO 之间的转换,

Java8 遍历对象组装list集合,并通过Spring 中的BeanUtils拷贝对象

//订单数据
List<EsBuyerOrderDO> buyerOrderList = esBuyerOrderList.getRecords();
		//遍历订单数据
        buyerOrderList.stream().map(EsBuyerOrderDO -> {
     
            // 赋值订单商品明细
            String orderSn = EsBuyerOrderDO.getOrderSn();
            // 订单商品明细数据
            List<EsOrderItemsDO> esOrderItemsDOList = iEsOrderItemsService.getEsOrderItemsByOrderSn(orderSn);
            List<EsBuyerOrderItemsDO> esBuyerOrderItemsDOList = new ArrayList<>();
            esBuyerOrderItemsDOList = esOrderItemsDOList.stream().map(EsOrderItemsDO -> {
     
                EsBuyerOrderItemsDO esBuyerOrderItemsDO = new EsBuyerOrderItemsDO();
                BeanUtils.copyProperties(EsOrderItemsDO,esBuyerOrderItemsDO);
                // 赋值规格json
                String specJson = EsOrderItemsDO.getSpecJson();
                if (StringUtils.isNotEmpty(specJson)) {
     
                    List<EsSpecValuesDO> specValuesDOList = JsonUtil.jsonToList(specJson, EsSpecValuesDO.class);
                    esBuyerOrderItemsDO.setSpecList(specValuesDOList);
                }
                return esBuyerOrderItemsDO;
            }).collect(Collectors.toList());
            EsBuyerOrderDO.setEsBuyerOrderItemsDO(esBuyerOrderItemsDOList);
            return buyerOrderList;
        }).collect(Collectors.toList());

你可能感兴趣的:(自成长,spring,bean)