校园商铺-8商品模块-7商品编辑之后段开发上

1.商品编辑页面

1.1如何处理已存在的图片

教程给出的方案是只要上传新的图片,就会将缩略图、商品详情图删除。
因为缩略图是存在与商品中的,所以只需要判断缩略图不为空即可。
详情图的处理过于简单粗暴,不会用于真实生产环境。

1.2操作步骤

1.获取商品信息
2.传入新的图片,就会将已存在的商品详情图、缩略图删除
3.点击提交,修改店铺信息

2.Dao层

Dao层已在校园商铺-8商品模块-1商品添加之Dao层建立。而商品图片Dao层及实现,单元测试都已完成。只剩商品Dao层的实现

// ProductDao 
 
    /**
     * 通过productId查询唯一的商品信息
     * @param productId
     * @return
     */
    Product queryProductById(Long productId);
    /**
     * 更新商品信息
     * @param product
     * @return
     */
    int updateProduct(Product product);

//ProductImgDao 

    /**
     * 删除指定商品下的所有详情图
     * @param productId
     * @return
     */
    int deleteProductImgByProductId(long productId);

2.1 商品层的实现

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"><!-- 对应接口类名 -->
 	<resultMap id="productMap" type="com.csj2018.o2o.entity.Product">
 		<id column="product_id" property="productId" />
 		<result column="product_name" property="productName" />
 		<result column="product_desc" property="productDesc" />
 		<result column="img_addr" property="imgAddr" />
 		<result column="normal_price" property="normalPrice" />
 		<result column="promotion_price" property="promotionPrice" />
 		<result column="priority" property="priority" />
 		<result column="create_time" property="createTime" />
 		<result column="last_edit_time" property="lastEditTime" />
 		<result column="enable_status" property="enableStatus" />
 		<association property="productCategory" column="product_category_id" javaType="com.csj2018.o2o.entity.ProductCategory">
 			<id column="product_category_id" property="productCategoryId" />
 			<result column="product_category_name" property="productCategoryName" />
 		</association>
 		<collection property="productImgList" column="product_id" ofType="com.csj2018.o2o.entity.ProductImg">
 			<id column="product_img_id" property="productImgId" />
 			<result column="detail_img" property="imgAddr" />
 			<result column="img_desc" property="imgDesc" />
 			<result column="priority" property="priority" />
 			<result column="create_time" property="createTime" />
 			<result column="product_id" property="productId" />
 		</collection>
 	</resultMap>
 	<select id="queryProductById" resultMap="productMap" parameterType="long">
 		select
 		p.product_id, p.product_name, p.product_desc, p.img_addr, p.normal_price, p.promotion_price, p.priority,
 		p.create_time, p.last_edit_time, p.enable_status, p.product_category_id, p.shop_id,
 		pm.product_img_id, pm.img_addr as detail_img, pm.img_desc, pm.priority, pm.create_time
 		from tb_product p left join tb_product_img pm 
 		on p.product_id = pm.product_id
 		where p.product_id = #{productId}
 		order by pm.priority desc
 	</select>
 	<update id="updateProduct" parameterType="com.csj2018.o2o.entity.Product" keyProperty="product_id" useGeneratedKeys="true">
 		update tb_product
 		<set>
 			<if test="productName != null">product_name = #{productName},</if>
 			<if test="productDesc != null">product_desc = #{productDesc},</if>
 			<if test="imgAddr != null">img_addr = #{imgAddr},</if>
 			<if test="normalPrice != null" >normal_price = #{normalPrice},</if>
 			<if test="promotionPrice != null">promotion_price = #{promotion_price},</if>
 			<if test="priority != null">priority = #{priority},</if>
 			<if test="lastEditTime != null">last_edit_time = #{lastEditTime},</if>
 			<if test="enableStatus != null">enable_status = #{enableStatus},</if>
 			<if test="productCategory != null and productCategory.productCategoryId != null">product_category_id = #{productCategory.productCategoryId}</if> 
 		</set>
 		where product_id = #{productId} and shop_id = #{shop.shopId}<!--shop_id = #{shop.shopId}保证了不会操作别的店铺的商品 -->
 	</update>
    <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>

3. Dao层单元测试

3.1 商品Dao层单元测试

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.Product;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.entity.ProductImg;
import com.csj2018.o2o.entity.Shop;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductDaoTest extends BaseTest{
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductImgDao productImgDao;
	
	@Test
	@Ignore
	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);
	}
	@Test
	@Ignore
	public void testBQueryProductList() {}
	@Test
	public void testCQueryProductByProductId() throws Exception{
		long productId = 1;
		//初始化两个商品详情图实例,作为productId为1的商品下的详情图片
		//批量插入到商品详情图标中
		ProductImg productImg1 = new ProductImg();
		productImg1.setImgAddr("图片1");
		productImg1.setImgDesc("测试图片1");
		productImg1.setPriority(1);
		productImg1.setCreateTime(new Date());
		productImg1.setProductId(productId);
		ProductImg productImg2 = new ProductImg();
		productImg2.setImgAddr("图片1");
		productImg2.setImgDesc("测试图片1");
		productImg2.setPriority(1);
		productImg2.setCreateTime(new Date());
		productImg2.setProductId(productId);
		List<ProductImg> productImgList = new ArrayList<>();
		productImgList.add(productImg1);
		productImgList.add(productImg2);
		int effectedNum = productImgDao.batchInsertProductImg(productImgList);
		assertEquals(2,effectedNum);
		//查询productId为1的商品信息并校验返回的详情图实例列表size是否为2
		Product product = productDao.queryProductById(productId);
		assertEquals(2,product.getProductImgList().size());
		//删除新增的这两个商品详情图示例,形成闭环
		effectedNum = productImgDao.deleteProductImgByProductId(productId);
		assertEquals(2,effectedNum);
	}
	@Test
	public void testDUpdateProduct() {
		Product product = new Product();
		ProductCategory pc = new ProductCategory();
		Shop shop = new Shop();
		shop.setShopId(1L);
		pc.setProductCategoryId(12L);
		
		product.setProductId(1L);
		product.setShop(shop);
		product.setProductCategory(pc);
		product.setProductName("躺着喝的美式咖啡");
		
		int effectedNum = productDao.updateProduct(product);
		assertEquals(1,effectedNum);
	}
	@Test
	@Ignore
	public void testEUpdateProductCategoryToNull() {}
	@Test
	@Ignore
	public void testFDeleteShopAutoMap() {}
}

posted on 2020-03-15 22:35  singleSpace  阅读(317)  评论(0编辑  收藏  举报