嵌套查询,嵌套结果

嵌套查询,嵌套结果

    <!--
    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 //让该方法跳过单元测试

posted @ 2019-02-20 10:51  fly_bk  阅读(4338)  评论(0编辑  收藏  举报