springboot 集成pagehelper

本文介绍如何在SpringBoot项目中快速集成PageHelper实现分页功能。通过添加依赖及配置分页参数,即可轻松完成集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在springboot 中集成pagehelper 很简单,只需两步即可.
一、添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version> <请不要用1.0.0 该版本不支持拦截器插件,选用最新版即可>
</dependency>
查看maven 依赖包 可发现 该包自动依赖分页包 、mybatis包


二、配置分页参数
有两种方式:
1.在application.yml或properties文件中配置。
#pagehelper分页插件配置 此处是yml书写格式
pagehelper:
autoDialect:true
reasonable:true
supportMethodsArguments:true
params:count=countSql
2.采用javaConfig配置
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
 final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 
sqlSessionFactoryBean.setDataSource(dataSource); 
//配置分页插件
 PageHelper pageHelper = new PageHelper(); 
Properties properties = new Properties(); 
properties.setProperty("reasonable", "true"); 
properties.setProperty("supportMethodsArguments", "true");
 properties.setProperty("returnPageInfo", "check"); 
properties.setProperty("params", "count=countSql");
 pageHelper.setProperties(properties); 

 sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper}); 
 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/*.xml")); 
return sqlSessionFactoryBean.getObject(); 
}
如此配置就可使用分页功能,分页注意事项:
在pageHelper.startpage方法后的第一条查询方法才会被分页
带有for update 行锁的语句,不要使用分页插件,会抛异常



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值