springBoot学习笔记(2.4)—— 整合PageHelper分页组件
一、构建项目步骤
1.引入pageHelper的jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2.完整pom.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot-pageHelp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.application.yml的内容
spring:
datasource:
# 数据源基本配置
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
main:
allow-circular-references: true #解决升级 Spring Boot 2.6后,因循环引用导致启动时报错的问题
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
server:
port: 8083 # 配置项目启动端口
pagehelper:
helper-dialect: mysql #设置pageHelper的语言
reasonable: true
4.controller中查询分页数据
/****
* description: 查询分页数据
* version: 1.0 ->
* date: 2022/1/7 18:09
* author: xiaYZ
* iteration: 迭代说明
* @param commodityName
* @param pageSize 每页数据大小
* @param pageNum 页码
* @return java.lang.Object
*/
@GetMapping("findAllCommodity")
public Object findAllCommodity(String commodityName, int pageSize,int pageNum){
List<Commodity> allCommodity;
try{
PageHelper.startPage(pageNum, pageSize);
allCommodity = commodityService.findAllCommodity(commodityName);
}catch (Exception e){
e.printStackTrace();
return "查询分页数据错误";
}
PageInfo<Commodity> pageInfo = new PageInfo<>(allCommodity);
return pageInfo;
}
5.service层中调用dao接口
public List<Commodity> findAllCommodity(String commodityName){
return commodityDao.findAllCommodity(commodityName);
}
6.dao中的接口
/**
* description: 查询所有商品数据
* version: 1.0
* date: 2022/1/7 18:01
* author: xiaYZ
* iteration: 迭代说明
* @param commodityName 商品类
* @return
*/
List<Commodity> findAllCommodity(String commodityName);
7.mapper文件内容
<select id="findAllCommodity" resultType="com.example.demo.entity.Commodity">
select id, commodity_name commodityName, create_time createTime
from commodity
<where>
<if test="commodityName != null and commodityName != ''">
commodity_name = #{commodityName}
</if>
</where>
</select>
pageHelper生效后查询语句后面会自动添加limit语句限制
8.运行截图
总结
引入pagehelper的jar包
设置pageHelper的语言种类
pagehelper:
helper-dialect: mysql #设置pageHelper的语言
reasonable: true
- java语言中使用PageHelper
PageHelper.startPage(pageNum, pageSize);
注意 PageHelper.startPage(pageNum, pageSize);这句要紧跟查询数据的语句,否则会失效