PageHelper 插件是真的很方便,特别方便,很是方便。尽管手撸一个分页也不难,用一下limit,order 之类的SQL便可。但有简便的怎么能不用呢。java
这儿就介绍一种简便的方法就好了,其余不少种不少种方法请本身查看官方文档,文档里面也有实例。git
我以前在讲 mybatis 的博客中,专门提了这个组件的做者。 在Springboot里面使用,做者专门集成了springboot 使用的Maven。添加便可使用。github
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
复制代码
最新版本查看 githubspring
这儿若是在PageHelper 的官方页面上直接使用上面的Maven的话,会出现一个bug。json
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
复制代码
分页会出现问题,分页没有效果,会把全部结果都返回出来。若是你是用的Springboot的话,必定要用Spring boot集成的版本Maven。springboot
Mapper中直接将List换成Page。 mybatis
public PageInfo<User> findAllConsumers(int pageNum, int pageSize){
//静态方法直接引用pageNum:页数 pageSize:每页多少数量
PageHelper.startPage(pageNum,pageSize);
PageInfo<User> pageInfo = new PageInfo<>(userMapper.selectAllUser());
return pageInfo;
}
复制代码
是否是很简单 app
{
"total":18,
"list":[
{
"consumerId":1234,
"consumerName":"张三",
"consumerPassword":"1234"
},
{
"consumerId":17069109987,
"consumerName":"思考",
"consumerPassword":"234lk"
},
{
"consumerId":17069130001,
"consumerName":"李四",
"consumerPassword":"1234234"
},
{
"consumerId":17069130011,
"consumerName":"张三",
"consumerPassword":"1234"
},
{
"consumerId":17069130012,
"consumerName":"张三",
"consumerPassword":"1234"
}
],
"pageNum":1,
"pageSize":5,
"size":5,
"startRow":1,
"endRow":5,
"pages":4,
"prePage":0,
"nextPage":2,
"isFirstPage":true,
"isLastPage":false,
"hasPreviousPage":false,
"hasNextPage":true,
"navigatePages":8,
"navigatepageNums":[
1,
2,
3,
4
],
"navigateFirstPage":1,
"navigateLastPage":4
}
复制代码
PageHelper yml中配置的话参考官方文档的参数介绍。不过基本不用设置。spring-boot