校园商铺-7商品类别模块-5商品类别删除后端开发
Posted csj2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了校园商铺-7商品类别模块-5商品类别删除后端开发相关的知识,希望对你有一定的参考价值。
1.Dao层
1.1Dao接口
package com.csj2018.o2o.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryDao {
/**
* 查询某个店铺下的商品分类
* @param shopId
* @return
*/
List<ProductCategory> queryProductCategoryList(long shopId);
/**
* 批量添加商品分类
* @param productCategoryList
* @return
*/
int batchInsertProductCategory(List<ProductCategory> productCategoryList);
/**
* 7-5新增 删除商品类别。由于是2个参数,mybatis认不出来,必须用@Param注解来表明它的参数名
* @param productCategoryId
* @param shopId
* @return
*/
int deleteProductCategory(@Param("productCategoryId")long productCategoryId,@Param("shopId")long shopId);
}
问题:用productCategoryId就可以确认唯一的productCategory。为什么还要加shopId?
目的:要做更安全的控制。需要店铺id和商品类别id同时正确,才能删除。
1.2 mapper文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.csj2018.o2o.dao.ProductCategoryDao">
<select id="queryProductCategoryList" resultType="com.csj2018.o2o.entity.ProductCategory" parameterType="Long">
SELECT
product_category_id,
product_category_name,
priority,
create_time,
shop_id
from tb_product_category
where
shop_id = #{shopId}
order by priority desc
</select>
<insert id="batchInsertProductCategory" parameterType="java.util.List">
insert into
tb_product_category(product_category_name,priority,create_time,shop_id)
values
<foreach collection="list" item="productCategory" index="index" separator=",">
(
#{productCategory.productCategoryName},
#{productCategory.priority},
#{productCategory.createTime},
#{productCategory.shopId}
)
</foreach>
</insert>
<delete id="deleteProductCategory">
delete from tb_product_category where product_category_id = #{productCategoryId} and shop_id =${shopId}
</delete>
</mapper>
1.3 单元测试
package com.csj2018.o2o.dao;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
public class ProductCategoryDaoTest extends BaseTest {
@Autowired
private ProductCategoryDao productCategoryDao;
...
//7-5新增
@Test
public void testDeleteProductCategory() {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
for(ProductCategory pc:productCategoryList) {
if("批量插入商品类别1".equals(pc.getProductCategoryName())||"批量插入商品类别2".equals(pc.getProductCategoryName())) {
int count = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(), shopId);
assertEquals(1,count);
}
}
}
}
1.4 Junit用例指定执行顺序
在执行UT之前,有一点需要注意。就是执行Junit测试时,默认的顺序是随机的。在写UT的时候,有时需要按照顺序执行测试方法,已实现测试回环。
- MethodSorters.DEFAULT:以确定但不可预期的顺序去执行,默认。
- MethodSorters.JVM:按照JVM得到的方法顺序执行(不好控制),即代码中定义的方法顺序
- MethodSorters.NAME_ASCENDING:按照方法名字顺序去执行
package com.csj2018.o2o.dao;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductCategoryDaoTest extends BaseTest {
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
// @Ignore
public void testBQueryByShopId() throws Exception {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
System.out.println("该店铺自定义商品类别数为:"+productCategoryList.size());
}
@Test
// @Ignore
public void testABatchInsertProductCategory() {
ProductCategory productCategory1 = new ProductCategory();
productCategory1.setProductCategoryName("批量插入商品类别1");
productCategory1.setPriority(1);
productCategory1.setCreateTime(new Date());
productCategory1.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(productCategory1);
productCategoryList.add(productCategory2);
int count = productCategoryDao.batchInsertProductCategory(productCategoryList);
assertEquals(2,count);
}
//7-5新增
@Test
public void testCDeleteProductCategory() {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
for(ProductCategory pc:productCategoryList) {
if("批量插入商品类别1".equals(pc.getProductCategoryName())||"批量插入商品类别2".equals(pc.getProductCategoryName())) {
int count = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(), shopId);
assertEquals(1,count);
}
}
}
}
2 Service层
2.1 接口
package com.csj2018.o2o.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryDao {
/**
* 查询某个店铺下的商品分类
* @param shopId
* @return
*/
List<ProductCategory> queryProductCategoryList(long shopId);
/**
* 批量添加商品分类
* @param productCategoryList
* @return
*/
int batchInsertProductCategory(List<ProductCategory> productCategoryList);
/**
* 7-5删除商品类别
* @param productCategoryId
* @param shopId
* @return
*/
int deleteProductCategory(@Param("productCategoryId")long productCategoryId,@Param("shopId")long shopId);
}
2.2 实现类
package com.csj2018.o2o.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.csj2018.o2o.dao.ProductCategoryDao;
import com.csj2018.o2o.dto.ProductCategoryExecution;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.enums.ProductCategoryStateEnum;
import com.csj2018.o2o.exceptions.ProductCategoryOperationException;
import com.csj2018.o2o.service.ProductCategoryService;
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Autowired
private ProductCategoryDao productCategoryDao;
@Override
public List<ProductCategory> getProductCategoryList(long shopId) {
// TODO Auto-generated method stub
return productCategoryDao.queryProductCategoryList(shopId);
}
//7.3增加
@Override
@Transactional
public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList)
throws ProductCategoryOperationException {
if(productCategoryList != null && productCategoryList.size() >0) {
try {
int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
if(effectedNum <= 0) {
throw new ProductCategoryOperationException("店铺类别创建失败");
}else {
return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
}
}catch (Exception e) {
throw new ProductCategoryOperationException("batchAddProductCategory error:" + e.getMessage());
}
}else {
return new ProductCategoryExecution(ProductCategoryStateEnum.EMPTY_LIST);
}
}
//7-5
@Override
public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId)
throws ProductCategoryOperationException {
// TODO 将该类别下的商品类别置为空,以后添加
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("delete ProductCategory error:"+e.getMessage());
}
}
}
3.controller层
package com.csj2018.o2o.web.shopadmin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.csj2018.o2o.dto.ProductCategoryExecution;
import com.csj2018.o2o.dto.Result;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.enums.ProductCategoryStateEnum;
import com.csj2018.o2o.exceptions.ProductCategoryOperationException;
import com.csj2018.o2o.service.ProductCategoryService;
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
@Autowired
private ProductCategoryService productCategoryService;
@RequestMapping(value="/getproductcategorylist",method=RequestMethod.GET)
@ResponseBody
private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
//测试用,可删除
// Shop shop = new Shop();
// shop.setShopId(1L);
// request.getSession().setAttribute("currentShop", shop);
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
List<ProductCategory> list = null;
if(currentShop.getShopId() != null && currentShop.getShopId()>0){
list = productCategoryService.getProductCategoryList(currentShop.getShopId());
return new Result<List<ProductCategory>>(true,list);
}else {
ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
return new Result<List<ProductCategory>>(false,ps.getState(),ps.getStateInfo());
}
}
@RequestMapping(value="/addproductcategory",method=RequestMethod.POST)
@ResponseBody
private Map<String,Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,HttpServletRequest request){
Map<String,Object> modelMap = new HashMap<String,Object>();
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
for(ProductCategory pc:productCategoryList) {
pc.setShopId(currentShop.getShopId());
}
if(productCategoryList != null && productCategoryList.size()>0) {
try {
ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
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());
}
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "请输入至少一个商品类别");
}
return modelMap;
}
@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 {
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.getMessage());
}
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "请至少选择一个类别");
}
return modelMap;
}
}
以上是关于校园商铺-7商品类别模块-5商品类别删除后端开发的主要内容,如果未能解决你的问题,请参考以下文章