当前位置:   article > 正文

springboot整合分页插件PageHelper

pagehelper

springboot整合分页插件PageHelper

用idea来演示,新建一个springboot项目


配置信息好之后,单击next

选择依赖,简单选择一下就行

 命名项目和位置,之后点击finish

2.导入依赖page-helper

我们需要引入项目的更多依赖,打开项目的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.2.5.RELEASE</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>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- thymeleaf -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  24. </dependency>
  25. <!-- web -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <!-- 整合mybatis所需要的依赖-->
  31. <dependency>
  32. <groupId>org.mybatis.spring.boot</groupId>
  33. <artifactId>mybatis-spring-boot-starter</artifactId>
  34. <version>2.1.3</version>
  35. </dependency>
  36. <!-- pagehelper分页 -->
  37. <dependency>
  38. <groupId>com.github.pagehelper</groupId>
  39. <artifactId>pagehelper-spring-boot-starter</artifactId>
  40. <version>1.4.1</version>
  41. </dependency>
  42. <!-- 数据库驱动 -->
  43. <dependency>
  44. <groupId>mysql</groupId>
  45. <artifactId>mysql-connector-java</artifactId>
  46. </dependency>
  47. <!-- 数据库连接池 -->
  48. <dependency>
  49. <groupId>com.alibaba</groupId>
  50. <artifactId>druid</artifactId>
  51. <version>1.2.4</version>
  52. </dependency>
  53. <!-- 热部署devtools -->
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-devtools</artifactId>
  57. <scope>runtime</scope>
  58. <optional>true</optional>
  59. </dependency>
  60. <!-- lombok -->
  61. <dependency>
  62. <groupId>org.projectlombok</groupId>
  63. <artifactId>lombok</artifactId>
  64. <optional>true</optional>
  65. </dependency>
  66. <!-- 单元测试 -->
  67. <dependency>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-starter-test</artifactId>
  70. <scope>test</scope>
  71. </dependency>
  72. </dependencies>
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.springframework.boot</groupId>
  77. <artifactId>spring-boot-maven-plugin</artifactId>
  78. <configuration>
  79. <excludes>
  80. <exclude>
  81. <groupId>org.projectlombok</groupId>
  82. <artifactId>lombok</artifactId>
  83. </exclude>
  84. </excludes>
  85. </configuration>
  86. </plugin>
  87. </plugins>
  88. </build>
  89. </project>

我这里导入的是1.4.1版本,如果想要更换其他版本在maven仓库中查找

 pagehelper的maven仓库网址:https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter

3.新建数据库

我们数据库名称为bills,里面有一张表,名为 tb_customer,如图:

4.新建实体类

我们新建包pojo,在其包下新建 User.java实体类,以及查询类tbCustomerQuery.java ,由于我们在pom.xml中引入了lombok依赖,可以省略getter和setter等方法。

 Lombok插件安装

之后在Settings面板下点击 Plugins 

安装好lombok插件后,我们的具体代码如下:

  1. package com.pojo;
  2. import lombok.Data;
  3. /*
  4. *
  5. * 加了@Data注解的类,编译后会自动给我们加上下列方法:
  6. 所有属性的get和set方法
  7. toString 方法
  8. hashCode方法
  9. equals方法
  10. *
  11. * @AllArgsConstructor :有参构造方法
  12. * @NoArgsConstructor :无参构造方法
  13. * */
  14. @Data
  15. @AllArgsConstructor
  16. @NoArgsConstructor
  17. public class tbCustomer {
  18. private Integer id;
  19. private String name;
  20. private String remark;
  21. private String telephone;
  22. private String address;
  23. private Integer typeId;
  24. // private String typeName;
  25. }

