SpringBoot+MyBatis-Plus實現(xiàn)分頁功能
前言
在SpringBoot項目中,結合MyBatis-Plus(簡稱MP)可以非常方便地實現(xiàn)分頁功能。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)); // 根據(jù)你的數(shù)據(jù)庫類型選擇相應的DbType
return interceptor;
}
}
注意:在新版本的MyBatis-Plus中,分頁插件的配置方式有所變化,不再使用PaginationInterceptor,而是使用MybatisPlusInterceptor結合PaginationInnerInterceptor。請根據(jù)你的MP版本選擇合適的配置方式。
二、編寫Mapper接口
接下來,在Mapper接口中定義分頁查詢的方法。不需要在Mapper的XML文件中編寫SQL語句來實現(xiàn)分頁,因為分頁插件會自動對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已經(jīng)提供了分頁查詢的方法。
三、在服務層調(diào)用分頁查詢
在服務層,你可以調(diào)用Mapper接口中的分頁查詢方法。首先,你需要創(chuàng)建一個Page對象,并設置當前頁碼和每頁顯示的記錄數(shù)。然后,調(diào)用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為每頁顯示的數(shù)量
Page<EntityClass> page = new Page<>(current, size);
// 調(diào)用Mapper方法進行分頁查詢,這里假設沒有額外的查詢條件,如果有可以使用QueryWrapper添加條件
return yourMapper.selectPage(page, null); // 如果你的Mapper沒有定義selectPageList方法,請使用BaseMapper中的selectPage方法代替
}
}
請注意:如果你的MP版本較新,且你的實體沒有繼承MP的任何類(這是推薦的做法),你應該直接使用BaseMapper中的selectPage方法。此外,如果你的查詢需要額外的條件,可以創(chuàng)建一個QueryWrapper對象來指定這些條件。在上面的示例中,我沒有添加任何條件,所以傳遞了null作為第二個參數(shù)。
四、在控制器中調(diào)用服務層的分頁方法
最后,在控制器中調(diào)用服務層的分頁方法,并將結果返回給前端。通常,前端會期望得到一個包含當前頁碼、總頁數(shù)、每頁記錄數(shù)和數(shù)據(jù)列表的響應對象。你可以自定義一個響應類來封裝這些信息,或者直接使用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) {
// 調(diào)用服務層方法進行分頁查詢,并返回結果給前端
return yourService.getPageList(current, size); // 這里的方法名應與服務層一致
}
}
到此這篇關于SpringBoot+MyBatis-Plus實現(xiàn)分頁功能的文章就介紹到這了,更多相關SpringBoot MyBatis-Plus分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Spring Boot中實現(xiàn)文件上傳與管理的操作
在 Spring Boot 中實現(xiàn)文件上傳與管理非常簡單,通過配置文件上傳、創(chuàng)建文件上傳、下載、列表和刪除接口,我們可以輕松地處理文件操作,結合前端頁面,可以提供一個完整的文件管理系統(tǒng),這篇文章主要介紹了在Spring Boot中實現(xiàn)文件上傳與管理,需要的朋友可以參考下2024-07-07
springboot serverEndpoint導致@resource注解不生效
在SpringBoot中,@Resource注解用于注入依賴,本文主要介紹了springboot serverEndpoint導致@resource注解不生效,具有一定的參考價值,感興趣的可以了解一下2023-12-12
java使用MulticastSocket實現(xiàn)多點廣播
這篇文章主要為大家詳細介紹了java使用MulticastSocket實現(xiàn)多點廣播,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
java讀取配置文件(properties)的時候,unicode碼轉utf-8方式
這篇文章主要介紹了java讀取配置文件(properties)的時候,unicode碼轉utf-8方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
java利用Tabula實現(xiàn)對PDF內(nèi)表格數(shù)據(jù)提取
Tabula是一個開源工具,用于從PDF文檔中提取表格數(shù)據(jù),下面小編就來和大家詳細介紹一下java如何通過Tabula對PDF文件內(nèi)表格進行數(shù)據(jù)提取吧2023-09-09

