一、常规映射
<!--
一对多映射,根据订单查询订单明细出来
extends:继承resultMap,如果是垮namespace,需要加上namespace名
-->
<resultMap type="orders" id="ordersUserDetailResultMap" extends="ordersUserResultMap">
<!--
映射订单的明细
collection:映射集合对象
property:映射到po的属性名
ofType:映射po的类型
-->
<collection property="orderdetails" ofType="com.mo.pojo.Orderdetail">
<!--
column:字段名
property:po类属性
id:唯一的字段,一般为主键
result:普通字段
-->
<id column="orderdetail_id" property="id"/>
<result column="item_id" property="item_id"/>
<result column="item_num" property="item_num"/>
<result column="item_price" property="item_price"/>
</collection>
</resultMap>
二、子集为MAP。为了让集合中不返回null-集合返回Map。但是Order还是需要单独创建一个vo。因为collection property需要集合字段。
<!--
一对多映射,根据订单查询订单明细出来
-->
<resultMap extends="BaseResultMap" id="appListBaseResultMap" type="com.kf.shop.appVo.OrderApp">
<result column="shop_name" jdbcType="VARCHAR" property="shopName" />
<collection ofType="java.util.HashMap" property="orderGoodsList">
<result column="goods_name" property="goodsName" />
<result column="goods_img" property="goodsImg" />
<result column="goods_num" property="goodsNum" />
<result column="spec_item_key" property="specItemKey" />
<result column="shop_price" property="shopPrice" />
<result column="total_price" property="totalPrice" />
</collection>
</resultMap>
<select id="appList" parameterType="java.util.HashMap" resultMap="appListBaseResultMap">
SELECT
t_order.`id`,t_order.`order_status`,
t_shop.`shop_name`,
t_order_goods.`goods_name`,t_order_goods.`goods_img`,t_order_goods.`goods_num`,t_order_goods.`spec_item_key`,t_order_goods.`shop_price`,t_order_goods.`total_price`
FROM
t_order
INNER JOIN
t_shop ON t_shop.`id` = t_order.`shop_id`
LEFT JOIN
t_order_goods ON t_order_goods.`order_id` = t_order.`id`
WHERE
user_id=#{userId}
</select>
三、查询子集合返回一条数据原因
四、上面的一对多查询好像有分页的问题
解决方式如下:
<resultMap id="appListBaseResultMap" extends="BaseResultMap" type="com.kf.shop.appVo.OrderApp">
<result column="shop_name" jdbcType="VARCHAR" property="shopName" />
<collection property="orderGoodsList" ofType="java.util.HashMap" column="id"
select="com.kf.shop.mapper.OrderGoodsMapper.selectByOrderId" >
</collection>
</resultMap>
<select id="appList" parameterType="java.util.HashMap" resultMap="appListBaseResultMap">
SELECT
t_order.`id`,t_order.`order_status`,
t_shop.`shop_name`
FROM
t_order
INNER JOIN
t_shop ON t_shop.`id` = t_order.`shop_id`
<where>
t_order.`user_id`=#{userId}
AND t_order.`deleted`=0
<if test="orderStatus != null and orderStatus != ''">
AND t_order.`order_status`=#{orderStatus}
</if>
</where>
</select>
<select id="selectByOrderId" parameterType="java.lang.Long" resultType="java.util.HashMap">
select
t_order_goods.`goods_name` as goodsName,
t_order_goods.`goods_img` as goodsImg,
t_order_goods.`goods_num` as goodsNum,
t_order_goods.`spec_item_key` as specItemKey,
t_order_goods.`shop_price` as shopPrice,
t_order_goods.`total_price` as totalPrice,
t_order_goods.`statu`
from
t_order_goods
where
t_order_goods.`order_id`=#{orderId}
</select>