5.新建Dao层

 我们新建dao包,在其包下新建接口并命名为 tbCustomerDao.java ,具体代码如下:

  1. package com.mapper;
  2. import com.pojo.tbCustomer;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Param;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import java.util.List;
  7. @Mapper
  8. public interface tbCustomerMapper {
  9. /**
  10. * 带条件分页
  11. * @param tbCustomer
  12. * @return
  13. */
  14. List<tbCustomer> findPages(tbCustomer tbCustomer);
  15. /**
  16. * 分页查询
  17. * @return
  18. */
  19. List<tbCustomer> findPage();
  20. /**
  21. * 查询所有信息
  22. * @return
  23. */
  24. List<tbCustomer> getAll(@Param("name") String name);
  25. /**
  26. * 新增信息
  27. * @param tbCustomer
  28. * @return
  29. */
  30. int addTbCustomer(tbCustomer tbCustomer);
  31. /**
  32. * 删除信息
  33. */
  34. int deleteTbCustomer(Integer id);
  35. /**
  36. * 修改信息
  37. */
  38. int updateTbCustomer(tbCustomer tbCustomer);
  39. tbCustomer getById(Integer id);
  40. }

扩展

如果我们有多个dao接口,我们可以不用在每个接口上都添加 @Mapper 注解,直接在项目启动类上添加

 6.新建Mapper

