shop--8. Product category--batch operation (backend)

1. Bulk add

dao layers

/**
     * Bulk new product categories
     * @param productCategoryList
     * The number of lines affected by @return
     */
    public int batchInsertProductCategory(List<ProductCategory> productCategoryList);

  

When writing mapper, because List is passed in, parameterType=“java.util.List” to be written

Then you need to use <foreach to do bulk inserts

<!--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 layer

/**
     * Bulk add product categories
     * @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( "Failed to create product category" );
                }else{
                    return new ProductCategoryExecution( ProductCategoryEnum.SUCCESS);
                }
            } catch (Exception e){
                throw new ProductCategoryException("batchAddProductCategory Error: " + e.getMessage());
            }
        } else{
            return new ProductCategoryExecution(ProductCategoryEnum.EMPTY_LIST);
        }
    }

  

Controller layer

@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", "Please enter at least one product category");
        }
        return modelMap;
    }

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324711906&siteId=291194637