public class Goods {
private Integer goodsId; // 商品编号
private String title; // 标题
private String subTitle; // 子标题
private Float originalCost; // 原始价格
private Float currentPrice; // 当前价格
private Float discount; // 折扣率
private Integer isFreeDelivery; // 是否包邮,1-包邮 0-不包邮
private Integer categoryId; // 分类编号
private List<GoodsDetail> goodsDetailList; // 商品详情
private List<GoodsCover> goodsCoverList; // 商品封面
private List<GoodsParam> goodsParamList; // 商品参数
}
goods.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="goods">
<resultMap id="rmGoodsHaveMany" type="com.imooc.mybatis.entity.Goods">
<result property="goodsId" column="goods_id"/>
<collection property="goodsDetailList" select="goodsDetail.selectByGoodsId" column="goods_id"/>
<collection property="goodsCoverList" select="goodsCover.selectByGoodsId" column="goods_id"/>
<collection property="goodsParamList" select="goodsParam.selectByGoodsId" column="goods_id"/>
</resultMap>
<select id="selectGoodsHaveMany" resultMap="rmGoodsHaveMany">
select * from t_goods limit 0,5
</select>
<resultMap id="rmGoodsDTOHaveMany" type="com.imooc.mybatis.entity.Goods">
<result property="goods.goodsId" column="goods_id"/>
<result property="goods.title" column="title"/>
<result property="goods.subTitle" column="sub_title"/>
<result property="goods.originalCost" column="original_cost"/>
<result property="goods.currentPrice" column="current_price"/>
<result property="goods.discount" column="discount"/>
<result property="goods.isFreeDelivery" column="is_free_delivery"/>
<result property="goods.categoryId" column="category_id"/>
???怎么写?goodsDetailList
???怎么写?goodsCoverList
???怎么写?goodsParamList
<result property="category.categoryId" column="category_id"/>
<result property="category.categoryName" column="category_name"/>
<result property="category.parentId" column="parent_id"/>
<result property="category.categoryLevel" column="category_level"/>
<result property="category.categoryOrder" column="category_order"/>
</resultMap>
<select id="selectGoodsDTOHaveMany" resultMap="rmGoodsDTOHaveMany">
select g.*,c.* from t_goods as g join t_category c on g.category_id = c.category_id
order by g.goods_id limit 0,20
</select>
</mapper>
goods-detail.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="goodsDetail">
<select id="selectByGoodsId" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsDetail">
select * from t_goods_detail where goods_id = #{value}
</select>
</mapper>
goods-cover.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="goodsCover">
<select id="selectByGoodsId" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsCover">
select * from t_goods_cover where goods_id=#{value}
</select>
</mapper>
goods-param.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="goodsParam">
<select id="selectByGoodsId" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsParam">
select * from t_goods_param where goods_id=#{value}
</select>
</mapper>
1、在goods.xml文件中执行商品查询,因为是单表查询,无需DTO,建立1对多的映射,执行没有问题【id="selectGoodsHaveMany"】
2、在goods.xml文件中执行商品表和分类表关联查询,结果集用DTO保存,而DTO中的商品又需要级联查询,此时,映射有问题【id="selectGoodsDTOHaveMany"】
3、上述代码应该可以复制粘贴运行,我在goods.xml文件中标注了有三行???,请老师帮忙看看那三行?处,要如何获取这3个复合属性?
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星