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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM