Mybatis子查询(解决pagehelper左联分页查询数据,分页数据量有误)

首先定义resultMap

    <resultMap id="orderDetailMap" type="com.mall.domain.OmsOrderDetail"
               extends="com.mall.mapper.OmsOrderMapper.BaseResultMap">
        <collection property="orderItemList" select="selectOrderItem"  column="{orderId = id}" ofType="com.mall.model.OmsOrderItem"
                    javaType="java.util.List">
        </collection>
    </resultMap>

然后主查询

    <select id="getOrderList" resultMap="orderDetailMap">
        SELECT
            *
        FROM
            oms_order
        WHERE
            1 = 1
            <if test="status != -1">
                and status = #{status}
            </if>
            and delete_status = 0
            and member_id = #{memberId}
            order by create_time desc
    </select>

最后子查询

    <select id="selectOrderItem" resultMap="com.mall.mapper.OmsOrderItemMapper.BaseResultMap">
        select
            *
        from oms_order_item
        where order_id = #{orderId};
    </select>

注意:1.子查询方法dao曾代码也要写,否则编译报错;

           2.子查询传参在collection里column="{orderId = id}",这里的orderId是传到子查询中的变量名,id为主查询中的字段名,                  要和数据库表中字段名一致,而不是和model层对象变量一致;

 

上一篇:阻止Bootstrap 模态框(Modal)点击空白处时关闭


下一篇:MySql多列转行