7、SpringBoot+Mybatis整合------PageHelper简单分页

开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b8afcab7414370c

PageHelper官方地址:https://pagehelper.github.io/

前言:

当我们操作数据库获取数据列表时,往往面对对查询数据进行分页的情况。

今天,我们使用PageHelper插件,来简单实现分页效果。

一、Page简单使用

1.添加pom依赖:

图片描述

图片描述

 <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.xm</groupId>
     <artifactId>demo005_Mybatis</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <name>demo005_Mybatis</name>
     <description>Demo project for Spring Boot</description>
 
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.2.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>
 
     <dependencies>
         <!-- 添加PageHelper依赖 -->
         <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version>1.2.3</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>1.3.2</version>
         </dependency>
 
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 
 </project>

pom.xml

2.添加测试用例:

图片描述

图片描述

 package com.xm;
 
 import java.util.List;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import com.github.pagehelper.Page;
 import com.github.pagehelper.PageHelper;
 import com.xm.mapper.StudentMapper;
 import com.xm.pojo.Student;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
 public class StudentTest {
     @Autowired
     private StudentMapper studentMapper;
     
     
     
     @Test
     public void pageText() {
         Page page = PageHelper.startPage(1, 5);
         List<Student> students = studentMapper.list();
         for(Student student : students) {
             System.out.println(student.toString());
         }
         System.out.println("当前页:"+page.getPageNum());
         System.out.println("当前页数据数:"+page.getPageSize());
         System.out.println("页总数:"+page.getPages());
         System.out.println("数据总数:"+page.getCountColumn());
         System.out.println("第3个数据详情:"+page.get(2));
     }
     
 
 }

StudentTest.java

3.测试结果:

图片描述