实战SSM_O2O商铺_38【商品类别】解除商品与商品类别的关联
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_38【商品类别】解除商品与商品类别的关联
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- Dao層
- ProductDao.java
- ProductDao.xml
- 單元測試
- Service層完善
- ProductCategoryServiceImpl#deleteProductCategory
- 單元測試
- Github地址
概述
在 實戰SSM_O2O商鋪_27【商品類別】刪除商品類別從Dao到View層的開發 我們留下了一個TODO,在deleteProductCategory方法中,需要先將該商品目錄下的商品的類別Id置為空,然后再刪除該商品目錄。 下面我們在完成了商品的邏輯后,來完善缺失的部分。
Dao層
ProductDao.java
增加updateProductCategory2Null方法
/*** * * @Title: updateProductCategory2Null* * @Description: * 刪除productCategory的時候,需要先將tb_product中的該productCategoryId置為null* * @param productCategoryId* @param shopId* * @return: int*/int updateProductCategory2Null(@Param("productCategoryId") long productCategoryId, @Param("shopId") long shopId);ProductDao.xml
<update id="updateProductCategory2Null">UPDATE tb_productSET product_category_id = nullWHERE product_category_id = #{productCategoryId}AND shop_id = #{shopId}</update>單元測試
@Testpublic void testUpdateProductCategory2Null() {long productCategoryId = 37L;long shopId = 5L;int effectNum = productDao.updateProductCategory2Null(productCategoryId, shopId);Assert.assertEquals(1, effectNum);productCategoryId = 36L;effectNum = productDao.updateProductCategory2Null(productCategoryId, shopId);Assert.assertEquals(6, effectNum);}結合數據庫中的數據,設置合理的預期,單元測試通過
Service層完善
ProductCategoryServiceImpl#deleteProductCategory
/*** 需要先將該商品目錄下的商品的類別Id置為空,然后再刪除該商品目錄, 因此需要事務控制@Transactional*/@Override@Transactionalpublic ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {// 第一步 需要先將該商品目錄下的商品的類別Id置為空try {int effectNum = productDao.updateProductCategory2Null(productCategoryId, shopId);if (effectNum < 0) {throw new ProductCategoryOperationException("商品類別更新失敗");}} catch (Exception e) {throw new ProductCategoryOperationException(e.getMessage());}// 第二步 刪除該商品目錄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());}}單元測試
編寫單元測試用例,這里就省略了,因為新增的部分只調用了一個Dao層的方法。
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_38【商品类别】解除商品与商品类别的关联的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_37【商品】商品
- 下一篇: 实战SSM_O2O商铺_39【前端展示】