Mybatis一對多對映查詢
阿新 • • 發佈:2018-11-07
一、常規對映
<!-- 一對多對映,根據訂單查詢訂單明細出來 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>
三、查詢子集合返回一條資料原因