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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实战SSM_O2O商铺_27【商品类别】删除商品类别从Dao到View层的开发

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

文章目錄

  • 概述
  • Dao層
    • ProductCategoryDao接口增加接口方法
    • ProductCategoryDao SQL映射文件
    • 閉環(huán)的單元測試
  • Servie層
    • 接口
    • 接口實現(xiàn)
    • 單元測試
  • Controller層
    • 路由方法
    • 單元測試
  • View層
    • productcategorymanage.js
  • 聯(lián)調(diào)
  • Github地址

概述

上篇博客 實戰(zhàn)SSM_O2O商鋪_26【商品類別】批量新增商品類別從Dao到View層的開發(fā)實現(xiàn)了商品目錄的批量添加功能,我們按照既定的設(shè)計,繼續(xù)來完成商品目錄的修改吧。


Dao層

ProductCategoryDao接口增加接口方法

/*** * * @Title: deleteProductCategory* * @Description: 刪除特定shop下的productCategory* * @param productCategoryId* @param shopId* * @return: int*/int deleteProductCategory(@Param("productCategoryId") Long productCategoryId, @Param("shopId") Long shopId);

ProductCategoryDao SQL映射文件

<delete id="deleteProductCategory">DELETE FROM tb_product_categoryWHERE product_category_id = #{productCategoryId}and shop_id = #{shopId}</delete>

閉環(huán)的單元測試

這里我們使用Junit 4.11里及其以后的版本中增加的@FixMethodOrder注解來實現(xiàn). 具體見代碼注釋。

package com.artisan.o2o.dao;import static org.junit.Assert.assertEquals;import java.util.ArrayList; import java.util.Date; import java.util.List;import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ProductCategory;/*** * * @ClassName: ProductCategoryTest* * @Description: Junit 4.11里增加了指定測試方法執(zhí)行順序的特性 .* * 測試類的執(zhí)行順序可通過對測試類添加注解@FixMethodOrder(value) 來指定,其中value 為執(zhí)行順序* * 三種執(zhí)行順序可供選擇:* * 默認(rèn)(MethodSorters.DEFAULT),* 默認(rèn)順序由方法名hashcode值來決定,如果hash值大小一致,則按名字的字典順序確定* 由于hashcode的生成和操作系統(tǒng)相關(guān)* (以native修飾),所以對于不同操作系統(tǒng),可能會出現(xiàn)不一樣的執(zhí)行順序,在某一操作系統(tǒng)上,多次執(zhí)行的順序不變* * 按方法名( MethodSorters.NAME_ASCENDING)【推薦】,* 按方法名稱的進(jìn)行排序,由于是按字符的字典順序,所以以這種方式指定執(zhí)行順序會始終保持一致;* 不過這種方式需要對測試方法有一定的命名規(guī)則,如 測試方法均以testNNN開頭(NNN表示測試方法序列號 001-999)* * JVM(MethodSorters.JVM)* 按JVM返回的方法名的順序執(zhí)行,此種方式下測試方法的執(zhí)行順序是不可預(yù)測的,即每次運行的順序可能都不一樣* * * @author: Mr.Yang* * @date: 2018年6月21日 下午11:55:45*/ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ProductCategoryTest extends BaseTest {@AutowiredProductCategoryDao productCategoryDao;@Testpublic void testB_SelectProductCategoryList() {long shopId = 5L;List<ProductCategory> productCategories = productCategoryDao.selectProductCategoryList(shopId);// shopId = 5 有2條測試數(shù)據(jù),期望list中有2條assertEquals(2, productCategories.size());// SQL中按照權(quán)重排序, product1 priority 99 ,期望第一條數(shù)據(jù)是 product1assertEquals("product1", productCategories.get(0).getProductCategoryName());for (ProductCategory productCategory : productCategories) {System.out.println(productCategory.toString());}productCategories = productCategoryDao.selectProductCategoryList(6L);assertEquals(0, productCategories.size());}@Testpublic void testA_BatchInsertProductCategory() {ProductCategory productCategory1 = new ProductCategory();productCategory1.setProductCategoryName("product1");productCategory1.setProductCategoryDesc("product1_desc");productCategory1.setPriority(99);productCategory1.setCreateTime(new Date());productCategory1.setLastEditTime(new Date());productCategory1.setShopId(5L);ProductCategory productCategory2 = new ProductCategory();productCategory2.setProductCategoryName("product2");productCategory2.setProductCategoryDesc("product2_desc");productCategory2.setPriority(98);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);}@Testpublic void testC_DeleteProductCategory() {// 查詢出來shopId=5的商鋪下面全部的商品目錄List<ProductCategory> productCategoryList = productCategoryDao.selectProductCategoryList(5L);// 遍歷循環(huán)刪除for (ProductCategory productCategory : productCategoryList) {if ("product1".equals(productCategory.getProductCategoryName()) || "product2".equals(productCategory.getProductCategoryName())) {int effectNum = productCategoryDao.deleteProductCategory(productCategory.getProductCategoryId(), 5L);assertEquals(1, effectNum);}}}}

運行單元測試

日志信息:

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7fb95505] will not be managed by Spring ==> Preparing: INSERT INTO tb_product_category( product_category_name, product_category_desc, priority, create_time, last_edit_time, shop_id) VALUES ( ?, ?, ?, ?, ?, ? ) , ( ?, ?, ?, ?, ?, ? ) ==> Parameters: product1(String), product1_desc(String), 99(Integer), 2018-06-22 00:17:25.611(Timestamp), 2018-06-22 00:17:25.611(Timestamp), 5(Long), product2(String), product2_desc(String), 98(Integer), 2018-06-22 00:17:25.612(Timestamp), 2018-06-22 00:17:25.612(Timestamp), 5(Long) <== Updates: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@38b27cdc] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@336f1079] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@21fd5faa] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 5(Long) <== Columns: product_category_id, product_category_name, product_category_desc, priority, create_time, last_edit_time, shop_id <== Row: 24, product1, product1_desc, 99, 2018-06-22 00:17:26.0, 2018-06-22 00:17:26.0, 5 <== Row: 25, product2, product2_desc, 98, 2018-06-22 00:17:26.0, 2018-06-22 00:17:26.0, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@336f1079] ProductCategory [productCategoryId=24, shopId=5, productCategoryName=product1, productCategoryDesc=product1_desc, priority=99, createTime=Fri Jun 22 00:17:26 BOT 2018, lastEditTime=Fri Jun 22 00:17:26 BOT 2018] ProductCategory [productCategoryId=25, shopId=5, productCategoryName=product2, productCategoryDesc=product2_desc, priority=98, createTime=Fri Jun 22 00:17:26 BOT 2018, lastEditTime=Fri Jun 22 00:17:26 BOT 2018] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19b93fa8] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@1f010bf0] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 6(Long) <== Total: 0 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19b93fa8] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@2631f68c] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 5(Long) <== Columns: product_category_id, product_category_name, product_category_desc, priority, create_time, last_edit_time, shop_id <== Row: 24, product1, product1_desc, 99, 2018-06-22 00:17:26.0, 2018-06-22 00:17:26.0, 5 <== Row: 25, product2, product2_desc, 98, 2018-06-22 00:17:26.0, 2018-06-22 00:17:26.0, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5443d039] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d1cfb8b] will not be managed by Spring ==> Preparing: DELETE FROM tb_product_category WHERE product_category_id = ? and shop_id = ? ==> Parameters: 24(Long), 5(Long) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5443d039] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6e4566f1] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@344f4dea] will not be managed by Spring ==> Preparing: DELETE FROM tb_product_category WHERE product_category_id = ? and shop_id = ? ==> Parameters: 25(Long), 5(Long) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6e4566f1]

