欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pageHelper分頁失效問題及解決方案

 更新時(shí)間:2025年01月16日 09:24:03   作者:菜鳥阿都  
文章介紹了Mybatis分頁插件pageHelper的一個(gè)bug,即在一對多查詢時(shí)會(huì)出現(xiàn)分頁錯(cuò)誤,作者提供了解決方案,包括方案一、方案二和方案三,方案一通過在服務(wù)層進(jìn)行額外查詢來解決分頁問題,方案二利用mybatis的嵌套子查詢來解決,但可能產(chǎn)生'1+N'問題

前言

pageHelper是一款優(yōu)秀的Mybatis分頁插件,在項(xiàng)目中可以非常便利的使用,使開發(fā)效率得到很大的提升,但不支持一對多結(jié)果映射的分頁查詢,所以在平時(shí)的使用時(shí),對于一對多分頁會(huì)出現(xiàn)分頁錯(cuò)誤,這篇文章主要對pageHelper分頁錯(cuò)誤進(jìn)行重現(xiàn)以及提出解決方案。

分析

mybatis進(jìn)行一對多查詢時(shí),映射文件(mapper.xml)中的sql語句中使用的左連接,pageHelper會(huì)自動(dòng)對這條左連接sql語句進(jìn)行select count(0)的處理,并把結(jié)果作為分頁結(jié)構(gòu)的記錄總數(shù),然后自動(dòng)將limit拼接到sql語句末尾進(jìn)行分頁,由于左連接查詢時(shí),連接條件on條件不唯一(即一對多)時(shí),結(jié)果會(huì)產(chǎn)生笛卡爾積,所以經(jīng)過pagehelper插件分頁得到的記錄總數(shù)和分頁結(jié)果并不是預(yù)期的結(jié)果。

數(shù)據(jù)準(zhǔn)備

共兩個(gè)表: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條),獲得用戶信息,每個(gè)用戶信息帶出對應(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語句就不會(huì)輸出正確的結(jié)果,更不會(huì)輸出符合期望的結(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,它會(huì)從column屬性指定的列中檢索數(shù)據(jù),作為參數(shù)傳遞給目標(biāo)select語句即子查詢。

缺點(diǎn):這種方式雖然可以解決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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Struts2文件上傳與自定義攔截器

    關(guān)于Struts2文件上傳與自定義攔截器

    本篇文章,小編將為大家介紹關(guān)于Struts2文件上傳與自定義攔截器,有需要的朋友可以參考一下
    2013-04-04
  • Java實(shí)現(xiàn)簡易學(xué)生管理系統(tǒng)

    Java實(shí)現(xiàn)簡易學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼

    Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼

    MinIO?是一款基于Go語言的高性能對象存儲(chǔ)服務(wù),本文主要介紹了Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Spring @Conditional注解從源碼層講解

    Spring @Conditional注解從源碼層講解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean,這篇文章主要介紹了Spring @Conditional注解示例詳細(xì)講解,需要的朋友可以參考下
    2023-01-01
  • 快速了解Spring Boot

    快速了解Spring Boot

    這篇文章主要介紹了快速了解Spring Boot,介紹了其環(huán)境準(zhǔn)備,URL中的變量以及模板渲染等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • java獲取當(dāng)前時(shí)間戳的方法

    java獲取當(dāng)前時(shí)間戳的方法

    本文主要介紹了java獲取當(dāng)前時(shí)間戳的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作

    mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作

    這篇文章主要介紹了mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java進(jìn)階:Struts多模塊的技巧

    Java進(jìn)階:Struts多模塊的技巧

    Java進(jìn)階:Struts多模塊的技巧...
    2006-12-12
  • 詳解Spring Boot 項(xiàng)目中的 parent

    詳解Spring Boot 項(xiàng)目中的 parent

    這篇文章主要介紹了Spring Boot中parent作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • springcloud之Feign、ribbon如何設(shè)置超時(shí)時(shí)間和重試機(jī)制

    springcloud之Feign、ribbon如何設(shè)置超時(shí)時(shí)間和重試機(jī)制

    這篇文章主要介紹了springcloud之Feign、ribbon如何設(shè)置超時(shí)時(shí)間和重試機(jī)制,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論