SpringBoot+MyBatis-Plus實現分頁功能
前言
在SpringBoot項目中,結合MyBatis-Plus(簡稱MP)可以非常方便地實現分頁功能。MP為開發(fā)者提供了分頁插件PaginationInterceptor,只需簡單配置即可使用。
一、配置分頁插件
首先,在SpringBoot的配置類中(通常是帶有@Configuration注解的類)配置PaginationInterceptor。
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); // 根據你的數據庫類型選擇相應的DbType
return interceptor;
}
}
注意:在新版本的MyBatis-Plus中,分頁插件的配置方式有所變化,不再使用PaginationInterceptor,而是使用MybatisPlusInterceptor結合PaginationInnerInterceptor。請根據你的MP版本選擇合適的配置方式。
二、編寫Mapper接口
接下來,在Mapper接口中定義分頁查詢的方法。不需要在Mapper的XML文件中編寫SQL語句來實現分頁,因為分頁插件會自動對SQL進行分頁處理。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import your.package.name.EntityClass;
public interface YourMapper extends BaseMapper<EntityClass> {
// 使用MP提供的分頁功能,不需要編寫具體的分頁SQL
IPage<EntityClass> selectPageList(Page<EntityClass> page, QueryWrapper<EntityClass> queryWrapper);
}
實際上,如果你的EntityClass正確繼承了MP的BaseModel(在新版本中通常不需要繼承),并且你的Mapper繼承了BaseMapper,你甚至不需要在Mapper接口中定義上述的selectPageList方法,因為BaseMapper已經提供了分頁查詢的方法。
三、在服務層調用分頁查詢
在服務層,你可以調用Mapper接口中的分頁查詢方法。首先,你需要創(chuàng)建一個Page對象,并設置當前頁碼和每頁顯示的記錄數。然后,調用Mapper的方法執(zhí)行分頁查詢。
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import your.package.name.EntityClass;
import your.package.name.YourMapper;
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public Page<EntityClass> getPageList(int current, int size) {
// 創(chuàng)建分頁對象,current為當前頁碼,size為每頁顯示的數量
Page<EntityClass> page = new Page<>(current, size);
// 調用Mapper方法進行分頁查詢,這里假設沒有額外的查詢條件,如果有可以使用QueryWrapper添加條件
return yourMapper.selectPage(page, null); // 如果你的Mapper沒有定義selectPageList方法,請使用BaseMapper中的selectPage方法代替
}
}
請注意:如果你的MP版本較新,且你的實體沒有繼承MP的任何類(這是推薦的做法),你應該直接使用BaseMapper中的selectPage方法。此外,如果你的查詢需要額外的條件,可以創(chuàng)建一個QueryWrapper對象來指定這些條件。在上面的示例中,我沒有添加任何條件,所以傳遞了null作為第二個參數。
四、在控制器中調用服務層的分頁方法
最后,在控制器中調用服務層的分頁方法,并將結果返回給前端。通常,前端會期望得到一個包含當前頁碼、總頁數、每頁記錄數和數據列表的響應對象。你可以自定義一個響應類來封裝這些信息,或者直接使用MP提供的Page對象。這里演示后者。
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import your.package.name.EntityClass;
import your.package.name.YourService;
@RestController
@RequestMapping("/your-endpoint")
public class YourController {
@Autowired
private YourService yourService;
@GetMapping("/page") // 分頁查詢的API路徑,例如/your-endpoint/page?current=1&size=10
public Page<EntityClass> getPageList(@RequestParam int current, @RequestParam int size) {
// 調用服務層方法進行分頁查詢,并返回結果給前端
return yourService.getPageList(current, size); // 這里的方法名應與服務層一致
}
}
到此這篇關于SpringBoot+MyBatis-Plus實現分頁功能的文章就介紹到這了,更多相關SpringBoot MyBatis-Plus分頁內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot validation參數校驗實例分析
這篇文章主要介紹了spring boot validation參數校驗,結合實例形式分析了spring boot validation進行數據有效性驗證的相關操作技巧,需要的朋友可以參考下2019-11-11
MybatisPlus 主鍵策略之type=IdType.ASSIGN_ID等詳解
雪花算法(雪花)是微博開源的分布式ID生成算法其核心思想就是:使用一個64位的長型的數字作為全局唯一ID,這篇文章主要介紹了MybatisPlus 主鍵策略(type=IdType.ASSIGN_ID等詳解),需要的朋友可以參考下2024-04-04
Java如何基于EasyExcel實現導入數據校驗并生成錯誤信息Excel
這篇文章主要介紹了Java如何基于EasyExcel實現導入數據校驗并生成錯誤信息Excel,為了優(yōu)化項目中的文件導入功能,考慮構建一個基于EasyExcel的通用Excel導入框架,主要解決導入數據的校驗問題,避免業(yè)務代碼中堆積大量校驗邏輯,需要的朋友可以參考下2024-09-09

