1. 程式人生 > 程式設計 >MyBatis Plus 實現多表分頁查詢功能的示例程式碼

MyBatis Plus 實現多表分頁查詢功能的示例程式碼

在Mybatis Plus 中,雖然IService 介面幫我們定義了很多常用的方法,但這些都是 T 物件有用,如果涉及到 多表的查詢,還是需要自定義Vo 物件和自己編寫sql 語句,Mybatis Plus提供了一個Page 物件,查詢是需要設定其中的 size 欄位 和 current 欄位的值

一、分頁配置

可以直接使用selectPage這樣的分頁,但返回的資料確實是分頁後的資料,但在控制檯列印的SQL語句其實並沒有真正的物理分頁,而是通過快取來獲得全部資料中再進行的分頁,這樣對於大資料量操作時是不可取的,那麼接下來就敘述一下,真正實現物理分頁的方法。
官方在分頁外掛上如是描述:自定義查詢語句分頁(自己寫sql/mapper),也就是針對自己在Mapper中寫的方法,但經過測試,如果不配置分頁外掛,其預設採用的分頁為RowBounds的分頁即邏輯分頁,也就是先把資料記錄全部查詢出來,然在再根據offset和limit截斷記錄返回(資料量大的時候會造成記憶體溢位),故而不可取,而通過分頁外掛的配置即可達到物理分頁效果。

新建一個MybatisPlusConfig配置類檔案,程式碼如下所示:

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisPlusConfig {
 
  /**
   * mybatis-plus分頁外掛<br>
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    return paginationInterceptor;
  }
}

二、使用分頁進行單表的查詢

對於單表的分頁查詢,ServiceImpl 類已經為我們提供了對應的方法 selectPage(),並將結果封裝到Page 物件中:

在這裡插入圖片描述

在專案開發當中,都會將分頁的一些引數封裝成一個類 PageReq(不要在意這個Req 為什麼不是全大寫)->import java.io.Serializable;

public class PageReq implements Serializable {

  /**
   * 每頁顯示大小
   */
  private long size;

  /**
   * 當前頁碼
   */
  private long current;

  /**
   * 最大頁數
   */
  private long maxCurrent;

  /**
   * 資料總條數
   */
  private long total;

  public long getSize() {
    return size;
  }

  public void setSize(long size) {
    this.size = size;
  }

  public long getCurrent() {
    return current;
  }

  public void setCurrent(long current) {
    this.current = current;
  }

  public long getMaxCurrent() {
    return maxCurrent;
  }

  public void setMaxCurrent(long maxCurrent) {
    this.maxCurrent = maxCurrent;
  }

  public long getTotal() {
    return total;
  }

  public void setTotal(long total) {
    if(size != 0){
      if(total % size != 0){
        maxCurrent = total / size + 1;
      }else {
        maxCurrent = total / size;
      }
    }
  }

  public PageReq() {

  }

  public PageReq(long size,long current,long total) {
    this.size = size;
    this.current = current;
    this.total = total;
    setTotal(total);
  }
}

功能編寫:

在這裡插入圖片描述
在這裡插入圖片描述

執行完之後,會將查詢的介面封裝到我們 Page的 物件中:

在這裡插入圖片描述

三、多表關聯分頁查詢

對於多表關聯的查詢時,還是需要編寫 VO 類和 手動的在Mapper.xml 中編寫sql,雖然是可以不用建立VO,用Map 的方式接受返回的結果,但這樣只會更麻煩,甚至VO 是很有可能在其他地方使用的
先準備個VO類:

在這裡插入圖片描述

編寫Mapper介面,新增一個分頁查詢的方法
package com.eiot.e_view.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.eiot.e_view.model.req.RoomPageReq;
import com.eiot.e_view.model.vo.RoomVO;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface RoomMapper extends BaseMapper<Room> {

  List<RoomVO> getRoomPageList(Page page,@Param("roomPageReq")RoomPageReq roomPageReq);
}

編寫sql,和我們使用Mybatis 沒有區別:

在這裡插入圖片描述
在這裡插入圖片描述

編寫Server :

在這裡插入圖片描述

執行結果:

在這裡插入圖片描述

總結

到此這篇關於MyBatis Plus 實現多表分頁查詢功能的示例程式碼的文章就介紹到這了,更多相關MyBatis Plus 多表分頁查詢內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!