pageHelper分頁失效問題及解決方案
前言
pageHelper是一款優(yōu)秀的Mybatis分頁插件,在項目中可以非常便利的使用,使開發(fā)效率得到很大的提升,但不支持一對多結(jié)果映射的分頁查詢,所以在平時的使用時,對于一對多分頁會出現(xiàn)分頁錯誤,這篇文章主要對pageHelper分頁錯誤進(jìn)行重現(xiàn)以及提出解決方案。
分析
mybatis進(jìn)行一對多查詢時,映射文件(mapper.xml)中的sql語句中使用的左連接,pageHelper會自動對這條左連接sql語句進(jìn)行select count(0)的處理,并把結(jié)果作為分頁結(jié)構(gòu)的記錄總數(shù),然后自動將limit拼接到sql語句末尾進(jìn)行分頁,由于左連接查詢時,連接條件on條件不唯一(即一對多)時,結(jié)果會產(chǎn)生笛卡爾積,所以經(jīng)過pagehelper插件分頁得到的記錄總數(shù)和分頁結(jié)果并不是預(yù)期的結(jié)果。
數(shù)據(jù)準(zhǔn)備
共兩個表:user、address,用戶id與收貨地址表中userId對應(yīng)。
- 用戶表【user】:11條數(shù)據(jù)

- 收貨地址信息表【address】:4條數(shù)據(jù)

