商品类别批量添加后端开发-批量添加在mapper和controller的写法
本文转载自链接:https://blog.csdn.net/weixin_40703303/article/details/89607701
一、dao层
1、编写接口 ProductCategoryDao
public interface ProductCategoryDao {
/**
* 批量新增商品类别
* @param productCategoryList
* @return
*/
int batchInsertProductCategory(List<ProductCategory> productCategoryList);
}
2、编写mapper实现
批量插入用<foreach>标签
传入的因为是list所以collection是list,
item是迭代的属性名,
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
separator表示在每次进行迭代之间以什么符号作为分隔符
<!-- 批量插入 -->
<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>
3、测试
public class ProductCategoryDaoTest extends BaseTest{
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
public void testBatchInsertProductCategory() {
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);
}
}
二、service层
1、编写接口
public interface ProductCategoryService {
/**
* 批量添加商品类别
* @param productCategoryList
* @return
* @throws ProductCategoryOperationException
*/
ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList)
throws ProductCategoryOperationException;
}
2、编写接口实现类
记得要有@Transactional事务的注解,因为异常里用到了
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Autowired
private ProductCategoryDao productCategoryDao;
@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);
}
}
}
三、编写controller
注意
1、这里前端传入的是list,所以RequestMapping里的method要是POST
2、addProductCategorys中用@RequestBody接收前端传来的List
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
@Autowired
private ProductCategoryService productCategoryService;
@RequestMapping(value = "/addproductcategorys", 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 (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "请至少输入一个商品类别");
}
return modelMap;
}
}
文章最后发布于: 2019-05-16 12:12:58