嵌套查詢,嵌套結果
<!--
association:將關聯查詢信息映射到一個pojo對象中。
collection:將關聯查詢信息映射到一個list集合中。
-->
<!--嵌套結果 只查一次-->
<resultMap id="ordersResultMap" type="Orders">
<id property="id" column="id"/>
<result property="user_id" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<!--<association property="user" javaType="User">-->
<!--<id property="id" column="user_id"/>-->
<!--<result property="username" column="username"/>-->
<!--<result property="address" column="address"/>-->
<!--</association>-->
<!--嵌套查詢 查2次-->
<association property="user" select="mapper.UserMapper.mySelectByPrimary2" column="user_id">
</association>
</resultMap>
<select id="selectByPrimary2" resultMap="ordersResultMap">
select o.*, u.username, u.address
from orders o,
user u
where o.user_id = u.id
and o.id = #{id}
</select>
多對多
<resultMap id="ordersResultMap4" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<collection property="ordersList" ofType="Orders">
<id property="id" column="order_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<collection property="orderdetails" ofType="Orderdetail">
<id property="id" column="od_id"/>
<result property="items_id" column="items_id"/>
<result property="items_num" column="items_num"/>
<association property="items" javaType="Items">
<id property="id" column="i_id"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
<result property="detail" column="detail"/>
</association>
</collection>
</collection>
</resultMap>
<select id="selectByPrimary4" resultMap="ordersResultMap4">
SELECT u.id,
u.username,
u.address,
o.id order_id,
o.number,
o.createtime,
o.note,
od.id od_id,
od.items_id,
od.items_num,
it.id i_id,
it.name,
it.price,
it.detail
FROM user u,
orders o,
orderdetail od,
items it
WHERE o.user_id = u.id
AND o.id = od.orders_id
AND od.items_id = it.id;
</select>
N+1 問題
當使用嵌套查詢,只想要1個結果,結果查到此對象的其他信息
解決方法使用嵌套結果,或使用懶加載緩解
@Ignore 補充
@Ignore //讓該方法跳過單元測試