第一件事,导入jar包,贴上maven

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>

然后我们要知道,如果是以前我们用Mybatis的话用这个分页插件需要在mybatis的配置文件中配置,但是如今使用springboot的话我们没有mybatis的配置文件了,我们只能在springboot的配置文件中配置分页插件的信息

 

配置分页插件信息包括一些数据库的信息 application.properties

#端口号
server.port=8091
#设置访问路径
#server.servlet.context-path=/hw
#配置编码格式
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

#配置数据库相关信息
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#设置别名
mybatis.type-aliases-package=com.hw.entity

#配置mybatis的分页插件信息
#数据库
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.page-size-zero=true
pagehelper.params=count=countSql

然后贴上controller的代码

@RequestMapping("/gets.action")
public void getS(HttpServletRequest request, HttpServletResponse response){
//第一个参数为页数,第二个为显示数
PageHelper.startPage( Integer.parseInt(request.getParameter("page")),
Integer.parseInt(request.getParameter("limit")));
//去数据库中查询数据
List<Student> allStudents = studentsServices.getAllStudents();
//获取分页后的数据和信息
PageInfo pageInfo=new PageInfo(allStudents);
//pageInfo为分页的详细信息
Map<String,Object> object=new HashMap<>();
object.put("code", 0);
object.put("msg", "");
object.put("count", pageInfo.getTotal());
object.put("data", pageInfo.getList());
String str = JSON.toJSONString(object);
try {
//将数据返回到前端
print(str,response);
} catch (IOException e) {
e.printStackTrace();
}
}

前端页面示例

Springboot整合Mybatis的分页插件PageHelper_springboot使用

Springboot整合Mybatis的分页插件PageHelper_分页_02