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

1.商品编辑页面

1.1如何处理已存在的图片

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

1.2操作步骤

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

2.Dao层

Dao层已在https://www.cnblogs.com/csj2018/p/12445473.html建立

// 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" /><!-- 有疑问,为什么不是img_addr -->
            <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}
    </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>

2.2 商品图片的实现

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>

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.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);
    }
}

商品Dao层单元测试

猜你喜欢

转载自www.cnblogs.com/csj2018/p/12500734.html