实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 概述
- Dao層
- ProductCategoryDao接口
- ProductCategoryDao SQL映射文件
- 單元測(cè)試
- Service層
- ProductCategoryExecution DTO類的開(kāi)發(fā)
- ProductCategoryStateEnum 增加幾個(gè)標(biāo)識(shí)
- 封裝特定異常類
- ProductCategoryService接口
- ProductCategoryServiceImpl實(shí)現(xiàn)類
- 單元測(cè)試
- Controller層
- ProductCategoryController增加addProductCategory方法
- 單元測(cè)試
- View層
- productcategorymanage.js
- 前后端聯(lián)調(diào)
- Github地址
概述
上一篇博文 實(shí)戰(zhàn)SSM_O2O商鋪_25【商品類別】商品類別列表展示從Dao到View層的開(kāi)發(fā) ,我們完成了 商品類別 列表展示的開(kāi)發(fā),接下來(lái),我們繼續(xù)來(lái)完成 【批量添加商品類別】的功能吧。
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>單元測(cè)試
@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);}單元測(cè)試OK。
Service層
ProductCategoryExecution DTO類的開(kāi)發(fā)
我們需要增加操作的狀態(tài)及數(shù)量等信息,因此單獨(dú)的Domain類已經(jīng)無(wú)法滿足需求了,因此我們使用DTO來(lái)擴(kuò)展實(shí)體類的功能
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的返回結(jié)果,包括操作狀態(tài)和ProductCategory信息* * @author: Mr.Yang* * @date: 2018年6月21日 上午12:17:07*/ public class ProductCategoryExecution {private int state;private String stateInfo;// 因?yàn)槭桥坎僮?所以使用Listprivate List<ProductCategory> productCategoryList;private int count;/*** * * @Title:ProductCategoryExecution* * @Description:空的構(gòu)造函數(shù)*/public ProductCategoryExecution() {super();}/*** * * @Title:ProductCategoryExecution* * @Description:操作成功的時(shí)候使用的構(gòu)造函數(shù),返回操作狀態(tài)和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:操作失敗的時(shí)候返回的信息,僅包含狀態(tài)和狀態(tài)描述即可* * @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 增加幾個(gè)標(biāo)識(shí)
SUCCESS(1, "操作成功"), INNER_ERROR(-1001, "操作失敗"), NULL_SHOP(-1002, "Shop信息為空"), EMPETY_LIST(-1003, "請(qǐng)輸入商品目錄信息");封裝特定異常類
批量添加,這里我們使用事務(wù)控制
package com.artisan.o2o.exception;/*** * * @ClassName: ProductCategoryOperationException* * @Description: 繼承RuntimeException,便于異常時(shí)候的回滾。 保持所有的操作在一個(gè)事務(wù)中。* * 這樣在標(biāo)注了@Transactional事務(wù)的方法中,出現(xiàn)了異常,才會(huì)回滾數(shù)據(jù)。* * 默認(rèn)情況下,如果在事務(wù)中拋出了未檢查異常(繼承自 RuntimeException 的異常)或者 Error,則 Spring* 將回滾事務(wù);除此之外,Spring 不會(huì)回滾事務(wù)。* * * @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實(shí)現(xiàn)類
/*** 使用@Transactional控制事務(wù)*/@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);}}單元測(cè)試
@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());}單元測(cè)試通過(guò)。
Controller層
ProductCategoryController增加addProductCategory方法
/*** * * @Title: addProductCategory* * @Description: 添加商鋪目錄 ,使用@RequestBody接收前端傳遞過(guò)來(lái)的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設(shè)置shopIdfor (ProductCategory productCategory : productCategoryList) {productCategory.setShopId(currentShop.getShopId());}try {// 批量插入ProductCategoryExecution pce = productCategoryService.addProductCategory(productCategoryList);if (pce.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {modelMap.put("success", true);// 同時(shí)也將新增成功的數(shù)量返回給前臺(tái)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", "至少輸入一個(gè)店鋪目錄信息");}return modelMap;}單元測(cè)試
待前端頁(yè)面完成,一并測(cè)試
View層
productcategorymanage.js
$(function () {// 后臺(tái)從session中獲取shop的信息,這里就不傳shopId了//var shopId = getQueryString("shopId");//var productCategoryURL = '/o2o/shopadmin/getproductcategorybyshopId?shopId=' + shopId;var getProductCategoryURL = '/o2o/shopadmin/getproductcategorybyshopId';var addProductCategoryURL = '/o2o/shopadmin/addproductcategory';// 調(diào)用getProductCategoryList,加載數(shù)據(jù)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);}});}// 新增按鈕的點(diǎn)擊事件$('#new').click(function(){// 新增數(shù)據(jù) 以 temp 為標(biāo)識(shí),便于和庫(kù)表中的數(shù)據(jù)區(qū)分開(kāi)來(lái)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="優(yōu)先級(jí)"></div>'+ '<div class="col-33"><a href="#" class="button delete">刪除</a></div>'+ '</div>';$('.product-categroy-wrap').append(tempHtml);});$('#submit').click(function() {// 通過(guò)temp 獲取新增的行var tempArr = $('.temp');// 定義數(shù)組接收新增的數(shù)據(jù)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',// 后端通過(guò) @HttpRequestBody直接接收data : JSON.stringify(productCategoryList),contentType : 'application/json',success : function(data) {if (data.success) {$.toast('新增【' + data.effectNum + '】條成功!');// 重新加載數(shù)據(jù)getProductCategoryList();} else {$.toast(data.errMsg);}}});});});前后端聯(lián)調(diào)
前端頁(yè)面debug, 后端也可以加入斷點(diǎn),以debug的方式開(kāi)啟tomcat,逐步調(diào)測(cè)
效果如下:
庫(kù)表數(shù)據(jù):
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結(jié)
以上是生活随笔為你收集整理的实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Oracle-Oracle SQL Re
- 下一篇: 实战SSM_O2O商铺_27【商品类别】