一、dao层
1、开发dao接口
public interface ProductCategoryDao {
/**
* 删除指定商品类别
* @param productCategoryId
* @param shopId
* @return effectedNum
*/
int deleteProductCategory(@Param("productCategoryId") long productCategory,@Param("shopId") long shopId);
}
2、开发mapper实现类
此处注意<delete>中没有parameterType标签,SQL语句中直接#{}取值,这是因为dao层传值用了@Param注解
<mapper namespace="com.imooc.o2o.dao.ProductCategoryDao">
<!-- 删除 -->
<delete id="deleteProductCategory">
DELETE FROM
tb_product_category
WHERE
product_category_id = #{productCategoryId}
AND shop_id = #{shopId}
<!-- 如果使用了@Param,就不需要在mapper.xml中设置parameterType属性了,所以上面两行是dao接口的参数传进来的 -->
</delete>
</mapper>
3、测试
当加入删除后,插入、查询、产出形成闭环操作,可以使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)注解对多个单元测试进行按名称顺序执行。这里把方法名分别修改加入ABC。
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductCategoryDaoTest extends BaseTest{
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
public void testBQueryByShopId() {
long shopId = 1L;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
System.out.println("该店铺自定义类别数为:" + productCategoryList.size());
}
@Test
public void testABatchInsertProductCategory() {
ProductCategory productCategory = new ProductCategory();
productCategory.setProductCategoryName("商品类别1");
productCategory.setPriority(1);
productCategory.setCreateTime(new Date());
productCategory.setShopId(1L);
ProductCategory productCategory2 = new ProductCategory();
productCategory2.setProductCategoryName("商品类别2");
productCategory2.setPriority(2);
productCategory2.setCreateTime(new Date());
productCategory2.setShopId(1L);
List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();
productCategoryList.add(productCategory);
productCategoryList.add(productCategory2);
int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
assertEquals(2, effectedNum);
}
@Test
public void testCDeleteProductCategory() {
long shopId = 1L;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
for(ProductCategory pc : productCategoryList) {
if("商品类别1".equals(pc.getProductCategoryName())||"商品类别2".equals(pc.getProductCategoryName())) {
int effectedNum = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(), shopId);
assertEquals(1, effectedNum);
}
}
}
}
二、service层
1、开发service接口
public interface ProductCategoryService {
/**
* 将此类别里的商品中的类别id置为空,再删除该商品类别
* @param productCategoryId
* @param shopId
* @return
* @throws ProductCategoryOperationException
*/
ProductCategoryExecution deleteProductCategory(long productCategoryId,long shopId)
throws ProductCategoryOperationException;
}
2、开发service实现类
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Override
@Transactional
public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId)
throws ProductCategoryOperationException {
// TODO 将此类别里的商品中的类别id置为空
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());
}
}
}
三、controller层
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
@Autowired
private ProductCategoryService productCategoryService;
@RequestMapping(value = "/removeproductcategory",method = RequestMethod.POST)
@ResponseBody
private Map<String,Object> removeProductCategory(Long productCategoryId,HttpServletRequest request){
Map<String,Object> modelMap = new HashMap<String,Object>();
if(productCategoryId != null && productCategoryId > 0) {
try {
/*getAttribute里的currentShop是ShopManagementController中
getshopmanagementinfo里setAttribute("currentShop", currentShop)。
*/
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;
}
}