作者: Memory(星哥) Wechat:/QQ: 574373426
整理不易,感谢支持,欢迎 收藏 转发 分享
专注IT职业教育多年,学编程找星哥
SpringBoot整合Mybatis分页插件-pagehelper
引言
技术栈
后端采用 SpringBoot,Mybatis,pagehelper
开发工具 IDEA2020.3.4 , Maven
数据库 Mysql
这里没有前端实现,我们从浏览器发起请求,直接查看返回的数据是否符合分页数据即可
背景
分页查询在我们项目中很常见,常用的实现方法有很多
- 自己写分页逻辑(麻烦)
- Mybatis整合pagehelper分页插件(常用,简单易用)
- Mybtais-plus中的PaginationInterceptor(常用,简单易用)
本文我们使用第二种方式,快速实现分页效果
准备数据库文件
CREATE database pagehelperdata DEFAULT CHARACTER SET utf8;
use pagehelperdata;
create table user(
id int primary key auto_increment,
name varchar(10),
pwd varchar(10),
status int
) charset utf8;
insert into user(id,name,pwd,status) value (null,'张三','123',1);
insert into user(id,name,pwd,status) value (null,'学编程找星哥','574373426',1);
insert into user(id,name,pwd,status) value (null,'李四','123',0);
insert into user(id,name,pwd,status) value (null,'aa','123',1);
insert into user(id,name,pwd,status) value (null,'bb','574373426',1);
insert into user(id,name,pwd,status) value (null,'cc','123',0);
insert into user(id,name,pwd,status) value (null,'dd','123',1);
insert into user(id,name,pwd,status) value (null,'ee','574373426',1);
insert into user(id,name,pwd,status) value (null,'ff','123',0);
insert into user(id,name,pwd,status) value (null,'gg','123',1);
insert into user(id,name,pwd,status) value (null,'hh','574373426',1);
insert into user(id,name,pwd,status) value (null,'ii','123',0);
执行以上数据库文件,准备测试数据
分页实现
创建项目
使用阿里云脚手架创建: https://start.aliyun.com
倒入的依赖有点多,pagehelper的依赖是搜不到的,只能手动倒入,所以我这里先选一个web依赖,后续手动倒入
倒入依赖
在pom.xml中 倒入依赖
<dependencies>
<!--Web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring整合jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!--Mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--测试类依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
编写配置文件
编写Springboot配置文件,整合数据源,整合pagehelper等
pagehelper相关的配置,不配置也行,因为有默认的值
如果有特殊业务需求可以参考官方文档:https://pagehelper.github.io/docs/howtouse/
application.properties 格式
# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/pagehelperdata?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root
#pagehelper分页插件配置 不写也行
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
application.yml 格式
server:
port:8080
mybatis:
mapper-locations:classpath:mappers/*xml
spring:
datasource:
driver-class-name:com.mysql.cj.jdbc.Driver
name:defaultDataSource
url:jdbc:mysql://localhost:3306/pagehelperdata?serverTimezone=UTC
username:root
password:memory0309
pagehelper:
helperDialect:mysql
reasonable:true
supportMethodsArguments:true
params:count=countSql
创建实体类
在com.xg.pojo下创建User实体类
利用lombok插件中@Data注解帮我们生成get set方法
属性名与数据库字段名保持一致
package com.xg.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String pwd;
private String status;
}
Mapper层
在resource下创建mappers目录,用于存放mapper.xml文件
在resource/mappers下创建UserMapper.xml
编写sql文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xg.mapper.UserMapper">
<select id="findAll" resultType="com.xg.pojo.User">
select id,name,pwd,status from user
</select>
</mapper>
Service层
在com.xg.service下创建UserService接口
返回值类型: PageInfo 此类型是pagehelper中提供的类型,后续分页的数据都会自动封装到这个对象的属性中,具体有哪些属性,我们下面分析
参数:
当前页: pageNo
每页显示条数: pageSize
package com.xg.service;
import com.github.pagehelper.PageInfo;
import com.xg.pojo.User;
public interface UserService {
PageInfo<User> findAll(Integer pageNo, Integer pageSize);
}
UserServiceImpl实现类
调用PageHelper.startPage(pageNo,pageSize)方法,进行分页,将分页规则传递到此方法,会自动识别分页信息,生成分页查询条件limit
然后将将查询到的数据封装到PageInfo中,在PageInfo中自动帮我们计算出总记录数,总页数,是否有上一页下一页等等分页信息
package com.xg.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xg.mapper.UserMapper;
import com.xg.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public PageInfo<User> findAll(Integer pageNo,Integer pageSize) {
PageHelper.startPage(pageNo,pageSize);
List<User> list = userMapper.findAll();
//将查询到的数据封装到PageInfo中
PageInfo<User> pageInfo = new PageInfo<>(list);
return pageInfo;
}
}
Controller层
在com.xg.controller下创建UserController
使用restful风格接收前端传递的当前页与每页显示条数
当前页: pageNo
每页显示条数: pageSize
返回值类型: PageInfo 封装了分页信息
package com.xg.controller;
import com.github.pagehelper.PageInfo;
import com.xg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/findAll/{pageNo}/{pageSize}")
public PageInfo findAll(
@PathVariable("pageNo") Integer pageNo,
@PathVariable("pageSize") Integer pageSize){
System.out.println(pageNo+pageSize);
return userService .findAll(pageNo,pageSize);
}
}
发起请求查看返回的数据
假设查询第二页,每页显示三条,通过restful风格传递参数
请求地址: http://localhost:8080/findAll/2/3
我们来分析一下返回的数据有哪些
为了方便查看,将返回的json数据格式化整理如下:
{
"total": 12,
"list": [
{
"id": 4,
"name": "aa",
"pwd": "123",
"status": "1"
},
{
"id": 5,
"name": "bb",
"pwd": "574373426",
"status": "1"
},
{
"id": 6,
"name": "cc",
"pwd": "123",
"status": "0"
}
],
"pageNum": 2,
"pageSize": 3,
"size": 3,
"startRow": 4,
"endRow": 6,
"pages": 4,
"prePage": 1,
"nextPage": 3,
"isFirstPage": false,
"isLastPage": false,
"hasPreviousPage": true,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3,
4
],
"navigateFirstPage": 1,
"navigateLastPage": 4
}
PageInfo.list 结果集当前页数据
PageInfo.pageNum 当前页码
PageInfo.pageSize 当前页面显示的数据条目
PageInfo.pages 总页数
PageInfo.total 数据的总条目数
PageInfo.prePage 上一页
PageInfo.nextPage 下一页
PageInfo.isFirstPage 是否为第一页
PageInfo.isLastPage 是否为最后一页
PageInfo.hasPreviousPage 是否有上一页
PageHelper.hasNextPage 是否有下一页
总结
我们只传递了当前页和每页显示的条数
调用PageHelper.startPage(pageNo,pageSize)方法
将数据封装到PageInfo: PageInfo pageInfo = new PageInfo<>(list);
PageHelper分页插件自动帮我们计算出了分页信息大全,封装到PageInfo中,我们可以在前端从PageInfo中取需要的值即可,极大的方便了我们分页的业务
项目Gitee仓库地址
https://gitee.com/memoryzgx/pagehelper.git
欢迎转发收藏分享
欢迎转发收藏分享
欢迎转发收藏分享