springBoot使用mybatis-plus插件实现分页过程
作者:水悠悠予安
时间:2026-07-12
浏览:0
分页功能几乎是后端开发中的标配。Mybatis-Plus 提供的分页插件用起来非常顺手,配置简单、逻辑清晰。下面直接来看完整的实现过程,从项目结构到代码落地,一步到位。 一、项目结构 二、pom.xml 依赖添加 (这里添加到 krystal_dao 模块的 pom.xml 中;单模块项目直接加在根
分页功能几乎是后端开发中的标配。Mybatis-Plus 提供的分页插件用起来非常顺手,配置简单、逻辑清晰。下面直接来看完整的实现过程,从项目结构到代码落地,一步到位。
一、项目结构

二、pom.xml 依赖添加
(这里添加到 krystal_dao 模块的 pom.xml 中;单模块项目直接加在根 pom.xml,多模块项目则根据实际情况放到对应模块的配置文件里。)
com.baomidou mybatis-plus 3.0-beta
三、新建 MyBatisPlusConfig.ja va
(建议把配置类放在专门的功能模块里,所需的依赖按编译报错提示逐个添加即可。)

package com.dm.krystal.core.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author krystal
*/
@Configuration
/*@MapperScan("com.dm.krystal")*/ /* KrystalApiApplication启动类上已写,此处不再重复 */
public class MyBatisPlusConfig {
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
四、具体代码实现
1. Controller
IPagepageData = examinationPaperRelevanceService.getExaminationPaperPageByQueryVO(page, examinationPaperQueryVO);
2. Service 接口
IPagegetExaminationPaperPageByQueryVO(Page page, ExaminationPaperQueryVO examinationPaperQueryVO);
3. Service 实现类
@Override public IPagegetExaminationPaperPageByQueryVO(Page page, ExaminationPaperQueryVO examinationPaperQueryVO) { return this.baseMapper.getExaminationPaperPageByQueryVO(page, examinationPaperQueryVO); }
4. Mapper (DAO 接口)
IPagegetExaminationPaperPageByQueryVO(Page page, @Param("examinationPaperQueryVO") ExaminationPaperQueryVO examinationPaperQueryVO);
5. Mapper XML
五、SQL 打印

执行结果:

六、自定义分页(已有100条数据,每次截取十条)
IPagepageData = PageUtil.listToPage(paperList, page.getCurrent(), page.getSize());
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import ja va.util.ArrayList;
import ja va.util.List;
import ja va.util.concurrent.atomic.LongAccumulator;
@Slf4j
public class PageUtil {
public static IPage listToPage(List list, long pageNum, long pageSize){
IPage iPage = new Page<>(pageNum, pageSize);
iPage.setTotal(list.size());
int startIndex = (int)((pageNum - 1) * pageSize);
if (null == list || list.isEmpty() || startIndex > list.size()) {
iPage.setRecords(new ArrayList<>());
} else {
int toIndex = (int)(pageNum * pageSize);
iPage.setRecords(list.subList(startIndex, toIndex > list.size() ? list.size() : toIndex));
}
return iPage;
}
}
总结
以上就是利用 Mybatis-Plus 分页插件在 SpringBoot 中实现分页的完整流程,从依赖配置、插件注册到业务代码的编写,以及针对内存列表的自定义分页工具类,都一并覆盖了。按此步骤操作,可以快速给项目加上稳定高效的分页能力。
作者最新文章
photoshop通道和蒙版在哪
2026-10-03 12:15
苹果折叠屏iPhone预计售价是多少
2026-09-14 13:44
OpenAI GPT-6 Astra 自主通关《传送门》:技术原理与实验成本解析
2026-09-08 19:08
苹果与铠侠签署NAND长期供应协议:3-5年长约与不设价格上限背后的供应链战略
2026-09-08 16:58
PDF转PPT操作指南:在线、本地与批量转换及结果核对
2026-09-04 15:04
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































