实战SSM_O2O商铺_28【商品】商品添加之Dao层的实现

举报
小工匠 发表于 2021/09/10 01:48:26 2021/09/10
【摘要】 文章目录 概述重温实体类ProuctProductImg ProductDao接口Mapper配置文件ProductImgDao接口Mapper配置文件单元测试#ProductDaoTestP...

在这里插入图片描述

概述

完成了商品类别的功能后,

实战SSM_O2O商铺_25【商品类别】商品类别列表展示从Dao到View层的开发

实战SSM_O2O商铺_26【商品类别】批量新增商品类别从Dao到View层的开发

实战SSM_O2O商铺_27【商品类别】删除商品类别从Dao到View层的开发


接下来我们继续实现商品部分的功能。

功能点:

1. 商品的添加

2. 商品图片的批量添加(主要是指商品详情部分的图片)


重温实体类

Prouct

package com.artisan.o2o.entity;

import java.util.Date;
import java.util.List;

public class Product {
	
	private Long productId;
	private String productName;
	private String productDesc;

	/**
	 * 简略图
	 */
	private String imgAddr;

	/**
	 * 原价
	 */
	private String normalPrice;

	/**
	 * 折后价
	 */
	private String promotionPrice;
	private Integer priority;
	private Date createTime;
	private Date lastEditTime;

	/**
	 * -1 不可用 0 下架 1 展示
	 */
	private Integer enableStatus;

	/**
	 * 产品对应的详情列表,一对多
	 */
	private List<ProductImg> productImgList;

	/**
	 * 产品所属产品目录
	 */
	private ProductCategory productCategory;

	/**
	 * 产品所属店铺
	 */
	private Shop shop;
	
	// setter/gettter省略

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

ProductImg

package com.artisan.o2o.entity;

import java.util.Date;

public class ProductImg {
	private Long productImgId;
	private String imgAddr;
	private String imgDesc;
	private Integer priority;
	private Date createTime;
	// 只需要一个productId属性,不需要获取Product的其他属性,所以这里直接使用Long类型的productId,而没有使用Product。
	private Long productId;

	// setter/gettter省略

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

ProductDao接口

package com.artisan.o2o.dao;

import com.artisan.o2o.entity.Product;

public interface ProductDao {

	/**
	 * 
	 * 
	 * @Title: insertProduct
	 * 
	 * @Description: 增加商品
	 * 
	 * @param product
	 * 
	 * @return: int
	 */
	int insertProduct(Product product);
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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.artisan.o2o.dao.ProductDao">
	
	<insert id="insertProduct" 
		parameterType="com.artisan.o2o.entity.Product" 
		useGeneratedKeys="true" keyProperty="productId" keyColumn="product_id">
		INSERT INTO
			tb_product
			(
				product_name,
				product_desc,
				img_addr,
				normal_price,
				promotion_price,
				priority,
				create_time,
				last_edit_time,
				enable_status,
				product_category_id,
				shop_id
			)
		VALUES(
			#{productName},
			#{productDesc},
			#{imgAddr},
			#{normalPrice},
			#{promotionPrice},
			#{priority},
			#{createTime},
			#{lastEditTime},
			#{enableStatus},
			#{productCategory.productCategoryId},
			#{shop.shopId}
		)
	</insert>
	
</mapper>  

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

ProductImgDao接口

package com.artisan.o2o.dao;

import java.util.List;

import com.artisan.o2o.entity.ProductImg;

public interface ProductImgDao {

	/**
	 * 
	 * 
	 * @Title: batchInsertProductImg
	 * 
	 * @Description: 一个商品下可能拥有多个图片,所以这里是批量新增商品图片
	 * 
	 * @param productImgList
	 * 
	 * @return: int
	 */
	int batchInsertProductImg(List<ProductImg> productImgList);
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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.artisan.o2o.dao.ProductImgDao">
	
	<insert id="batchInsertProductImg" 
		parameterType="com.artisan.o2o.entity.ProductImg" 
		useGeneratedKeys="true" keyProperty="productImgId" keyColumn="product_img_id">
		INSERT INTO
			tb_product_img
			(
				img_addr,
				img_desc,
				priority,
				create_time,
				product_id
			)
		VALUES
			<foreach collection="list" item="productImg" index="index"  separator=",">
				(
					#{productImg.imgAddr},
					#{productImg.imgDesc},
					#{productImg.priority},
					#{productImg.createTime},
					#{productImg.productId}
				)
			</foreach>
	</insert>
	
</mapper>  

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

单元测试

注意表之间的外键关系,确保数据之间的约束正确。

否则会引起如下类似错误

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product_img`, CONSTRAINT `fk_proimg_product` FOREIGN KEY (`product_id`) REFERENCES `tb_product` (`product_id`) ON DELETE CASCADE ON UPDATE CASCADE)

  
 
  • 1

#ProductDaoTest

package com.artisan.o2o.dao;

import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.Product;
import com.artisan.o2o.entity.ProductCategory;
import com.artisan.o2o.entity.Shop;

public class ProductDaoTest extends BaseTest {

	@Autowired
	ProductDao productDao;

	@Test
	public void testInsertProdcut() {

		// 注意表中的外键关系,确保这些数据在对应的表中的存在
		ProductCategory productCategory = new ProductCategory();
		productCategory.setProductCategoryId(36L);

		// 注意表中的外键关系,确保这些数据在对应的表中的存在
		Shop shop = new Shop();
		shop.setShopId(5L);

		Product product = new Product();
		product.setProductName("test_product");
		product.setProductDesc("product desc");
		product.setImgAddr("/aaa/bbb");
		product.setNormalPrice("10");
		product.setPromotionPrice("8");
		product.setPriority(66);
		product.setCreateTime(new Date());
		product.setLastEditTime(new Date());
		product.setEnableStatus(1);
		product.setProductCategory(productCategory);
		product.setShop(shop);

		int effectNum = productDao.insertProduct(product);
		Assert.assertEquals(1, effectNum);
	}

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

ProductImgDaoTest

package com.artisan.o2o.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.ProductImg;

public class ProductImgDaoTest extends BaseTest {

	@Autowired
	private ProductImgDao productImgDao;

	@Test
	public void testBatchInsertProductImg() {
		ProductImg productImg1 = new ProductImg();
		productImg1.setImgAddr("/xiaogongjiang/xxxx");
		productImg1.setImgDesc("商品详情图片1");
		productImg1.setPriority(99);
		productImg1.setCreateTime(new Date());
		productImg1.setProductId(2L);
		
		ProductImg productImg2 = new ProductImg();
		productImg2.setImgAddr("/artisan/xxxx");
		productImg2.setImgDesc("商品详情图片2");
		productImg2.setPriority(98);
		productImg2.setCreateTime(new Date());
		productImg2.setProductId(2L);

		// 添加到productImgList中
		List<ProductImg> productImgList = new ArrayList<ProductImg>();
		productImgList.add(productImg1);
		productImgList.add(productImg2);

		// 调用接口批量新增商品详情图片
		int effectNum = productImgDao.batchInsertProductImg(productImgList);

		Assert.assertEquals(2, effectNum);


	}

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

单元测试正常 ,测试通过


Github地址

代码地址: https://github.com/yangshangwei/o2o

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/80796423

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。