zoukankan      html  css  js  c++  java
  • springboot使用分页插件PageHelper

    依赖:

    1 <!-- spring boot的分页插件PageHelper -->
    2         <dependency>
    3             <groupId>com.github.pagehelper</groupId>
    4             <artifactId>pagehelper-spring-boot-starter</artifactId>
    5             <version>1.2.3</version>
    6         </dependency>

    重写 PageHelper类,后面直接调用即可

     1 package com.test.controller;
     2 
     3 import com.github.pagehelper.Page;
     4 import com.github.pagehelper.PageInfo;
     5 import com.test.entity.Person;
     6 
     7 import java.util.ArrayList;
     8 import java.util.List;
     9 
    10 /**
    11  * @author liuwenlong
    12  * @create 2021-11-07 13:15:41
    13  */
    14 @SuppressWarnings("all")
    15 public class PageHelper {
    16     public  static PageInfo pageHelper(List list, Integer pageNum, Integer pageSize){
    17         Page page = new Page(pageNum, pageSize);
    18         int total = list.size();
    19         page.setTotal(total);
    20         int startIndex = (pageNum - 1) * pageSize;
    21         int endIndex = Math.min(startIndex + pageSize,total);
    22         if(startIndex>endIndex){
    23             page.addAll(new ArrayList());
    24             PageInfo pageInfo = new PageInfo<>(page);
    25             return pageInfo;
    26         }else{
    27             page.addAll(list.subList(startIndex,endIndex));
    28             PageInfo pageInfo = new PageInfo<>(page);
    29             return pageInfo;
    30         }
    31     }
    32 }

    调用

     1 package com.test.controller;
     2 
     3 import com.github.pagehelper.PageInfo;
     4 import com.test.entity.Person;
     5 import org.springframework.stereotype.Controller;
     6 import org.springframework.ui.Model;
     7 import org.springframework.ui.ModelMap;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RequestParam;
    10 import org.springframework.web.bind.annotation.ResponseBody;
    11 import org.springframework.web.servlet.ModelAndView;
    12 
    13 import javax.servlet.http.HttpServletRequest;
    14 import java.util.ArrayList;
    15 import java.util.List;
    16 
    17 /**
    18  * @author liuwenlong
    19  * @create 2021-11-06 22:52:27
    20  */
    21 @SuppressWarnings("all")
    22 @RequestMapping(value = "testController")
    23 @Controller
    24 public class PersonController {
    25 
    26     @RequestMapping(value = "test")
    27     public String getAllUser(@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
    28                              HttpServletRequest req,Model mod) {
    29 
    30 
    31         Person person1 = new Person("aaa", "44");
    32         Person person2 = new Person("bbb", "34");
    33         Person person3 = new Person("vvv", "3333");
    34         Person person4 = new Person("dfdfd", "4543");
    35         Person person5 = new Person("ere", "4567");
    36         Person person6 = new Person("sfa", "7654");
    37         Person person7 = new Person("sfe", "4567");
    38         Person person8 = new Person("ggee", "7654");
    39         Person person9 = new Person("dh3", "567");
    40         Person person10 = new Person("eteg", "6543");
    41 
    42         List<Person> list = new ArrayList<>();
    43         list.add(person1);
    44         list.add(person2);
    45         list.add(person3);
    46         list.add(person4);
    47         list.add(person5);
    48         list.add(person6);
    49         list.add(person7);
    50         list.add(person8);
    51         list.add(person9);
    52         list.add(person10);
    53 
    54         PageHelper pageHelper = new PageHelper();
    55         PageInfo pages = pageHelper.pageHelper(list,pageNum,3);
    56         mod.addAttribute("PageInfo",pages);
    57 
    58         return "lwl/Pagehelper/index";
    59     }
    60 }

    实体类

     1 package com.test.entity;
     2 
     3 import javax.persistence.Entity;
     4 
     5 /**
     6  * @author liuwenlong
     7  * @create 2021-11-06 22:51:12
     8  */
     9 @SuppressWarnings("all")
    10 @Entity
    11 public class Person {
    12     private String username;
    13     private String password;
    14 
    15     public Person() {
    16     }
    17 
    18     public Person(String username, String password) {
    19         this.username = username;
    20         this.password = password;
    21     }
    22 
    23     public String getUsername() {
    24         return username;
    25     }
    26 
    27     public void setUsername(String username) {
    28         this.username = username;
    29     }
    30 
    31     public String getPassword() {
    32         return password;
    33     }
    34 
    35     public void setPassword(String password) {
    36         this.password = password;
    37     }
    38 
    39     @Override
    40     public String toString() {
    41         return "Person{" +
    42                 "username='" + username + '\'' +
    43                 ", password='" + password + '\'' +
    44                 '}';
    45     }
    46 }

    页面

     1 <%--
     2   Created by IntelliJ IDEA.
     3   User: honor
     4   Date: 2021/11/6
     5   Time: 22:58
     6   To change this template use File | Settings | File Templates.
     7 --%>
     8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     9 <%--引用Java公司(Sun)公司写的标签--%>
    10 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    11 <html>
    12 <head>
    13     <title>aaa</title>
    14 </head>
    15 <body>
    16 <table>
    17     <tr>
    18         <th>用户名</th>
    19         <th>密码</th>
    20        
    21        
    22     </tr>
    23     <c:forEach items="${PageInfo.list}" var="y">
    24         <tr>
    25             <td>${y.username}</td>
    26             <td>${y.password}</td>
    27         </tr>
    28     </c:forEach>
    29 
    30 
    31     <div align="center">
    32 
    33         <div>
    34        <%--当前第${PageInfo.pageNum}页,总共${PageInfo.pages}页,总共${PageInfo.total}条记录--%>
    35 
    36             <div align="center">
    37                 <font size="2">总${PageInfo.total}条记录</font>
    38                 <font size="2">共 ${PageInfo.pages}页</font>
    39                 <font size="2">第${PageInfo.pageNum}页</font>
    40                 <a href="${pageContext.request.contextPath}/testController/test?pageNum=1">首页</a>
    41                 <c:choose>
    42                     <c:when test="${PageInfo.pageNum - 1 > 0}">
    43                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pageNum - 1}">上一页</a>
    44                     </c:when>
    45                     <c:when test="${PageInfo.pageNum - 1 <= 0}">
    46                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=1">上一页</a>
    47                     </c:when>
    48                 </c:choose>
    49                 <c:choose>
    50                     <c:when test="${PageInfo.pages==0}">
    51                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pageNum}">下一页</a>
    52                     </c:when>
    53                     <c:when test="${PageInfo.pageNum + 1 < PageInfo.pages}">
    54                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pageNum + 1}">下一页</a>
    55                     </c:when>
    56                     <c:when test="${PageInfo.pageNum + 1 >= PageInfo.pages}">
    57                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pages}">下一页</a>
    58                     </c:when>
    59                 </c:choose>
    60                 <c:choose>
    61                     <c:when test="${PageInfo.pages==0}">
    62                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pageNum}">尾页</a>
    63                     </c:when>
    64                     <c:otherwise>
    65                         <a href="${pageContext.request.contextPath}/testController/test?pageNum=${PageInfo.pages}">尾页</a>
    66                     </c:otherwise>
    67                 </c:choose>
    68             </div>
    69         </div>
    70         </div>
    71 
    72     </div>
    73 
    74 </table>
    75 </body>
    76 </html>

  • 相关阅读:
    Spring2——特殊值的注入问题、自动装配、使用注解定义bean
    关于ueditor配置单图无法上传的问题
    初始WebApi(3)
    WebApi参数检查验证FluentValidation的使用方法
    js获取兄弟之间的标签
    C#集合ArrayList、泛型集合List(3)
    C#栈、堆的理解(2)
    C#值类型、引用类型(1)
    字符串截取的几种方法
    MVC结合Ajax实现简单的批量删除
  • 原文地址:https://www.cnblogs.com/lwl80/p/15519884.html
Copyright © 2011-2022 走看看