導入
クエリをページング財受注を運ぶとき、私たちは、マッパーは、次のように書かれています:
<select id="queryMyOrders" resultMap="myOrdersVO" parameterType="Map">
SELECT
od.id as orderId,
od.created_time as createdTime,
od.pay_method as payMethod,
od.real_pay_amount as realPayAmount,
od.post_amount as postAmount,
os.order_status as orderStatus,
oi.item_id as itemId,
oi.item_name as itemName,
oi.item_img as itemImg,
oi.item_spec_name as itemSpecName,
oi.buy_counts as buyCounts,
oi.price as price
FROM
orders od
LEFT JOIN
order_status os
on od.id = os.order_id
LEFT JOIN
order_items oi
ON od.id = oi.order_id
WHERE
od.user_id = #{paramsMap.userId}
AND
od.is_delete = 0
<if test="paramsMap.orderStatus != null">
and os.order_status = #{paramsMap.orderStatus}
</if>
ORDER BY
od.updated_time ASC
</select>
<resultMap id="myOrdersVO" type="com.imooc.pojo.vo.MyOrdersVO">
<id column="orderId" property="orderId"/>
<result column="createdTime" property="createdTime"/>
<result column="payMethod" property="payMethod"/>
<result column="realPayAmount" property="realPayAmount"/>
<result column="postAmount" property="postAmount"/>
<result column="orderStatus" property="orderStatus"/>
<result column="isComment" property="isComment"/>
<collection property="subOrderItemList"
ofType="com.imooc.pojo.vo.MySubOrderItemVO">
<result column="itemId" property="itemId"/>
<result column="itemName" property="itemName"/>
<result column="itemImg" property="itemImg"/>
<result column="itemSpecId" property="itemSpecId"/>
<result column="itemSpecName" property="itemSpecName"/>
<result column="buyCounts" property="buyCounts"/>
<result column="price" property="price"/>
</collection>
</resultMap>
:上記のマッパーが長い(3は、テーブルをリンクされていた)、私たちは唯一に焦点を当てる必要がありますで登場言い換えると含まれているオブジェクトのプロパティ。<resultMap>
<collection>
MyOrdersVO
List<MySubOrderItemVO>
もちろん、我々はページングツールを使用することはPageHelperです。
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
バックエンドのコードでは、これは次のようになります。
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public PagedGridResult queryMyOrders(String userId,
Integer orderStatus,
Integer page,
Integer pageSize) {
Map<String, Object> map = new HashMap<>();
map.put("userId", userId);
if (orderStatus != null) {
map.put("orderStatus", orderStatus);
}
PageHelper.startPage(page, pageSize);
List<MyOrdersVO> list = ordersMapperCustom.queryMyOrders(map);
return setterPagedGrid(list, page);
}
public PagedGridResult setterPagedGrid(List<?> list, Integer page) {
PageInfo<?> pageList = new PageInfo<>(list);
PagedGridResult grid = new PagedGridResult();
grid.setPage(page);
grid.setRows(list);
grid.setTotal(pageList.getPages());
grid.setRecords(pageList.getTotal());
return grid;
}
ページングが標準であるが、コードの組み合わせは、バグを生成する、上述しました:
- フロントページ番号の内容は正確ではありません。言い換えれば、それが可能になっていた
MyOrdersVO
のpageSizeの番号が、現在MyOrdersVO
のオブジェクトMySubOrderItemVO
のpageSizeでオブジェクトの章の数。
このような状況の理由は、我々は多くのネストされたクエリを使用することです。でGithubの上PageHelper.startPage、プラグイン作者も強調しました。
1. Please do not configure more than one PageHelper
When using Spring, you can config PageHelper by mybatis-config.xml or Spring<bean>. Select one of them, do not configure PageHelper in two ways at the same time.
2. PageHelper does not support paging with for update statement
3. PageHelper does not support Nested Results Mapping
Since the nested result mode causes the resultSet to be folded, the total number of results for the paged query will decrease after folding. So It cannot guarantee the number of paged results correctly.
ネストされたアプローチの結果は、折りたたまれて、結果セットにつながる総数折りたたみ後のクエリ結果のページが軽減されますので、それはページング結果の正確な数を保証することはできませんので、それはです。
ソリューション
- オプション1:2段階のお問い合わせ
最初のステップ:クエリのMyOrdersVO
内容が、その子の順序ではない問い合わせくださいMySubOrderItemVO
コンテンツはフロントヌルに表示されています。
ステップ2:我々は、クエリの子オーダーに来たときに、ユーザーが記事の順序を参照します。
この方法の欠点は、照会要求を高めることです。
- 方法2:使用して属性を
<collection>
select
方法と同様の方法は、実際には、2つの段階に分割され、最初のステップは、クエリのみクエリの注文に3つのテーブルではなく、2つのテーブルリンクリンクを選択します子供のため、ではありません。
<select id="queryMyOrders" resultMap="myOrdersVO" parameterType="Map">
SELECT
od.id as orderId,
od.created_time as createdTime,
od.pay_method as payMethod,
od.real_pay_amount as realPayAmount,
od.post_amount as postAmount,
os.order_status as orderStatus,
od.is_comment as isComment
FROM
orders od
LEFT JOIN
order_status os
on od.id = os.order_id
WHERE
od.user_id = #{paramsMap.userId}
AND
od.is_delete = 0
<if test="paramsMap.orderStatus != null">
and os.order_status = #{paramsMap.orderStatus}
</if>
ORDER BY
od.updated_time ASC
</select>
修正後は、う<resultMap>
お読みください。
<resultMap id="myOrdersVO" type="com.imooc.pojo.vo.MyOrdersVO">
<id column="orderId" property="orderId"/>
<result column="createdTime" property="createdTime"/>
<result column="payMethod" property="payMethod"/>
<result column="realPayAmount" property="realPayAmount"/>
<result column="postAmount" property="postAmount"/>
<result column="orderStatus" property="orderStatus"/>
<result column="isComment" property="isComment"/>
<collection property="subOrderItemList"
select="getSubItems"
column="orderId"
ofType="com.imooc.pojo.vo.MySubOrderItemVO">
<result column="itemId" property="itemId"/>
<result column="itemName" property="itemName"/>
<result column="itemImg" property="itemImg"/>
<result column="itemSpecId" property="itemSpecId"/>
<result column="itemSpecName" property="itemSpecName"/>
<result column="buyCounts" property="buyCounts"/>
<result column="price" property="price"/>
</collection>
</resultMap>
注があることを<collection>
2つの新しいプロパティは、1ですselect
1 column
。
私たちは、クエリに仕上がっqueryMyOrders
それは、それに対応するでしょう、resultMap
との内部subOrderItemList
応じて、このプロパティをselect
、対応するクエリに。クエリは、プロパティ値のクエリになり、私たちは、注文IDがあることを願って<id column="orderId" property="orderId"/>
、クエリの子順に渡されたので、我々は使用column
を指定するプロパティをresultMap
、着信collection
回線を。
そこでここでは書きます<select>
、それに対応するIDを:
<select id="getSubItems" parameterType="String" resultType="com.imooc.pojo.vo.MySubOrderItemVO">
select
oi.item_id as itemId,
oi.item_name as itemName,
oi.item_img as itemImg,
oi.item_spec_name as itemSpecName,
oi.buy_counts as buyCounts,
oi.price as price
from
order_items oi
where
oi.order_id = #{orderId}
</select>
また、これは実際にコンテンツの方法の第2工程であることがわかります。