日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发

發布時間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • Dao層
    • ProductCategoryDao接口
    • ProductCategoryDao SQL映射文件
    • 單元測試
  • Service層
    • ProductCategoryExecution DTO類的開發
    • ProductCategoryStateEnum 增加幾個標識
    • 封裝特定異常類
    • ProductCategoryService接口
    • ProductCategoryServiceImpl實現類
    • 單元測試
  • Controller層
    • ProductCategoryController增加addProductCategory方法
    • 單元測試
  • View層
    • productcategorymanage.js
  • 前后端聯調
  • Github地址

概述

上一篇博文 實戰SSM_O2O商鋪_25【商品類別】商品類別列表展示從Dao到View層的開發 ,我們完成了 商品類別 列表展示的開發,接下來,我們繼續來完成 【批量添加商品類別】的功能吧。


Dao層

ProductCategoryDao接口

/*** * * @Title: batchInsertProductCategory* * @Description: 批量增加roductCategory* * @param productCategoryList* * @return: int*/int batchInsertProductCategory(List<ProductCategory> productCategoryList);

ProductCategoryDao SQL映射文件

<insert id="batchInsertProductCategory" parameterType="java.util.List">INSERT INTOtb_product_category(product_category_name,product_category_desc,priority,create_time,last_edit_time,shop_id)VALUES <foreach collection="list" item="productCategory" index="index" separator=",">(#{productCategory.productCategoryName},#{productCategory.productCategoryDesc},#{productCategory.priority},#{productCategory.createTime},#{productCategory.lastEditTime},#{productCategory.shopId})</foreach></insert>

單元測試

@Testpublic void testBatchInsertProductCategory() {ProductCategory productCategory1 = new ProductCategory();productCategory1.setProductCategoryName("ProductCategoryTest1");productCategory1.setProductCategoryDesc("ProductCategoryTest1-desc");productCategory1.setPriority(300);productCategory1.setCreateTime(new Date());productCategory1.setLastEditTime(new Date());productCategory1.setShopId(5L);ProductCategory productCategory2 = new ProductCategory();productCategory2.setProductCategoryName("ProductCategoryTest2");productCategory2.setProductCategoryDesc("ProductCategoryTest2-desc");productCategory2.setPriority(600);productCategory2.setCreateTime(new Date());productCategory2.setLastEditTime(new Date());productCategory2.setShopId(5L);List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();productCategoryList.add(productCategory1);productCategoryList.add(productCategory2);int effectNum = productCategoryDao.batchInsertProductCategory(productCategoryList);Assert.assertEquals(2, effectNum);}

單元測試OK。


Service層

ProductCategoryExecution DTO類的開發

我們需要增加操作的狀態及數量等信息,因此單獨的Domain類已經無法滿足需求了,因此我們使用DTO來擴展實體類的功能

package com.artisan.o2o.dto;import java.util.List;import com.artisan.o2o.entity.ProductCategory; import com.artisan.o2o.enums.ProductCategoryStateEnum;/*** * * @ClassName: ProductCategoryExecution* * @Description: 封裝操作ProductCategory的返回結果,包括操作狀態和ProductCategory信息* * @author: Mr.Yang* * @date: 2018年6月21日 上午12:17:07*/ public class ProductCategoryExecution {private int state;private String stateInfo;// 因為是批量操作,所以使用Listprivate List<ProductCategory> productCategoryList;private int count;/*** * * @Title:ProductCategoryExecution* * @Description:空的構造函數*/public ProductCategoryExecution() {super();}/*** * * @Title:ProductCategoryExecution* * @Description:操作成功的時候使用的構造函數,返回操作狀態和ProductCategory集合* * @param productCategoryStateEnum* @param productCategoryList* @param count*/public ProductCategoryExecution(ProductCategoryStateEnum productCategoryStateEnum, List<ProductCategory> productCategoryList, int count) {this.state = productCategoryStateEnum.getState();this.stateInfo = productCategoryStateEnum.getStateInfo();this.productCategoryList = productCategoryList;this.count = count;}/*** * * @Title:ProductCategoryExecution* * @Description:操作失敗的時候返回的信息,僅包含狀態和狀態描述即可* * @param productCategoryStateEnum*/public ProductCategoryExecution(ProductCategoryStateEnum productCategoryStateEnum) {this.state = productCategoryStateEnum.getState();this.stateInfo = productCategoryStateEnum.getStateInfo();}public int getState() {return state;}public void setState(int state) {this.state = state;}public String getStateInfo() {return stateInfo;}public void setStateInfo(String stateInfo) {this.stateInfo = stateInfo;}public List<ProductCategory> getProductCategoryList() {return productCategoryList;}public void setProductCategoryList(List<ProductCategory> productCategoryList) {this.productCategoryList = productCategoryList;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}

ProductCategoryStateEnum 增加幾個標識

SUCCESS(1, "操作成功"), INNER_ERROR(-1001, "操作失敗"), NULL_SHOP(-1002, "Shop信息為空"), EMPETY_LIST(-1003, "請輸入商品目錄信息");

封裝特定異常類

批量添加,這里我們使用事務控制

package com.artisan.o2o.exception;/*** * * @ClassName: ProductCategoryOperationException* * @Description: 繼承RuntimeException,便于異常時候的回滾。 保持所有的操作在一個事務中。* * 這樣在標注了@Transactional事務的方法中,出現了異常,才會回滾數據。* * 默認情況下,如果在事務中拋出了未檢查異常(繼承自 RuntimeException 的異常)或者 Error,則 Spring* 將回滾事務;除此之外,Spring 不會回滾事務。* * * @author: Mr.Yang* * @date: 2018年6月21日 上午12:22:44*/ public class ProductCategoryOperationException extends RuntimeException {private static final long serialVersionUID = 6500682256313143297L;public ProductCategoryOperationException(String message) {super(message);}}

ProductCategoryService接口

/*** * * @Title: addProductCategory* * @Description: 批量插入ProductCategory* * @param productCategoryList* @throws ProductCategoryOperationException* * @return: ProductCategoryExecution*/ProductCategoryExecution addProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException;

ProductCategoryServiceImpl實現類

/*** 使用@Transactional控制事務*/@Override@Transactionalpublic ProductCategoryExecution addProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException {// 非空判斷if (productCategoryList != null && productCategoryList.size() > 0) {try {// 批量增加ProductCategoryint effectNum = productCategoryDao.batchInsertProductCategory(productCategoryList);if (effectNum > 0) {return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS, productCategoryList, effectNum);} else {return new ProductCategoryExecution(ProductCategoryStateEnum.INNER_ERROR);}} catch (Exception e) {e.printStackTrace();throw new ProductCategoryOperationException("batchAddProductCategory Error:" + e.getMessage());}} else {return new ProductCategoryExecution(ProductCategoryStateEnum.EMPETY_LIST);}}

單元測試

@Testpublic void testAddProductCategory() {ProductCategory productCategory1 = new ProductCategory();productCategory1.setProductCategoryName("ProductCategoryTest3");productCategory1.setProductCategoryDesc("ProductCategoryTest3-desc");productCategory1.setPriority(300);productCategory1.setCreateTime(new Date());productCategory1.setLastEditTime(new Date());productCategory1.setShopId(5L);ProductCategory productCategory2 = new ProductCategory();productCategory2.setProductCategoryName("ProductCategoryTest4");productCategory2.setProductCategoryDesc("ProductCategoryTest4-desc");productCategory2.setPriority(600);productCategory2.setCreateTime(new Date());productCategory2.setLastEditTime(new Date());productCategory2.setShopId(5L);List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();productCategoryList.add(productCategory1);productCategoryList.add(productCategory2);ProductCategoryExecution productCategoryExecution = productCategoryService.addProductCategory(productCategoryList);Assert.assertEquals(1, productCategoryExecution.getState());Assert.assertEquals(2, productCategoryExecution.getProductCategoryList().size());}

單元測試通過。


Controller層

ProductCategoryController增加addProductCategory方法

/*** * * @Title: addProductCategory* * @Description: 添加商鋪目錄 ,使用@RequestBody接收前端傳遞過來的productCategoryList* * @param productCategoryList* @param request* * @return: Map<String,Object>*/@RequestMapping(value = "/addproductcategory", method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> addProductCategory(@RequestBody List<ProductCategory> productCategoryList, HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();if (productCategoryList != null && productCategoryList.size() > 0) {// 從session中獲取shop的信息Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");if (currentShop != null && currentShop.getShopId() != null) {// 為ProductCategory設置shopIdfor (ProductCategory productCategory : productCategoryList) {productCategory.setShopId(currentShop.getShopId());}try {// 批量插入ProductCategoryExecution pce = productCategoryService.addProductCategory(productCategoryList);if (pce.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {modelMap.put("success", true);// 同時也將新增成功的數量返回給前臺modelMap.put("effectNum", pce.getCount());} else {modelMap.put("success", false);modelMap.put("errMsg", pce.getStateInfo());}} catch (ProductCategoryOperationException e) {e.printStackTrace();modelMap.put("success", false);modelMap.put("errMsg", e.getMessage());return modelMap;}} else {modelMap.put("success", false);modelMap.put("errMsg", ProductCategoryStateEnum.NULL_SHOP.getStateInfo());}} else {modelMap.put("success", false);modelMap.put("errMsg", "至少輸入一個店鋪目錄信息");}return modelMap;}

單元測試

待前端頁面完成,一并測試


View層

productcategorymanage.js

$(function () {// 后臺從session中獲取shop的信息,這里就不傳shopId了//var shopId = getQueryString("shopId");//var productCategoryURL = '/o2o/shopadmin/getproductcategorybyshopId?shopId=' + shopId;var getProductCategoryURL = '/o2o/shopadmin/getproductcategorybyshopId';var addProductCategoryURL = '/o2o/shopadmin/addproductcategory';// 調用getProductCategoryList,加載數據getProductCategoryList();function getProductCategoryList() {$.getJSON(getProductCategoryURL,function(data) {if (data.success) {var dataList = data.data;$('.product-categroy-wrap').html('');var tempHtml = '';dataList.map(function(item, index) {tempHtml += ''+ '<div class="row row-product-category now">'+ '<div class="col-33 product-category-name">'+ item.productCategoryName+ '</div>'+ '<div class="col-33">'+ item.priority+ '</div>'+ '<div class="col-33"><a href="#" class="button delete" data-id="'+ item.productCategoryId+ '">刪除</a></div>'+ '</div>';});$('.product-categroy-wrap').append(tempHtml);}});}// 新增按鈕的點擊事件$('#new').click(function(){// 新增數據 以 temp 為標識,便于和庫表中的數據區分開來var tempHtml = '<div class="row row-product-category temp">'+ '<div class="col-33"><input class="category-input category" type="text" placeholder="分類名"></div>'+ '<div class="col-33"><input class="category-input priority" type="number" placeholder="優先級"></div>'+ '<div class="col-33"><a href="#" class="button delete">刪除</a></div>'+ '</div>';$('.product-categroy-wrap').append(tempHtml);});$('#submit').click(function() {// 通過temp 獲取新增的行var tempArr = $('.temp');// 定義數組接收新增的數據var productCategoryList = [];tempArr.map(function(index, item) {var tempObj = {};tempObj.productCategoryName = $(item).find('.category').val();tempObj.priority = $(item).find('.priority').val();if (tempObj.productCategoryName && tempObj.priority) {productCategoryList.push(tempObj);}});$.ajax({url : addProductCategoryURL,type : 'POST',// 后端通過 @HttpRequestBody直接接收data : JSON.stringify(productCategoryList),contentType : 'application/json',success : function(data) {if (data.success) {$.toast('新增【' + data.effectNum + '】條成功!');// 重新加載數據getProductCategoryList();} else {$.toast(data.errMsg);}}});});});

前后端聯調

前端頁面debug, 后端也可以加入斷點,以debug的方式開啟tomcat,逐步調測

效果如下:

庫表數據:


Github地址

代碼地址: https://github.com/yangshangwei/o2o

總結

以上是生活随笔為你收集整理的实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。