实战SSM_O2O商铺_38【商品类别】解除商品与商品类别的关联

概述

实战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_product
		SET 
			product_category_id = null
		WHERE 
			product_category_id = #{productCategoryId}
		AND 
			shop_id = #{shopId}
	</update>	

单元测试

@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);

	}

结合数据库中的数据,设置合理的预期,单元测试通过


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());
		}
	}

单元测试

编写单元测试用例,这里就省略了,因为新增的部分只调用了一个Dao层的方法。


Github地址

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值