springboot整合pegehelper不起作用
1、导入jar包
不要使用1.4.1版本,没作用(好多低版本都不行,1.4.6可以)
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
2、配置yaml
# 设置数据源(必要)
spring:
datasource:
url: jdbc:mysql://localhost:3306/db
username: root
password: "root"
type: com.zaxxer.hikari.HikariDataSource
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
# 整合mybatis(必要)
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.pagehelper.pojo
configuration:
# 开启驼峰映射
map-underscore-to-camel-case: true
# pagehelper配置(不配置一样可以用)
pagehelper:
helperDialect: mysql
reasonable: true
params: count=countSql
support-methods-arguments: true
3、创建测试类
@Test
public void test1() throws Exception{
//startPage(页面值,每页显示条数);
PageHelper.startPage(2,3);//必须位于获取数据前
List<Category> categories = categoryMapper.queryCategoryList();
PageInfo<Category> pageInfo = new PageInfo(categories);
//总页数
int pages = pageInfo.getPages();
//每页显示条数
int pageSize = pageInfo.getPageSize();
//总条数
long total = pageInfo.getTotal();
//获取分页后的数据
List list = pageInfo.getList();
for (Object o : list) {
System.out.println(o);
}
}
总结
1、不要使用pagehelper包,要使用springboot的pagehelper-spring-boot-starter包
2、测试代码中的PageHelper.startPage(页码值,每页显示条数);必须在获取数据前设置(可以直接写在方法的第一行)
3、如果代码没问题,还是不能使用,切换jar包版本,多切换几个(1.4.6现在是能用的)