Servie層

接口

/*** * * @Title: deleteProductCategory* * @Description: TODO 需要先將該商品目錄下的商品的類別Id置為空,然后再刪除該商品目錄, 因此需要事務(wù)控制* * @param productCategoryId* @param shopId* @throws ProductCategoryOperationException* * @return: ProductCategoryExecution*/ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException;

接口實現(xiàn)

/*** TODO 需要先將該商品目錄下的商品的類別Id置為空,然后再刪除該商品目錄, 因此需要事務(wù)控制@Transactional*/@Override@Transactionalpublic ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {// TODO 第一步 需要先將該商品目錄下的商品的類別Id置為空// 第二步 刪除該商品目錄try {int effectNum = productCategoryDao.deleteProductCategory(productCategoryId, shopId);if (effectNum > 0) {return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);} else {return new ProductCategoryExecution(ProductCategoryStateEnum.INNER_ERROR);}} catch (Exception e) {throw new ProductCategoryOperationException(e.getMessage());}}

單元測試

@Testpublic void testDeleteProductCategory() {ProductCategoryExecution productCategoryExecution = productCategoryService.deleteProductCategory(26, 5);Assert.assertEquals(1, productCategoryExecution.getState());ProductCategoryExecution productCategoryExecution2 = productCategoryService.deleteProductCategory(27, 5);Assert.assertEquals(1, productCategoryExecution2.getState());}

Controller層

路由方法

/*** * * @Title: remooveProductCategory* * @Description: 刪除商品目錄* * @param productCategoryId* @param request* * @return: Map<String,Object>*/@RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> remooveProductCategory(Long productCategoryId, HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();if (productCategoryId != null && productCategoryId > 0) {// 從session中獲取shop的信息Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");if (currentShop != null && currentShop.getShopId() != null) {try {// 刪除Long shopId = currentShop.getShopId();ProductCategoryExecution pce = productCategoryService.deleteProductCategory(productCategoryId, shopId);if (pce.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {modelMap.put("success", true);} 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

增加如下代碼

var deleteProductCategoryUrl = '/o2o/shopadmin/removeproductcategory';// 一種是需要提交到后臺的刪除 now ,另外一種是 新增但未提交到數(shù)據(jù)庫中的刪除 temp$('.product-categroy-wrap').on('click', '.row-product-category.now .delete',function(e) {var target = e.currentTarget;$.confirm('確定么?', function() {$.ajax({url : deleteProductCategoryUrl,type : 'POST',data : {productCategoryId : target.dataset.id,},dataType : 'json',success : function(data) {if (data.success) {$.toast('刪除成功!');// 重新加載數(shù)據(jù)getProductCategoryList();} else {$.toast('刪除失敗!');}}});});});$('.product-categroy-wrap').on('click', '.row-product-category.temp .delete',function(e) {$(this).parent().parent().remove();});

聯(lián)調(diào)

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

效果如下:


Github地址

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

總結(jié)

以上是生活随笔為你收集整理的实战SSM_O2O商铺_27【商品类别】删除商品类别从Dao到View层的开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。