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