程序员社区

【校园商铺SSM-23】商品编辑--Dao层的实现

文章目录

      • 1. 商品编辑--删除商品详情图
        • 1. ProductImgDao接口
        • 2. ProductImgDao.xml实现类
        • 3. ProductImgDaoTest实现类
      • 2. 商品编辑--查询商品详情图
        • 2.1 ProductImgDao接口
        • 2.2 ProductImgDao.xml
      • 3. 商品编辑--查询商品信息
        • 3.1 ProductDao接口
        • 3.2 ProductDao.xml实现类
        • 3.3 ProducDaoTest测试类
      • 4. 商品编辑--更新商品信息
        • 4.1 ProductDao接口
        • 4.2 ProductDao.xml实现类
        • 4.3 ProductDaoTest实现类

1. 商品编辑–删除商品详情图

1. ProductImgDao接口

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

2. ProductImgDao.xml实现类

    <delete id="deleteProductImgByProductId">
        delete from
        tb_product_img
        where
        product_id=#{productId}
    </delete>

3. ProductImgDaoTest实现类

    @Test
    public void testCDeleteProductImgByProductId(){
        long productId =24;
        int effectedNum = productImgDao.deleteProductImgByProductId(productId);
        assertEquals(2,effectedNum);
    }

2. 商品编辑–查询商品详情图

2.1 ProductImgDao接口

/**
* 根据商品Id查询商品对应的详情图
* @param productId
* @return
*/
List<ProductImg> queryProductImgList(long productId);

2.2 ProductImgDao.xml

    <select id="queryProductImgList" resultType="com.imooc.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
			product_img_id
	</select>

3. 商品编辑–查询商品信息

3.1 ProductDao接口

    /**
     * 通过productId查询唯一的商品信息
     * @param productId
     * @return
     */
    Product queryProductById(long productId);

3.2 ProductDao.xml实现类

    <resultMap id="productMap" type="com.imooc.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.imooc.o2o.entity.ProductCategory">
            <id column="product_category_id" property="productCategoryId"/>
            <result column="product_category_name" property="productCategoryName"/>
        </association>
        <association property="shop" 
                     column="shop_id" 
                     javaType="com.imooc.o2o.entity.Shop">
            <id column="shop_id" property="shopId"/>
            <result column="shop_name" property="shopName"/>
        </association>
        <collection property="productImgList"
                    column="product_id"
                    ofType="com.imooc.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">
        <!--具体的sql-->
        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,
        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>

3.3 ProducDaoTest测试类

   @Test
    public void testCQueryProductByProductId() throws Exception{
        //初始化两个商品详情图作为productId为24的商品下的详情图
        long productId = 24;
        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为的商品信息
        Product product = productDao.queryProductById(productId);
        assertEquals(2,product.getProductImgList().size());
    }

4. 商品编辑–更新商品信息

4.1 ProductDao接口

    /**
     * 根据传入的商品实体类,更新商品信息
     * @param product
     * @return
     */
    int updateProduct(Product product);

4.2 ProductDao.xml实现类

    <update id="updateProduct" parameterType="com.imooc.o2o.entity.Product">
        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=#{promotionPrice},</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>

4.3 ProductDaoTest实现类

首先查看下数据库中的信息,再确定更新那个商品信息:
在这里插入图片描述

    @Test
    public void testDUpdateProduct() throws Exception{
        //必须和数据库中的数据对应
        Shop shop = new Shop();
        shop.setShopId(143L);
        ProductCategory productCategory = new ProductCategory();
        productCategory.setProductCategoryId(8L);

        Product product = new Product();
        product.setProductId(20L);
        product.setProductName("小黄人和大白");
        product.setShop(shop);
        product.setProductCategory(productCategory);

        int effectedNum = productDao.updateProduct(product);
        assertEquals(1,effectedNum);
    }

在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【校园商铺SSM-23】商品编辑--Dao层的实现

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区