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

    dao层

    1     /**
    2      * 批量新增商品类别
    3      * 
    4      * @param productCategoryList
    5      * @return
    6      */
    7     int batchInsertProductCategory(List<ProductCategory> productCategoryList);

    映射文件

        <insert id="batchInsertProductCategory"
            parameterType="java.util.List">
            INSERT INTO
            tb_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层

    1     /**
    2      * 批量添加商品类别
    3      * 
    4      * @param productCategoryList
    5      * @return
    6      * @throws ProductCategoryOperationException
    7      */
    8     ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException;

    Service的实现类

     1     @Transactional
     2     @Override
     3     public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList)
     4             throws ProductCategoryOperationException {
     5         if(productCategoryList != null && productCategoryList.size() > 0) {
     6             try {
     7                 int effectNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
     8                 if(effectNum <= 0) {
     9                     throw new ProductCategoryOperationException("创建店铺类别失败");
    10                 }else {
    11                     return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
    12                 }
    13             }catch (Exception e) {
    14                 throw new ProductCategoryOperationException("batchAddProductCategory error :" + e.getMessage());
    15             }
    16         }else {
    17             return new ProductCategoryExecution(ProductCategoryStateEnum.EMPTY_LIST);
    18         }
    19     }

    Controller层

     1     @RequestMapping(value = "/addproductcategorys", method = RequestMethod.POST)
     2     @ResponseBody
     3     private Map<String, Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,
     4             HttpServletRequest request) {
     5         Map<String, Object> modelMap = new HashMap<String, Object>();
     6         /* 为什么要取出这个值呢,因为productCategoryList是需要店铺id的 */
     7         Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
     8         // 将需要添加的每一个类别都设置成当前店铺的shopid
     9         for (ProductCategory pc : productCategoryList) {
    10             pc.setShopId(currentShop.getShopId());
    11         }
    12 
    13         if (productCategoryList != null && productCategoryList.size() > 0) {
    14             try {
    15                 ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
    16                 if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
    17                     modelMap.put("success", true);
    18                 } else {
    19                     modelMap.put("success", false);
    20                     modelMap.put("errMsg", pe.getStateInfo());
    21                 }
    22             } catch (ProductCategoryOperationException e) {
    23                 modelMap.put("success", false);
    24                 modelMap.put("errMsg", e.toString());
    25                 return modelMap;
    26             }
    27         } else {
    28             modelMap.put("success", false);
    29             modelMap.put("errMsg", "请输入至少一个商品类别");
    30         }
    31         return modelMap;
    32     }
  • 相关阅读:
    42.纯 CSS 创作一个均衡器 loader 动画
    41.纯 CSS 绘制一支栩栩如生的铅笔
    1.如何在Cloud Studio上执行Python代码?
    2.每个 HTML 文件里开头都有个<!DOCTYPE>
    39.纯 CSS 创作一个表达怀念童年心情的条纹彩虹心特效
    LOJ #2127. 「HAOI2015」按位或 min-max容斥+FWT
    HDU
    LOJ #3044. 「ZJOI2019」Minimax 搜索 动态DP+概率
    LOJ #3043. 「ZJOI2019」线段树 线段树+分类讨论
    Comet OJ
  • 原文地址:https://www.cnblogs.com/windbag7/p/9391099.html
Copyright © 2011-2022 走看看