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 }