程序员社区

【校园商铺SSM-19】商品类别删除--Dao+Service+Controller+View层的实现

文章目录

      • 1. Dao层的实现
        • 1.1 ProductCategoryDao接口
        • 1.2 ProductCategoryDao.xml实现类
        • 1.3 ProductCategoryDaoTest测试类
      • 2. Service层的实现
        • 2.1 ProductCategoryService接口
        • 2.2 ProductCategoryServiceImpl实现类
      • 3. Controller层的实现
      • 4. View层的实现

1. Dao层的实现

1.1 ProductCategoryDao接口

    /**
     * 删除指定商品类别
     * @param productCategoryId
     * @param shopId
     * @return
     */
    int deleteProductCategory(@Param("productCategoryId")long productCategoryId,@Param("shopId")long shopId);

1.2 ProductCategoryDao.xml实现类

    <!--删除指定商品类别-->
    <delete id="deleteProductCategory">
        delete from tb_product_category
        where product_category_id = #{productCategoryId}
        and shop_id = #{shopId}
    </delete>

1.3 ProductCategoryDaoTest测试类

    @Test
    public void testCDeleteProductCategory(){
        long shopId = 143;
        List<ProductCategory> productCategoryList
                = productCategoryDao.queryProductCategoryList(shopId);
        for(ProductCategory pc:productCategoryList){
            if("商品类别1".equals(pc.getProductCategoryName())||"商品类别2".equals(pc.getProductCategoryName())){
                int i = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(), shopId);
                assertEquals(1,i);
            }
        }
    }

2. Service层的实现

2.1 ProductCategoryService接口

    /**
     * 将此类别里的商品里的类别id置为空,再删除掉该商品类别
     * @param productCategoryId
     * @param shopId
     * @return
     * @throws ProductCategoryOperationException
     */
    ProductCategoryExecution deleteProductCaegory(long productCategoryId,long shopId)
            throws ProductCategoryOperationException;

2.2 ProductCategoryServiceImpl实现类

    //由于将来有两步操作,需要添加事务,当第一步成功后不急于提交,而是等第二步成功后再提交
    //如果第一步失败,事务就会回滚
    @Transactional
    @Override
    public ProductCategoryExecution deleteProductCaegory(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());
        }
    }

3. Controller层的实现

   @ResponseBody
    @RequestMapping(value = "/removeproductcategory",method = RequestMethod.POST)
    public 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.deleteProductCaegory(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;
        }
        return modelMap;
    }

4. View层的实现

在productmanagement.js文件中添加:

var deleteProductCategoryUrl = '/o2o/shopadmin/removeproductcategory';
// 一种是需要提交到后台的删除now
// 另外一种是新增但未提交到数据库中的删除 temp
$('.product-categroy-wrap').on('click', '.row-product-category.now .delete',
    function(e) {
        var target = e.currentTarget;
        $.confirm('确定么?', function() {
            $.ajax({
                url : deleteProductCategoryUrl,
                type : 'POST',
                data : {
                    productCategoryId : target.dataset.id,
                },
                dataType : 'json',
                success : function(data) {
                    if (data.success) {
                        $.toast('删除成功!');
                        // 重新加载数据
                        getProductCategoryList();
                    } else {
                        $.toast('删除失败!');
                    }
                }
            });
        });
    });

$('.product-categroy-wrap').on('click', '.row-product-category.temp .delete',
    function(e) {
        $(this).parent().parent().remove();
    });

在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【校园商铺SSM-19】商品类别删除--Dao+Service+Controller+View层的实现

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区