1.导入相关依赖
这个是springboot的分页依赖插件,而spring的分页插件并不是这个依赖这里需要注意。
<!--pageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2.application.yml配置相关信息
#pagehelper 分页插件
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
这里提一下,当你准备运行程序时,可能会报错如下图
这时,你需要在yml配置文件中添加
main:
allow-circular-references: true
我的配置如下
3.去controller层完成分页(假分页)测试
首先我的数据库表数据如下
其次我的controller代码如下
package com.ty.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.bean.User;
import com.ty.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class Controller {
@Autowired
private UserService userService;
/*
* pageHelper分页*/
@RequestMapping("/showAll")
public PageInfo<User> show(@RequestParam(defaultValue = "1") Integer pageNum){
PageHelper.startPage(pageNum,3);
List<User> users = userService.queryUserList();
PageInfo<User> userInfoPage = new PageInfo(users);
//System.out.println(users);
return userInfoPage;
}
}
然后我的测试结果如下图
最后:好好学习,天天向上!