springboot实现分页功能

引入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.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.huyangfeng</groupId>
    <artifactId>springboot-student-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-student-crud</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--JDBC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>-->
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis整合springboot-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!--<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>-->
        <!--mysql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok:偷懒神器-->
        <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>

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency><!--导入分页插件-->
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </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>

实体

接口

 Service

 mapper.xml

 ServiceImpl 

package com.xc.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.mapper.Studentmapper;
import com.xc.pojo.Student;
import com.xc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 星晨
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private Studentmapper studentmapper;

    @Override
    public PageInfo<Student> queryAllByPage(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentmapper.queryAllByPage();
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        return pageInfo;
    }
}

Controller

package com.xc.controller;

import com.github.pagehelper.PageInfo;
import com.xc.pojo.Student;
import com.xc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 星晨
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public String queryAllByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                 Model model){
        PageInfo<Student> studentPageInfo = studentService.queryAllByPage(pageNum, pageSize);
//        System.out.println(studentPageInfo);
//        System.out.println(studentPageInfo.getList());
//        System.out.println(studentPageInfo.getList().indexOf(1)+1);
        model.addAttribute("studentPageInfo",studentPageInfo);
        return "list";
    }

}

前端

<div class="as"><!--取自:https://blog.csdn.net/qq_15329947/article/details/101364287-->
    <div class="as one">
        当前第<span th:text="${studentPageInfo.pageNum}"></span> 页,
        共<span th:text="${studentPageInfo.pages}"></span> 页,
        <span th:text="${studentPageInfo.total}"></span>条记录
    </div>
    <ul class="as current" style="list-style: none">
        <!--th:if="${studentPageInfo.hasPreviousPage}"}-->
        <li th:if="${studentPageInfo.hasPreviousPage}"}><!--hasPreviousPage默认值为false, 如果有上一页,则不显示首页-->
            <a th:href="@{/list?pageNum=}+${1}">首页</a>
        </li>
        <li th:if="${studentPageInfo.hasPreviousPage}"><!--hasPreviousPage默认值为false, 如果有上一页,则不显示-->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.prePage}">上一页</a>
        </li>
        <li th:each="nav:${studentPageInfo.navigatepageNums}"><!--navigatepageNums是一个数组:遍历所有导航页号。 -->
            <a th:href="@{/list?pageNum=}+${nav}" th:text="${nav}" th:if="${nav != studentPageInfo.pageNum}"></a><!--如果不是当前页则可以跳转其他页面。遍历展示-->
            <a th:class="${'active'}" th:if="${nav == studentPageInfo.pageNum}" th:text="${nav}"></a><!--如果是当前页则有样式。遍历 展示-->
        </li>
        <li th:if="${studentPageInfo.hasNextPage}"><!--hasNextPage默认值为false, 如果没有下一页,则不显示-->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.nextPage}">下一页</a>
        </li>
        <li th:if="${studentPageInfo.pageNum < studentPageInfo.pages}"><!--如果当前页小于总页数则不显示尾页 -->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.pages}">尾页</a>
        </li>
    </ul>
</div>

 效果

 

猜你喜欢

转载自blog.csdn.net/xingchenyv/article/details/122797602