实战SSM_O2O商铺_38【商品类别】解除商品与商品类别的关联
        【摘要】 
                    
                        
                    
                    
 文章目录
 概述Dao层ProductDao.javaProductDao.xml单元测试
  Service层完善ProductCategoryServiceImpl#deleteProductC...
    
    
    
    概述
在 实战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);
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
ProductDao.xml
	<update id="updateProductCategory2Null">
		UPDATE 
			tb_product
		SET 
			product_category_id = null
		WHERE 
			product_category_id = #{productCategoryId}
		AND 
			shop_id = #{shopId}
	</update>	
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
单元测试
@Test
	public 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);
	}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
结合数据库中的数据,设置合理的预期,单元测试通过
Service层完善
ProductCategoryServiceImpl#deleteProductCategory
/**
	 * 需要先将该商品目录下的商品的类别Id置为空,然后再删除该商品目录, 因此需要事务控制@Transactional
	 */
	@Override
	@Transactional
	public 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());
		}
	}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
单元测试
编写单元测试用例,这里就省略了,因为新增的部分只调用了一个Dao层的方法。
Github地址
代码地址: https://github.com/yangshangwei/o2o
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/80968399
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)