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";
    

      

  • 相关阅读:
    小程序自定义组件(3)子向父传参
    postgresql插件安装
    二进制减法的实现
    mysql锁表问题
    mysql查看修改参数
    众数问题-找出超过一半的数
    只出现一次的数
    元素最大间距离
    第一个缺失数字
    局部最小值位置
  • 原文地址:https://www.cnblogs.com/achengmu/p/9974639.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com