MyBatis-PageHelper分页插件使用指南
一、项目介绍
MyBatis-PageHelper是一款优秀的MyBatis分页插件,它能够以极简的方式实现MyBatis物理分页功能。该插件支持多种数据库,包括MySQL、Oracle、PostgreSQL等主流数据库,并且可以与Spring、Spring Boot等框架无缝集成。
二、安装配置
1. 依赖引入
Maven项目
在pom.xml中添加以下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本号</version>
</dependency>
Spring Boot项目
使用starter可以简化配置:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>最新版本号</version>
</dependency>
2. 配置方式
MyBatis原生配置
在mybatis-config.xml中添加插件配置:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 参数配置 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
Spring配置
在Spring的applicationContext.xml中配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
param1=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
Spring Boot配置
Spring Boot项目支持properties或yaml格式的配置:
# application.properties
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
三、核心功能使用
1. 基本分页查询
// 第一种方式:使用PageHelper.startPage方法
PageHelper.startPage(1, 10); // 第1页,每页10条
List<User> users = userMapper.selectAll();
// 第二种方式:使用PageHelper.offsetPage方法
PageHelper.offsetPage(0, 10); // 从第0条开始,取10条
List<User> users = userMapper.selectAll();
2. 获取分页信息
PageHelper.startPage(1, 10);
List<User> users = userMapper.selectAll();
// 方式一:使用PageInfo包装结果
PageInfo<User> pageInfo = new PageInfo<>(users);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("总页数:" + pageInfo.getPages());
// 方式二:直接使用Page对象
Page<User> page = (Page<User>) users;
System.out.println("当前页:" + page.getPageNum());
System.out.println("每页数量:" + page.getPageSize());
3. 复杂查询分页
// 带条件的分页查询
PageHelper.startPage(2, 5, "id desc"); // 第2页,每页5条,按id降序
Example example = new Example(User.class);
example.createCriteria().andLike("name", "%张%");
List<User> users = userMapper.selectByExample(example);
四、高级特性
1. 多数据源支持
通过配置autoRuntimeDialect=true可以实现运行时自动识别不同数据源的方言:
pagehelper.autoRuntimeDialect=true
pagehelper.closeConn=false
2. 安全调用检查
开启debug模式可以检查不安全的分页调用:
pagehelper.debug=true
3. 自定义分页逻辑
实现Dialect接口可以自定义分页逻辑:
public class CustomDialect extends AbstractHelperDialect {
// 实现自定义分页逻辑
}
然后在配置中指定:
pagehelper.dialect=com.example.CustomDialect
五、最佳实践
-
合理设置reasonable参数:当设置为true时,会自动修正不合法的页码(如负数会返回第一页,超过总页数会返回最后一页)
-
使用PageInfo简化分页信息获取:PageInfo提供了丰富的分页信息,包括页码导航等
-
注意分页顺序:PageHelper.startPage()必须在查询方法调用前执行
-
复杂SQL处理:对于包含子查询、UNION等复杂SQL,建议使用
/*keep orderby*/注解保留排序 -
性能优化:对于大数据量分页,建议使用
countColumn指定优化count查询
六、常见问题
-
分页失效:检查是否在查询方法前调用了startPage方法
-
总记录数不准确:检查SQL是否包含GROUP BY等影响行数的操作
-
排序失效:检查是否在PageHelper.startPage()中指定了排序条件
-
多数据源问题:确保正确配置了autoRuntimeDialect和closeConn参数
通过合理配置和使用MyBatis-PageHelper,可以大大简化分页功能的开发,提高开发效率和代码可维护性。
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00