校园商铺-8商品模块-1商品添加之Dao层
学习目标:
- 1.实现商品的添加
- 2.掌握批量图片添加
根据已有的店铺和商品类别,插入一条商品信息
insert into tb_product(product_name,product_desc,img_addr,normal_price,promotion_price,priority,enable_status,product_category_id,shop_id)
values('美式咖啡','美式咖啡的描述','美式咖啡的缩略图',15,10,2,1,10,1)
1. 商品Dao层
1.1 商品Dao接口
ProductDao.java
package com.csj2018.o2o.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csj2018.o2o.entity.Product;
public interface ProductDao {
List<Product> queryProductList(@Param("productCondition") Product productCondition, @Param("beginIndex") int beginIndex, @Param("pageSize") int pageSize);
int queryProductCount(Product productCondition);
Product queryProductById(Long productId);
/**
* 插入商品,商品只能一个一个的添加,不存在添加一个商品列表的情况
* @param product
* @return
*/
int insertProduct(Product product);
/**
* 更新商品信息
* @param product
* @return
*/
int updateProduct(Product product);
int updateProductCategoryToNull(Long productId);
int deleteProduct(Product product);
}
1.2 商品mapper文件 ProductDao.xml
<?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.ProductDao"><!-- 对应接口类名 -->
<insert id="insertProduct" parameterType="com.csj2018.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.3 商品Dao单元测试
package com.csj2018.o2o.dao;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.Product;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.entity.Shop;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductDaoTest extends BaseTest{
@Autowired
private ProductDao productDao;
@Autowired
private ProductImgDao productImgDao;
@Test
public void testAInsertProduct() throws Exception{
Shop shop1 = new Shop();
shop1.setShopId(1L);
ProductCategory pc1 = new ProductCategory();
pc1.setProductCategoryId(10L);
Product product1 = new Product();
product1.setProductName("商品1");
product1.setProductDesc("商品1的描述");
product1.setImgAddr("商品1的缩略图");
product1.setPriority(1);
product1.setEnableStatus(1);
product1.setCreateTime(new Date());
product1.setLastEditTime(new Date());
product1.setShop(shop1);
product1.setProductCategory(pc1);
Product product2 = new Product();
product2.setProductName("商品2");
product2.setProductDesc("商品2的描述");
product2.setImgAddr("商品2的缩略图");
product2.setPriority(2);
product2.setEnableStatus(0);
product2.setCreateTime(new Date());
product2.setLastEditTime(new Date());
product2.setShop(shop1);
product2.setProductCategory(pc1);
Product product3 = new Product();
product3.setProductName("商品3");
product3.setProductDesc("商品3的描述");
product3.setImgAddr("商品3的缩略图");
product3.setPriority(3);
product3.setEnableStatus(1);
product3.setCreateTime(new Date());
product3.setLastEditTime(new Date());
product3.setShop(shop1);
product3.setProductCategory(pc1);
int effectedNum = productDao.insertProduct(product1);
assertEquals(1,effectedNum);
effectedNum = productDao.insertProduct(product2);
assertEquals(1,effectedNum);
effectedNum = productDao.insertProduct(product3);
assertEquals(1,effectedNum);
}
}
2.商品图片Dao层
2.1 商品图片Dao层接口ProductImgDao.java
package com.csj2018.o2o.dao;
import java.util.List;
import com.csj2018.o2o.entity.ProductImg;
public interface ProductImgDao {
List<ProductImg> queryProductImgList(long productId);
/**
* 批量添加商品详情图片
* @param productImgList
* @return
*/
int batchInsertProductImg(List<ProductImg> productImgList);
int deleteProductImgByProductId(long productId);
}
2.2 mapper文件ProductImgDao.xml
<?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.ProductImgDao"><!-- 对应接口类名 -->
<insert id="batchInsertProductImg" parameterType="java.util.List">
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>
<select id="queryProductImgList" parameterType="Long" resultType="com.csj2018.o2o.entity.ProductImg">
select
product_img_id,img_addr,img_desc,priority,create_time,product_id
FROM tb_product_img
where product_id=#{productId}
order by priority desc
</select>
<delete id="deleteProductImgByProductId" parameterType="Long" >
delete from tb_product_img where product_id = #{productId}
</delete>
</mapper>
1.3Dao层单元测试
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.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductImg;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductImgDaoTest extends BaseTest{
@Autowired
private ProductImgDao productImgDao;
@Test
// @Ignore
public void testABatchInsertProductImg() throws Exception{
ProductImg productImg1 = new ProductImg();
productImg1.setImgAddr("图片1");
productImg1.setImgDesc("测试图片1");
productImg1.setPriority(1);
productImg1.setCreateTime(new Date());
productImg1.setProductId(1L);
ProductImg productImg2 = new ProductImg();
productImg2.setImgAddr("图片2");
productImg2.setImgDesc("测试图片2");
productImg2.setPriority(1);
productImg2.setCreateTime(new Date());
productImg2.setProductId(1L);
List<ProductImg> productImgList = new ArrayList<ProductImg>();
productImgList.add(productImg1);
productImgList.add(productImg2);
int effectedNum = productImgDao.batchInsertProductImg(productImgList);
assertEquals(2,effectedNum);
}
@Test
// @Ignore
public void testBQueryProductImgList() {
List<ProductImg> productImgList = productImgDao.queryProductImgList(1L);
assertEquals(2,productImgList.size());
}
@Test
// @Ignore
public void testCDeleteProductImgByProductId() throws Exception{
long productId =1;
int effectedNum = productImgDao.deleteProductImgByProductId(productId);
assertEquals(2,effectedNum);
}
}