zoukankan      html  css  js  c++  java
  • shop--8.商品类别--批量操作--添加(后端)

    1.批量添加

    dao层

    /**
         * 批量新增商品类别
         * @param productCategoryList
         * @return 影响的行数
         */
        public int batchInsertProductCategory(List<ProductCategory> productCategoryList);
    

      

    在编写mapper时,因为传入的是List,所以要写的parameterType=“java.util.List”

    然后需要使用<foreach来进行批量插入

    <!--public int batchInsertProductCategory(List<ProductCategory> productCategoryList);-->
        <insert id="batchInsertProductCategory" parameterType="java.util.List">
            INSERT INTO
            product_category(product_category_name, priority, create_time, shop_id)
            VALUES
            <foreach collection="list" item="productCategory" index="index" separator=",">
                (
                #{productCategory.productCategoryName},
                #{productCategory.priority},
                #{productCategory.createTime},
                #{productCategory.shopId}
                )
            </foreach>
        </insert>
    

      

    service层

    /**
         * 批量添加商品类别
         * @param productCategoryList
         * @return
         */
        public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList);
    

      

    impl

    @Override
        @Transactional
        public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList) {
            if(productCategoryList != null && productCategoryList.size() >= 0){
                int effecNum = productCategoryDao.batchInsertProductCategory( productCategoryList );
                try{
                    if(effecNum <= 0){
                        throw new ProductCategoryException( "商品类别创建失败" );
                    }else{
                        return new ProductCategoryExecution( ProductCategoryEnum.SUCCESS);
                    }
                } catch (Exception e){
                    throw new ProductCategoryException("batchAddProductCategory Error: " + e.getMessage());
                }
            } else{
                return new ProductCategoryExecution(ProductCategoryEnum.EMPTY_LIST);
            }
        }
    

      

    Controller层

    @RequestMapping(value="addproductcategories", method = RequestMethod.POST)
        @ResponseBody
        public Map<String, Object> addProductCategories(@RequestBody List<ProductCategory> productCategoryList,
                                                        HttpServletRequest request){
            Map<String, Object> modelMap = new HashMap<>();
            Shop currentShop = new Shop();
            currentShop.setShopId(1L);
            for(ProductCategory productCategory : productCategoryList){
                productCategory.setShopId( currentShop.getShopId() );
            }
            if(productCategoryList != null && productCategoryList.size() > 0){
                ProductCategoryExecution productCategoryExecution = null;
                try{
                    productCategoryExecution = productCategoryService.batchAddProductCategory( productCategoryList );
                    if(productCategoryExecution.getState() == ProductCategoryEnum.SUCCESS.getState()){
                        modelMap.put( "success", true );
                    }else{
                        modelMap.put( "success", false );
                        modelMap.put( "errMsg",  productCategoryExecution.getStateInfo());
                    }
                }catch(ProductCategoryException e){
                    modelMap.put( "success", false );
                    modelMap.put( "errMsg",  e.getMessage());
                    return modelMap;
                }
            }else{
                modelMap.put( "success", false );
                modelMap.put( "errMsg",  "请至少输入一个商品类别");
            }
            return modelMap;
        }
    

      

  • 相关阅读:
    前路漫长
    OEL6 /boot分区删除恢复
    区块链:剖析工作量证明
    【转】区块链:Schnoor签名究竟是什么
    使用 Hyperledger Fabric 开展私密交易
    Sovrin:技术落地可期,但推广难度较高
    白硕:区块链技术与数据隐私(附视频)
    从入门到精通零知识证明与zkSNARK
    区块链安全:匿名性以及隐私性
    朋友去面试Python工程师,又带回来几道基础题,Python面试题No10
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8916873.html
Copyright © 2011-2022 走看看