pageHelper分頁失效解決方案
前言
pageHelper是一款優秀的Mybatis分頁外掛,在專案中可以非常便利的使用,使開發效率得到很大的提升,但不支援一對多結果對映的分頁查詢,所以在平時的使用時,對於一對多分頁會出現分頁錯誤,這篇文章主要對pageHelper分頁錯誤進行重現以及提出解決方案。
分析
mybatis進行一對多查詢時,對映檔案(mapper.xml)中的sql語句中使用的左連線,pageHelper會自動對這條左連線sql語句進行select count(0)的處理,並把結果作為分頁結構的記錄總數,然後自動將limit拼接到sql語句末尾進行分頁,由於左連線查詢時,連線條件on條件不唯一(即一對多)時,結果會產生笛卡爾積,所以經過pagehelper外掛分頁得到的記錄總數和分頁結果並不是預期的結果。
資料準備
共兩個表:user、address,使用者id與收貨地址表中userId對應。
使用者表【user】:11條資料
收貨地址資訊表【address】:4條資料
資料結構
public class UserDto {
public int id;
public String name;
List<Address> addressList;
}
預期結果
要求對資料進行分頁(每頁5條),獲得使用者資訊,每個使用者資訊帶出對應收貨資訊, 使用者id為2和3的使用者各有兩條收貨地址資訊,其餘沒有。期望結果如下
{ "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": [] } ] } }
問題重現
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進行分頁,並輸出日誌
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行:進行資料總數的查詢,作為資料的總條數total
第2-4行:進行分頁結果的查詢,查詢出5條資料
從日誌中可以看出
1. pageHelper外掛拼接後的sql語句就不會輸出正確的結果,更不會輸出符合期望的結果
2. pageHelper外掛分兩步查詢,第一步查詢出記錄總數,第二步查詢出分頁結果
解決方案
方案一
思路:先分頁查詢出user表資料,然後在serviec服務層根據使用者id查詢對應的收貨地址資訊,並關聯使用者資訊與收貨資訊。
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>
與巢狀對映結構的resultMap格式基本一致,一對多查詢採用的依舊是collection,區別在於collection中多了select與column屬性,select用於載入子查詢對映語句的id,它會從column屬性指定的列中檢索資料,作為引數傳遞給目標select語句即子查詢。
缺點:這種方式雖然可以解決pagehelper一對多分頁的問題,但在大型資料表與資料集上效能表現不佳,即產生'1+N'問題。
輸出以下sql日誌:首先通過主查詢語句獲得主表的資料總量作為分頁的total,第二步通過limit獲得前5條分頁資料(就是‘1’),第三步將第二步獲得結果作為引數通過子查詢獲得地址表的資訊(就是‘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)進行分頁,並把分頁結果作為虛擬表與副表(address)進行左連線查詢
<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>