我们在项目resources资源目录下新建一个 mapper 包,并在其包下新建 tbCustomerMapper.xml 文件,编写tbCustomerrDao.java接口对应的sql语句,具体代码如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.mapper.tbCustomerMapper">
  6. <select id="findPages" resultType="tbCustomer">
  7. select * from tb_customer
  8. <trim prefix="where" prefixOverrides="and|or">
  9. <if test="name!=null and name!='' and name.length!=0">
  10. and name like concat("%",#{name},"%")
  11. </if>
  12. <if test="remark!=null and remark!='' and remark.length!=0">
  13. and remark like concat("%",#{remark},"%")
  14. </if>
  15. </trim>
  16. </select>
  17. <select id="findPage" resultType="tbCustomer">
  18. select * from tb_customer
  19. </select>
  20. <select id="getAll" resultType="tbCustomer">
  21. select t.id,t.`name`,t.remark,t.telephone,t.address,t.typeId,b.typeName from tb_customer t,tb_customer_type b
  22. <where>
  23. and t.id=b.id
  24. <if test="name!=null and name!=''">and t.name like concat("%",#{name},"%")</if>
  25. </where>
  26. </select>
  27. <insert id="addTbCustomer">
  28. insert into tb_customer values(null,#{name},#{remark},#{telephone},#{address},#{typeId})
  29. </insert>
  30. <delete id="deleteTbCustomer">
  31. delete from tb_customer where id=#{id}
  32. </delete>
  33. <update id="updateTbCustomer">
  34. update tb_customer set name=#{name},remark=#{remark},telephone=#{telephone},address=#{address},typeId=#{typeId} where id=#{id}
  35. </update>
  36. <select id="getById" resultType="tbCustomer">
  37. select * from tb_customer where id=#{id}
  38. </select>
  39. </mapper>

7.application.yml配置

注意:ideal创建的springboot项目的配置文件默认是properties格式的,这里我们将其后缀更改为yml,具体代码如下:

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://127.0.0.1:3306/bills?characterEncoding=utf-8&serverTimezone=UTC
  7. username: root
  8. password:
  9. thymeleaf:
  10. mode: HTML5
  11. prefix: classpath:/templates/
  12. suffix: .html
  13. encoding: utf-8
  14. cache: false
  15. mybatis:
  16. mapper-locations: classpath:/mapper/*.xml #mapper文件位置
  17. type-aliases-package: com.pojo # 实体类位置
  18. configuration:
  19. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出SQL日志
  20. pagehelper:
  21. helper-dialect: mysql # 指定分页插件使用哪种语言
  22. reasonable: true # 分页合理化参数,默认为false,当该值为true,pageNum<=0默认查询第一页,pageNum>pages时会查询最后一页,false时直接根据参数进行查询
  23. support-methods-arguments: true # 默认为false, 为true时允许在运行时根据多数据源自动识别对应的方言进行分页
  24. params: countSql # 为了支持startPage(Object params)方法,增加该参数来配置参数映射,用于从对象中根据属性名取值,可以配置pageNum,pageSize,pageSizeZero, reasonable, 不配置映射是使用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero

8.新建service

新建接口 tbCustomerService.java 以及他的实现类 tbCustomerService.java ,具体代码如下:

tbCustomerService.java

  1. package com.service;
  2. import com.github.pagehelper.PageInfo;
  3. import com.pojo.tbCustomer;
  4. import java.util.List;
  5. public interface tbCustomerService {
  6. PageInfo findPages(tbCustomer tbCustomer,int pageIndex,int pageSize);
  7. PageInfo findPage(int pageIndex,int pageSize);
  8. List<tbCustomer> getAll(String name);
  9. int addTbCustomer(tbCustomer tbCustomer);
  10. int deleteTbCustomer(Integer id);
  11. int updateTbCustomer(tbCustomer tbCustomer);
  12. tbCustomer getById(Integer id);
  13. }

tbCustomerServiceImpl.java

  1. package com.service;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.mapper.tbCustomerMapper;
  5. import com.pojo.tbCustomer;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. public class tbCustomerServiceImpl implements tbCustomerService{
  11. @Autowired
  12. private tbCustomerMapper tbCustomerMapper;
  13. @Override
  14. public PageInfo findPages(tbCustomer tbCustomer, int pageIndex, int pageSize) {
  15. PageHelper.startPage(pageIndex,pageSize);
  16. List<tbCustomer> pages = tbCustomerMapper.findPages(tbCustomer);
  17. PageInfo pageInfo=new PageInfo(pages);
  18. return pageInfo;
  19. }
  20. @Override
  21. public PageInfo findPage(int pageIndex, int pageSize) {
  22. PageHelper.startPage(pageIndex,pageSize);
  23. List<tbCustomer> page = tbCustomerMapper.findPage();
  24. PageInfo pageInfo=new PageInfo(page);
  25. return pageInfo;
  26. }
  27. @Override
  28. public List<tbCustomer> getAll(String name){
  29. return tbCustomerMapper.getAll(name);
  30. }
  31. @Override
  32. public int addTbCustomer(tbCustomer tbCustomer) {
  33. return tbCustomerMapper.addTbCustomer(tbCustomer);
  34. }
  35. @Override
  36. public int deleteTbCustomer(Integer id) {
  37. return tbCustomerMapper.deleteTbCustomer(id);
  38. }
  39. @Override
  40. public int updateTbCustomer(tbCustomer tbCustomer) {
  41. return tbCustomerMapper.updateTbCustomer(tbCustomer);
  42. }
  43. @Override
  44. public tbCustomer getById(Integer id) {
  45. return tbCustomerMapper.getById(id);
  46. }
  47. }

9.新建html文件

我们在resources的templates下新建 index.html 和 page.html 文件,具体代码如下:

index.html

  1. <!DOCTYPE html>
  2. <!--suppress ALL-->
  3. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Title</title>
  7. </head>
  8. <body>
  9. <form th:action="@{/getPages}">
  10. 商品信息<input type="text" name="name" placeholder="请输入你要查询的信息"/>
  11. 商品描述<input type="text" name="remark"/>
  12. <input value="查询" type="submit">
  13. </form>
  14. <a href="add.html" th:href="@{/add.html}">新增</a>
  15. <table border="1">
  16. <tr>
  17. <td>商品编号</td>
  18. <td>商品名称</td>
  19. <td>商品信息</td>
  20. <td>联系方式</td>
  21. <td>家庭住址</td>
  22. <td>会员等级</td>
  23. <td>操作</td>
  24. </tr>
  25. <tr th:each="put:${page.list}">
  26. <td th:text="${put.id}"></td>
  27. <td th:text="${put.name}"></td>
  28. <td th:text="${put.remark}"></td>
  29. <td th:text="${put.telephone}"></td>
  30. <td th:text="${put.address}"></td>
  31. <td th:text="${put.typeId}"></td>
  32. <td>
  33. <a th:href="@{'/deleteCustomer/'+${put.id}}">删除</a>
  34. <a th:href="@{'/toUpdate/'+${put.id}}">修改</a>
  35. </td>
  36. </tr>
  37. </table>
  38. <div th:insert="page :: page"></div>
  39. </div>
  40. </body>
  41. </html>

page.html

  1. <!DOCTYPE html>
  2. <!--suppress ALL-->
  3. <div th:fragment="page">
  4. <style>
  5. .page{
  6. border: 1px solid blue;
  7. margin-right: 5px;
  8. padding: 3px 5px;
  9. }
  10. .page.active{
  11. color: red;
  12. border: 1px solid red;
  13. }
  14. #div1{
  15. position: absolute;
  16. left:420px;
  17. top:140px;
  18. }
  19. </style>
  20. <div style="float: left">
  21. 当前第<span th:text="${page.pageNum}"></span>
  22. 总记录数<span th:text="${page.total}"></span>
  23. </div>
  24. <div id="div1">
  25. <a th:text="首页" th:if="${page.pageNum>1}" th:href="@{${path}}"></a>
  26. <a th:text="上一页" th:if="${page.pageNum>1}" th:href="@{${path}+${page.pageNum-1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  27. <a th:href="@{${path}+${i}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}" th:each="i:${#numbers.sequence(1,page.pages)}" th:text="${i}"
  28. th:class="${page.pageNum==i}? 'page active':'page'"></a>
  29. <a th:text="下一页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pageNum+1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  30. <a th:text="尾页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pages}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  31. </div>
  32. </div>
  33. </html>

10.新建controller

我们新建 UserController.java ,具体代码如下:

  1. package com.controller;
  2. import com.github.pagehelper.PageInfo;
  3. import com.pojo.tbCustomer;
  4. import com.service.tbCustomerServiceImpl;
  5. import com.util.Constants;
  6. import com.util.PageSupport;
  7. import org.apache.ibatis.annotations.Param;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.ui.Model;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import javax.servlet.http.HttpSession;
  15. import java.util.List;
  16. /**
  17. * @Author:李润成
  18. * @Date:2022-09-21
  19. */
  20. @Controller
  21. public class tbCustomerController {
  22. @Autowired
  23. private tbCustomerServiceImpl tbCustomerService;
  24. /**
  25. * 带条件分页加模糊查询
  26. * @param model
  27. * @param tbCustomer
  28. * @param pageIndex
  29. * @param pageSize
  30. * @return
  31. */
  32. @RequestMapping("/getPages")
  33. public String getPages(Model model,tbCustomer tbCustomer,
  34. @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
  35. @RequestParam(value = "pageSize",defaultValue = "2",required = false) int pageSize){
  36. PageInfo page = tbCustomerService.findPages(tbCustomer,pageIndex, pageSize);
  37. model.addAttribute("tbCustomer",tbCustomer);
  38. model.addAttribute("path","getPages?pageIndex=");
  39. model.addAttribute("page",page);
  40. return "index";
  41. }
  42. /**
  43. * 普通分页
  44. * @param model
  45. * @param pageIndex
  46. * @param pageSize
  47. * @return
  48. */
  49. @RequestMapping("/getPage")
  50. public String getPage(Model model,
  51. @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
  52. @RequestParam(value = "pageSize",defaultValue = "2",required = false) int pageSize){
  53. PageInfo page = tbCustomerService.findPage(pageIndex, pageSize);
  54. model.addAttribute("path","getPage?pageIndex=");
  55. model.addAttribute("page",page);
  56. return "index";
  57. }
  58. /**
  59. * 模糊查询
  60. * @param name
  61. * @param model
  62. * @return
  63. * @throws Exception
  64. */
  65. @RequestMapping("/getAll")
  66. public String getAll(@RequestParam(value = "name",required = false)String name, Model model) throws Exception {
  67. List<tbCustomer> list = tbCustomerService.getAll(name);
  68. model.addAttribute("name",name);
  69. model.addAttribute("list", list);
  70. return "index";
  71. }
  72. /**
  73. * 新增信息
  74. * @param tbCustomer
  75. * @return
  76. */
  77. @RequestMapping("/addCustomer")
  78. public String addCustomer(tbCustomer tbCustomer) {
  79. int i = tbCustomerService.addTbCustomer(tbCustomer);
  80. if (i > 0) {
  81. return "redirect:/getPages";
  82. }
  83. return "redirect:error";
  84. }
  85. /**
  86. * 删除信息
  87. * @param id
  88. * @return
  89. */
  90. @RequestMapping("/deleteCustomer/{id}")
  91. public String deleteCustomer(@PathVariable("id") Integer id) {
  92. int i = tbCustomerService.deleteTbCustomer(id);
  93. if (i > 0) {
  94. return "redirect:/getPages";
  95. }
  96. return "redirect:error";
  97. }
  98. /**
  99. * 修改信息
  100. * @param id
  101. * @param model
  102. * @return
  103. */
  104. @RequestMapping("/toUpdate/{id}")
  105. public String toUpdate(@PathVariable("id")Integer id, Model model) {
  106. tbCustomer byId = tbCustomerService.getById(id);
  107. model.addAttribute("updateList", byId);
  108. return "update";
  109. }
  110. @RequestMapping("/updateCustomer")
  111. public String updateCustomer(tbCustomer tbCustomer) {
  112. int i = tbCustomerService.updateTbCustomer(tbCustomer);
  113. if (i > 0) {
  114. return "redirect:/getPages";
  115. }
  116. return "redirect:/error";
  117. }
  118. /**
  119. * 统一返回页面代码
  120. * @param page
  121. * @return
  122. */
  123. @RequestMapping("/{page}.html")
  124. public String toPage(@PathVariable("page")String page){
  125. return page;
  126. }
  127. }

11.启动项目

项目演示

我们输入 http://localhost:8080/getPages 进入主界面,

分页的代码

显示当前页数、总页数和总条数,当前是第一页时不显示“上一页”,当前是最后一页时不显示“下一页”,可以输入具体的页面进行跳转

  1. <!DOCTYPE html>
  2. <!--suppress ALL-->
  3. <div th:fragment="page">
  4. <style>
  5. .page{
  6. border: 1px solid blue;
  7. margin-right: 5px;
  8. padding: 3px 5px;
  9. }
  10. .page.active{
  11. color: red;
  12. border: 1px solid red;
  13. }
  14. #div1{
  15. position: absolute;
  16. left:420px;
  17. top:140px;
  18. }
  19. </style>
  20. <div style="float: left">
  21. 当前第<span th:text="${page.pageNum}"></span>
  22. 总记录数<span th:text="${page.total}"></span>
  23. </div>
  24. <div id="div1">
  25. <a th:text="首页" th:if="${page.pageNum>1}" th:href="@{${path}}"></a>
  26. <a th:text="上一页" th:if="${page.pageNum>1}" th:href="@{${path}+${page.pageNum-1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  27. <a th:href="@{${path}+${i}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}" th:each="i:${#numbers.sequence(1,page.pages)}" th:text="${i}"
  28. th:class="${page.pageNum==i}? 'page active':'page'"></a>
  29. <a th:text="下一页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pageNum+1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  30. <a th:text="尾页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pages}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
  31. </div>
  32. </div>
  33. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/87935
推荐阅读
相关标签
  

闽ICP备14008679号