zoukankan      html  css  js  c++  java
  • spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)

    spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)

    它的出现原因: BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。

    我有一个Category分类表对象,和一个ProductInfo商品表对象

    我需要的数据格式是(以分类作为基数,分类下面有多条帖子):

    data:[
       {
          "cid":1,
          "name":"灌水",
         "theads":[
             {
                 "id":1,
                 "cid":1,
                 "name":"有喜欢这歌的吗?"
                 "time":"2018-12-12 18:25" 
              },
              {
                 "id":1,
                 "cid":1,
                 "name":"有喜欢这歌的吗?"
                 "time":"2018-12-12 18:25" 
              }
          ]
      } ,{
          "cid":2,
          "name":"文学",
         "theads":[
             {
                 "id":18,
                 "cid":2,
                 "name":"徐志摩的诗"
                 "time":"2018-12-12 18:25" 
              },
              {
                 "id":21,
                 "cid":2,
                 "name":"鲁迅的散文"
                 "time":"2018-12-12 18:25" 
              }
          ]
      }   
    ]
    

      

    代码如下

     ResultVO resultVO = new ResultVO();
            //ProductVO productVO = new ProductVO();
            //List<ProductInfoVO> productInfoVOList  = new ArrayList<ProductInfoVO>();
            //productVO.setProductInfoVOList(productInfoVOList);
            //resultVO.setData(productVO);
            //查询商品
            List<ProductInfo> productInfoList = product.findAll();
    
    
            //查询商品类目(一次性读完)
            //传统方法
            // List<Integer> categoryTypeList = new ArrayList<>();
            //for (ProductInfo productInfo: productInfoList)
           // {
            //    categoryTypeList.add(productInfo.getCategoryType());
            //}
            //java8-lambda方法
            List<Integer> categoryTypeList = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
            List<ProductCategory> CategoryList = category.findByCategoryTypeIn(categoryTypeList);
    
    
            //拼合数据
            for (ProductCategory category: CategoryList)
            {
                //productCategory
                ProductVO productVO = new ProductVO();
                productVO.setCategoryType(category.getCategoryType());
                productVO.setCategoryName(category.getCategoryName());
    
    
                //productInfo
                List<ProductInfoVO> productInfoVOList = new ArrayList<>();
                for (ProductInfo productInfo : productInfoList)
                {
                    if(productInfo.getCategoryType().equals(category.getCategoryType()))
                    {
                        //老方法:
                        ProductInfoVO productInfoVO = new ProductInfoVO();
                        productInfoVO.setProductId(productInfo.getProductId());
                        productInfoVO.setProductName(productInfo.getProductName());
                        productInfoVOList.add(productInfoVO);
    
                        //新方法:将productInfo属性复制到productInfoVO1中
                        //beanutils.copyproperties
                        ProductInfoVO productInfoVO1 = new ProductInfoVO();
                        BeanUtils.copyProperties(productInfo, productInfoVO1);
                        productInfoVOList.add(productInfoVO1);
                    }
                }
                productVO.setProductInfoVOList(productInfoVOList);
            }
    
            return resultVO;
            //return "list";
    

      

  • 相关阅读:
    atitit.atiOrmStoreService 框架的原理与设计 part1  概述与新特性
    Atitit. atiOrder   Order 订单管理框架的设计
    Atitit.为什么小公司也要做高大上开源项目
    Atitit.atiInputMethod v2词库清理策略工具    q229
    12条黄金法则
    数据库主键设计
    xhtml常见问题
    程序员面试宝典
    采用XHTML和CSS设计可重用可换肤的WEB站点
    XHTML基础问答
  • 原文地址:https://www.cnblogs.com/achengmu/p/9974639.html
Copyright © 2011-2022 走看看