目录
补充:mybatis的association以及collection的用法
一、Dao层
public interface ProductDao {
/**
* 通过productId查询唯一商品信息
* @param productId
* @return
*/
Product queryProductById(long productId);
/**
* 更新商品信息
* @param product
* @return
*/
int updateProduct(Product product);
}
public interface ProductImgDao {
/**
* 删除指定商品下的所有详情图
* @param productId
* @return
*/
int deleteProductImgByProductId(long productId);
}
二、Dao实现--mapper
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.imooc.o2o.dao.ProductDao">
<resultMap type="com.imooc.o2o.entity.Product" id="productMap">
<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="owner_id" property="owner.userId"/>
<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 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.imooc.o2o.entity.Product"
keyColumn="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=#{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>
</mapper>
补充:mybatis的association以及collection的用法
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.imooc.o2o.dao.ProductImgDao">
<delete id="deleteProductImgByProductId" parameterType="Long">
<!-- 具体的sql -->
DELETE FROM
tb_product_img
WHERE
product_id =
#{productId}
</delete>
</mapper>
三、单元测试
1、ProductDaoTest
package com.imooc.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.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Product;
import com.imooc.o2o.entity.ProductCategory;
import com.imooc.o2o.entity.ProductImg;
import com.imooc.o2o.entity.Shop;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductDaoTest extends BaseTest {
@Autowired ProductDao productDao;
@Autowired ProductImgDao productImgDao;
@Test
@Ignore
public void testCQueryProductByProductId() {
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("图片2");
productImg2.setPriority(1);
productImg2.setCreateTime(new Date());
productImg2.setProductId(productId);
List<ProductImg> productImgList = new ArrayList<ProductImg>();
productImgList.add(productImg1);
productImgList.add(productImg2);
int effectedNum = productImgDao.batchInsertProductImg(productImgList);
assertEquals(2, effectedNum);
//查询productId为1的商品信息并校验返回的详情图实例列表size是否为2
Product product = productDao.queryProductById(productId);
System.out.println(product.getProductImgList().size());
//assertEquals(2, product.getProductImgList().size());
//删除新增的两个商品详情图实例
effectedNum = productImgDao.deleteProductImgByProductId(productId);
System.out.println(effectedNum);
//assertEquals(2, effectedNum);
}
@Test
public void testDUpdateProduct() throws Exception{
Product product = new Product();
ProductCategory pc = new ProductCategory();
Shop shop = new Shop();
shop.setShopId(1L);
pc.setProductCategoryId(3L);
product.setProductId(1L);
product.setShop(shop);
product.setProductName("第二个产品");
product.setProductCategory(pc);
//修改productId为1的商品的名称
//以及商品类别并校验影响的行数是否为1
int effectedNum = productDao.updateProduct(product);
assertEquals(1, effectedNum);
}
}
2、ProductImgDaoTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductImgDaoTest extends BaseTest{
@Autowired
private ProductImgDao productImgDao;
@Test
//@Ignore
public void testCDeleteProductImgByProductId() {
//删除新增的两条商品详情图片记录
long productId = 1;
int effectedNum = productImgDao.deleteProductImgByProductId(productId);
assertEquals(2, effectedNum);
}
}