Mybatis一对多映射查询

一、常规映射 

	
	<!-- 	
		一对多映射,根据订单查询订单明细出来
		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>

 三、查询子集合返回一条数据原因

猜你喜欢

转载自blog.csdn.net/Lei_Da_Gou/article/details/83651508