數(shù)據(jù)結(jié)構(gòu)
public class UserDto {
public int id;
public String name;
List<Address> addressList;
}預(yù)期結(jié)果
要求對數(shù)據(jù)進(jìn)行分頁(每頁5條),獲得用戶信息,每個用戶信息帶出對應(yīng)收貨信息, 用戶id為2和3的用戶各有兩條收貨地址信息,其余沒有。
期望結(jié)果如下
{
"code": 200,
"message": "success",
"data": {
"pageNum": 1,
"pageSize": 5,
"pages": 3,
"size": 5,
"total": 11,
"data": [
{
"id": 1,
"name": "張三",
"addressList": []
},
{
"id": 2,
"name": "李四",
"addressList": [
{
"id": 1,
"address": "陜西省寶雞市",
"userId": 2
},
{
"id": 2,
"address": "陜西省延安市",
"userId": 2
}
]
},
{
"id": 3,
"name": "王五",
"addressList": [
{
"id": 3,
"address": "陜西省西安市",
"userId": 3
},
{
"id": 4,
"address": "陜西省漢中市",
"userId": 3
}
]
},
{
"id": 4,
"name": "錢六",
"addressList": []
},
{
"id": 5,
"name": "劉七",
"addressList": []
}
]
}
}問題重現(xiàn)
mybatis映射文件
<resultMap id="list" type="UserDto">
<id property="id" column="id" />
<result property="name" column="name"/>
<collection property="addressList" ofType="Address">
<result property="address" column="address"/>
<result property="userId" column="userId"/>
</collection>
</resultMap>
<select id="findAll" resultMap="list" >
SELECT
a.*,b.address,b.userId
FROM user a
LEFT JOIN address b on a.id=b.userId
</select>然后我們使用pageHelper進(jìn)行分頁,并輸出日志
SELECT count(0) FROM user a LEFT JOIN address b ON a.id = b.userId Preparing: SELECT a.*,b.address,b.userId FROM user a LEFT JOIN address b on a.id=b.userId LIMIT ? Parameters: 5(Integer) Total: 5
日志分析:
第1行:進(jìn)行數(shù)據(jù)總數(shù)的查詢,作為數(shù)據(jù)的總條數(shù)total
第2-4行:進(jìn)行分頁結(jié)果的查詢,查詢出5條數(shù)據(jù)
從日志中可以看出:
1. pageHelper插件拼接后的sql語句就不會輸出正確的結(jié)果,更不會輸出符合期望的結(jié)果
2. pageHelper插件分兩步查詢,第一步查詢出記錄總數(shù),第二步查詢出分頁結(jié)果
解決方案
方案一
思路:先分頁查詢出user表數(shù)據(jù),然后在serviec服務(wù)層根據(jù)用戶id查詢對應(yīng)的收貨地址信息,并關(guān)聯(lián)用戶信息與收貨信息。
service文件
public List<UserDto> findAll(){
List<UserDto> userList=userMapper.findUser();
userList.forEach((item)-> {
item.setAddressList(userMapper.findByUserId(item.id));
});
return userList;
}mybatis映射文件
<select id="findUser" resultType="UserDto">
SELECT * FROM user
</select>
<select id="findByUserId" parameterType="integer" resultType="Address">
SELECT * FROM address where userId=#{userId}
</select>方案二
思路:使用mybatis的嵌套子查詢
<resultMap id="getList" type="UserDto">
<id property="id" column="id" />
<result property="name" column="name"/>
<collection property="addressList" ofType="Address" javaType="List" column="{userId=id}" select="getValueById" >
<id property="id" column="id" />
<result property="address" column="address"/>
<result property="userId" column="userId"/>
</collection>
</resultMap>
<!-- 主查詢 -->
<select id="findAll" resultMap="getList">
select * from user
</select>
<!-- 子查詢 -->
<select id="getValueById" resultType="Address" >
select a.* from address a where a.userId=#{userId}
</select>與嵌套映射結(jié)構(gòu)的resultMap格式基本一致,一對多查詢采用的依舊是collection,區(qū)別在于collection中多了select與column屬性,select用于加載子查詢映射語句的id,它會從column屬性指定的列中檢索數(shù)據(jù),作為參數(shù)傳遞給目標(biāo)select語句即子查詢。
缺點:這種方式雖然可以解決pagehelper一對多分頁的問題,但在大型數(shù)據(jù)表與數(shù)據(jù)集上性能表現(xiàn)不佳,即產(chǎn)生'1+N'問題。
輸出以下sql日志:首先通過主查詢語句獲得主表的數(shù)據(jù)總量作為分頁的total,第二步通過limit獲得前5條分頁數(shù)據(jù)(就是‘1’),第三步將第二步獲得結(jié)果作為參數(shù)通過子查詢獲得地址表的信息(就是‘N’)
Preparing: SELECT count(0) FROM user Parameters: Total: 1 Preparing: select * from user LIMIT ? Parameters: 5(Integer) Preparing: select a.* from address a where a.userId=? Parameters: 1(Integer) Total: 0 Preparing: select a.* from address a where a.userId=? Parameters: 2(Integer) Total: 2 Preparing: select a.* from address a where a.userId=? Parameters: 3(Integer) Total: 2 Preparing: select a.* from address a where a.userId=? Parameters: 4(Integer) Total: 0 Preparing: select a.* from address a where a.userId=? Parameters: 5(Integer) Total: 0
方案三
思路:棄用pageHelper插件,自定義分頁查詢,先對主表(user)進(jìn)行分頁,并把分頁結(jié)果作為虛擬表與副表(address)進(jìn)行左連接查詢
<resultMap id="list" type="UserDto">
<id property="id" column="id" />
<result property="name" column="name"/>
<collection property="addressList" ofType="Address">
<result property="address" column="address"/>
<result property="userId" column="userId"/>
</collection>
</resultMap>
<select id="findAll" resultMap="list" parameterType="integer">
SELECT
a.*,
b.address,
b.userId
FROM
( SELECT * FROM user LIMIT #{size} ) a
LEFT JOIN address b ON a.id = b.userid
</select>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot攔截器執(zhí)行后,Controller層不執(zhí)行的問題及解決
請求攔截器中若調(diào)用getInputStream()讀取流,會導(dǎo)致Controller層無法獲取請求體,引發(fā)400錯誤,解決方案:避免在攔截器中讀取流,或使用緩存(如將流轉(zhuǎn)為字符串)確保數(shù)據(jù)可重復(fù)使用2025-07-07
Spring Boot 2.0.0 終于正式發(fā)布-重大修訂版本
北京時間 2018 年 3 月 1 日早上,如約發(fā)布的 Spring Boot 2.0 在同步至 Maven 倉庫時出現(xiàn)問題,導(dǎo)致在 GitHub 上發(fā)布的 v2.0.0.RELEASE 被撤回2018-03-03
詳解spring cloud Feign使用中遇到的問題總結(jié)
本篇文章主要介紹了詳解spring cloud Feign使用中遇到的問題總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
反射機(jī)制:getDeclaredField和getField的區(qū)別說明
這篇文章主要介紹了反射機(jī)制:getDeclaredField和getField的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot工程啟動順序與自定義監(jiān)聽超詳細(xì)講解
這篇文章主要介紹了SpringBoot工程啟動順序與自定義監(jiān)聽,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11

