springBoot学习笔记(2.4)—— 整合PageHelper分页组件

一、构建项目步骤

1.引入pageHelper的jar包

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper-spring-boot-starter</artifactId>
  4. <version>1.2.3</version>
  5. </dependency>

2.完整pom.xml内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springBoot-pageHelp</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.mybatis.spring.boot</groupId>
  26. <artifactId>mybatis-spring-boot-starter</artifactId>
  27. <version>2.2.1</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.github.pagehelper</groupId>
  36. <artifactId>pagehelper-spring-boot-starter</artifactId>
  37. <version>1.2.3</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-web</artifactId>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. <configuration>
  60. <excludes>
  61. <exclude>
  62. <groupId>org.projectlombok</groupId>
  63. <artifactId>lombok</artifactId>
  64. </exclude>
  65. </excludes>
  66. </configuration>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

3.application.yml的内容

  1. spring:
  2. datasource:
  3. # 数据源基本配置
  4. username: root
  5. password: 123456
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
  8. main:
  9. allow-circular-references: true #解决升级 Spring Boot 2.6后,因循环引用导致启动时报错的问题
  10. mybatis:
  11. mapper-locations: classpath:mybatis/mapper/*.xml
  12. server:
  13. port: 8083 # 配置项目启动端口
  14. pagehelper:
  15. helper-dialect: mysql #设置pageHelper的语言
  16. reasonable: true

4.controller中查询分页数据

  1. /****
  2. * description: 查询分页数据
  3. * version: 1.0 ->
  4. * date: 2022/1/7 18:09
  5. * author: xiaYZ
  6. * iteration: 迭代说明
  7. * @param commodityName
  8. * @param pageSize 每页数据大小
  9. * @param pageNum 页码
  10. * @return java.lang.Object
  11. */
  12. @GetMapping("findAllCommodity")
  13. public Object findAllCommodity(String commodityName, int pageSize,int pageNum){
  14. List<Commodity> allCommodity;
  15. try{
  16. PageHelper.startPage(pageNum, pageSize);
  17. allCommodity = commodityService.findAllCommodity(commodityName);
  18. }catch (Exception e){
  19. e.printStackTrace();
  20. return "查询分页数据错误";
  21. }
  22. PageInfo<Commodity> pageInfo = new PageInfo<>(allCommodity);
  23. return pageInfo;
  24. }

5.service层中调用dao接口

  1. public List<Commodity> findAllCommodity(String commodityName){
  2. return commodityDao.findAllCommodity(commodityName);
  3. }

6.dao中的接口

  1. /**
  2. * description: 查询所有商品数据
  3. * version: 1.0
  4. * date: 2022/1/7 18:01
  5. * author: xiaYZ
  6. * iteration: 迭代说明
  7. * @param commodityName 商品类
  8. * @return
  9. */
  10. List<Commodity> findAllCommodity(String commodityName);

7.mapper文件内容

  1. <select id="findAllCommodity" resultType="com.example.demo.entity.Commodity">
  2. select id, commodity_name commodityName, create_time createTime
  3. from commodity
  4. <where>
  5. <if test="commodityName != null and commodityName != ''">
  6. commodity_name = #{commodityName}
  7. </if>
  8. </where>
  9. </select>

pageHelper生效后查询语句后面会自动添加limit语句限制

8.运行截图

springBoot学习笔记(2.4)—— 整合PageHelper分页组件 - 图1
springBoot学习笔记(2.4)—— 整合PageHelper分页组件 - 图2

总结

  1. 引入pagehelper的jar包

  2. 设置pageHelper的语言种类

    1. pagehelper:
    2. helper-dialect: mysql #设置pageHelper的语言
    3. reasonable: true
  1. java语言中使用PageHelper
    1. PageHelper.startPage(pageNum, pageSize);


注意 PageHelper.startPage(pageNum, pageSize);这句要紧跟查询数据的语句,否则会失效

项目源码