1. 程式人生 > >mysql一對多關聯查詢分頁錯誤解決

mysql一對多關聯查詢分頁錯誤解決

xml問價中查詢資料中包含list,需要使用collection

<resultMap id="XX" type="com.XXX.XXXX">
        <id column="o_id" jdbcType="BIGINT" property="id" />
        <result column="o_user_id" jdbcType="BIGINT" property="userId" />
        ....
        <collection property="orderProductList" ofType="com.XXXXXX.XXXXX">
            <id column="p_id" jdbcType="BIGINT" property="id" />
            <result column="p_order_id" jdbcType="BIGINT" property="orderId" />
            ....
        </collection>
    </resultMap>

這樣的查詢系統封裝的通用分頁查詢是不對的,所以需要自己sql中加入分頁解決

<select id="XXX" resultMap="OrderListMap">
        SELECT
        you.nick_name,
        yo.id o_id,
        yo.user_id o_user_id
        FROM
        (
        SELECT * FROM
        youpin_order
        WHERE
        1 = 1
        <if test="status != null">
            and `status` = #{status}
        </if>
        <if test="page != null and limit != null">
        LIMIT #{page},
        #{limit}
        </if>
        ) yo
        LEFT JOIN XXX yop ON yo.id = yop.order_id
        LEFT JOIN XXXX you ON yo.user_id = you.id
    </select>

傳入引數的時候需要計算

(offset - 1) * limit, limit