1 ProductCategoryDao.java
package com.tzb.o2o.dao;
import com.tzb.o2o.entity.ProductCategory;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ProductCategoryDao {
List<ProductCategory> queryProductCategoryList(long shopId);
int batchInsertProductCategory(List<ProductCategory> productCategoryList);
int deleteProductCategory(@Param("productCategoryId") long productCategoryId, @Param("shopId") long shopId);
}
1.1 ProductCategoryDao.xml
<delete id="deleteProductCategory">
DELETE FROM
tb_product_category
WHERE
product_category_id = #{productCategoryId}
AND shop_id = #{shopId}
</delete>
1.2 单元测试 ProductCategoryDaoTest.java
@Test
public void testDeleteProductCategory() throws Exception {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
for (ProductCategory pc : productCategoryList) {
if ("商品类别4".equals(pc.getProductCategoryName()) || "商品类别5".equals(pc.getProductCategoryName())) {
int effectedNum = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(),shopId);
assertEquals(1, effectedNum);
}
}
}
2 商品类别删除Service
ProductCategoryService.java
ProductCategoryExecution deleteProductCategory(
long productCategoryId, long shopId) throws ProductCategoryOperationException;
ProductCategoryServiceImpl.java
@Override
public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {
try{
int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId,shopId);
if(effectedNum <= 0){
throw new ProductCategoryOperationException("商品类别删除失败");
}else{
return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
}
}catch (Exception e){
throw new ProductCategoryOperationException("deleteProductCategory error" + e.getMessage());
}
}
3 商品类别删除Controller
ProductCategoryManagementController.java
@RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)
private Map<String, Object> removeProductCategory(Long productCategoryId, HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<>();
if (productCategoryId != null && productCategoryId > 0) {
try{
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()){
modelMap.put("success",true);
}else{
modelMap.put("success",false);
modelMap.put("errMsg", pe.getStateInfo());
}
}catch (ProductCategoryOperationException e){
modelMap.put("success",false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
}else{
modelMap.put("success",false);
modelMap.put("errMsg", "请至少选择一个商品类别");
}
return modelMap;
}