• shop--10.商品--商品编辑(后端)--dao层


    • 首先肯定要根据productId查到对应Product相关的信息,既然这里是Dao层的开发,所以需要在Dao层需要开发一个 selectProductById 方法
    • 商品信息有商品缩略图和详情图片,这里我们先约定好:如果用户传入了新的商品缩略图和详情图片,就将原有的商品缩略图和详情图片删除掉
    • 商品缩略图的地址存放在tb_product的img_addr字段,所以只需要更新改表即可。 所以对应Dao层应该有个方法updateProduct
    • 图片缩略图还涉及磁盘上的文件的删除,需要根据productId获取到Product ,然后获取Product的imgAddr属性,复用selectProductById 解渴
    • 详情图片的地址存放在tb_product_img中,根据product_id可以查找到对应商品下的全部详情图片,所以对应Dao层应该有个方法deleteProductImgById
    • 图片详情还涉及磁盘上的文件的删除,需要根据productId获取到List<ProductImg> ,然后遍历该list,获取集合中每个ProductImg的imgAddr地址,所以还需要有个selectProductImgList方法

      

    ProductDao层

     1     /**
     2      * 通过productId查询唯一的商品信息
     3      * @param productId
     4      * @return
     5      */
     6     Product queryProductById(long productId);
     7     
     8     /**
     9      * 更新商品信息
    10      * @param product
    11      * @return
    12      */
    13     int updateProduct(Product product);
    

    ProductDao映射文件

    <?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.ryanxu.o2o.dao.ProductDao">
    
    
        <resultMap id="productMap"
            type="com.ryanxu.o2o.entity.Product">
            <id column="product_id" property="productId" />
            <!-- property对应实体类中的属性名 column 对应库表中的字段名 -->
            <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 product中的属性为productCategory, 通过数据库中的product_category_id关联起来的 
                类型为 com.ryanxu.o2o.entity.ProductCategory -->
            <association property="productCategory"
                column="product_category_id"
                javaType="com.ryanxu.o2o.entity.ProductCategory">
                <!-- 对应ProductCategory中的属性 和 tb_product_category的字段 -->
                <id column="product_category_id" property="productCategoryId" />
                <result column="product_category_name"
                    property="productCategoryName" />
            </association>
    
            <!-- 一对一使用association product中的属性为shop, 通过数据库中的shop_id关联起来的 类型为com.ryanxu.o2o.entity.Shop -->
            <association property="shop" column="shop_id"
                javaType="com.ryanxu.o2o.entity.Shop">
                <id column="shop_id" property="shopId" />
                <!-- 对应Shop中的属性 和 tb_shop的字段 ,如果是符合对象,使用xx.xxx的方式 -->
                <result column="owner_id" property="owner.userId" />
                <result column="shop_name" property="shopName" />
            </association>
            <!-- 一对多使用collection product中的属性为productImgList,并且是通过库表中的product_id关联起来的, 
                保存的类型为com.ryanxu.o2o.entity.ProductImg -->
            <collection property="productImgList" column="product_id"
                ofType="com.ryanxu.o2o.entity.ProductImg">
                <id column="product_img_id" property="productImgId" />
                <result column="img_addr" 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>
    
        <insert id="insertProduct"
            parameterType="com.ryanxu.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>
    
    
        <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,(即使该商品没有商品详情图片,也要查询出来该商铺) -->
            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.ryanxu.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="createTime != null">create_time = #{createTime},</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>
        
    
    </mapper>

    ProductImgDao

        /**
         * 根据productId查询商铺对应的图片详情信息
         * @param productId
         * @return
         */
        List<ProductImg> queryProductImgList(long productId);
        
        /**
         * 批量添加商品详情图片
         * @param productImgList
         * @return
         */
        int batchInsertProductImg(List<ProductImg> productImgList);
        /**
         * 删除指定商品下的所有详情图
         * @param productId
         * @return
         */
        int deleteProductImgByProductId(long productId);

    ProductImgDao映射文件

    <?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.ryanxu.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>
    
        <delete id="deleteProductImgByProductId">
            <!-- 具体的sql -->
            DELETE FROM
            tb_product_img
            WHERE
            product_id =
            #{productId}
        </delete>
    
        <select id="queryProductImgList"
            resultType="com.ryanxu.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>
    </mapper>
  • 相关阅读:
    学习笔记—查找
    水晶报表图表制作问题
    Chrome对最小字体的限制
    Devexpress的ASPxDateEdit控件设置其‘today’ 为客户端当前日期
    水晶报表多表数据源
    System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded
    利用水晶报表制作甘特图
    水晶报表打印时最后多打印一空白页
    day3学习
    Python高级自动化培训day1
  • 原文地址:https://www.cnblogs.com/windbag7/p/9413592.html
Copyright © 2020-2023  润新知