dao层
1 /** 2 * 根据productCategoryId 和 shopId删除商品类别 3 * 因为是有两个参数的 所以使用param标签 4 * 5 * @param productCategoryId 6 * @param shopId 7 * @return 8 */ 9 int deleteProductCategory(@Param("productCategoryId") long productCategoryId,@Param("shopId") long shopId);
映射文件
<delete id="deleteProductCategory"> DELETE FROM tb_product_category WHERE product_category_id = #{productCategoryId} AND shop_id = #{shopId} </delete>
1 在做单元测试的时候可以使用@FixMethodOrder(MethodSorters.NAME_ASCENDING) 2 来调整测试方法顺序 3 public class ProductCategoryDaoTest extends BaseTest{ 4 @Autowired 5 private ProductCategoryDao dao; 6 7 @Test 8 public void testBQueryProductCategory() { 9 List<ProductCategory> list = dao.queryProductCategoryList(1); 10 assertEquals(5, list.size()); 11 } 12 13 @Test 14 public void testABatchInsertProductCategory() { 15 ProductCategory productCategory = new ProductCategory(); 16 productCategory.setProductCategoryName("商品类别1"); 17 productCategory.setPriority(1); 18 productCategory.setCreateTime(new Date()); 19 productCategory.setShopId(1L); 20 ProductCategory productCategory2 = new ProductCategory(); 21 productCategory2.setProductCategoryName("商品类别2"); 22 productCategory2.setPriority(2); 23 productCategory2.setCreateTime(new Date()); 24 productCategory2.setShopId(1L); 25 List<ProductCategory> productCategoryList = new ArrayList<>(); 26 productCategoryList.add(productCategory); 27 productCategoryList.add(productCategory2); 28 int effectNum = dao.batchInsertProductCategory(productCategoryList); 29 assertEquals(2, effectNum); 30 } 31 32 @Test 33 public void testCDeleteProductCategory() throws Exception{ 34 long shopId = 1; 35 List<ProductCategory> productCategoryList = dao.queryProductCategoryList(shopId); 36 for(ProductCategory pc : productCategoryList) { 37 if("商品类别1".equals(pc.getProductCategoryName())||"商品类别2".equals(pc.getProductCategoryName())) { 38 int effectedNum = dao.deleteProductCategory(pc.getProductCategoryId(), shopId); 39 assertEquals(1, effectedNum); 40 } 41 } 42 } 43 }
Service层
1 /** 2 * 将此类别下的商品里的类别id置为空,再删除掉改商品类别 3 * @param productCategory 4 * @param shopId 5 * @return 6 * @throws ProductCategoryOperationException 7 */ 8 ProductCategoryExecution deleteProductCategory(long productCategoryId,long shopId) throws ProductCategoryOperationException;
Service的实现类
1 //由于先将商品类别 商品id置为空 再删除 分为两步 所以使用事务 2 @Transactional 3 @Override 4 public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) 5 throws ProductCategoryOperationException { 6 // TODO 将此商品类别下的商品Id置为空 7 try { 8 int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId, shopId); 9 if(effectedNum <= 0) { 10 throw new ProductCategoryOperationException("商品类别删除失败"); 11 }else { 12 return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS); 13 } 14 }catch (Exception e) { 15 throw new ProductCategoryOperationException("deleteProductCategory error :" + e.getMessage()); 16 } 17 }
Controller层
1 @RequestMapping(value = "/removeproductcategory",method = RequestMethod.POST) 2 @ResponseBody 3 private Map<String , Object> removeProductCategory(Long productCategoryId,HttpServletRequest request){ 4 Map<String, Object> modelMap = new HashMap<String,Object>(); 5 if(productCategoryId!=null&&productCategoryId>0) { 6 try { 7 Shop currentShop = (Shop)request.getSession().getAttribute("currentShop"); 8 ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId()); 9 if(pe.getState()==ProductCategoryStateEnum.SUCCESS.getState()) { 10 modelMap.put("success", true); 11 }else { 12 modelMap.put("success", false); 13 modelMap.put("errMsg", pe.getStateInfo()); 14 } 15 }catch (ProductCategoryOperationException e) { 16 modelMap.put("success", false); 17 modelMap.put("errMsg", e.toString()); 18 return modelMap; 19 } 20 }else { 21 modelMap.put("success", false); 22 modelMap.put("errMsg", "请至少选择一个商品类别"); 23 } 24 return modelMap; 25 }