开发工具: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.测试结果:


7、SpringBoot+Mybatis整合------PageHelper简单分页的更多相关文章

  1. Springboot+Mybatis整合PageHelper

    一.全部的gradle引入 // https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-sta ...

  2. springBoot mybatis mysql pagehelper layui 分页

    <!-- 加入 pagehelper 分页插件 jar包--><dependency> <groupId>com.github.pagehelper</gro ...

  3. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  4. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  5. SpringBoot集成整合pageHelper分页插件

    今天来讲讲springboot 集成 pagehelper插件, 引入jar 依赖包 <!-- 分页插件 --><dependency> <groupId>com. ...

  6. SpringBoot+Mybatis+Freemark 最简单的例子

    springboot-sample 实现最简单的 SpringBoot + Mybatis + Freemarker 网页增删改查功能,适合新接触 Java 和 SpringBoot 的同学参考 代码 ...

  7. SpringBoot+Mybatis整合入门(一)

    SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...

  8. SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...

  9. springboot+mybatis使用PageHelper分页

    项目结构和spring搭建mybatis请参考springboot整合mybatis.在这个基础上配置分页. 一:导入PageHelper依赖 <dependency> <group ...

随机推荐

  1. 如何解释kworker线程的名称

    http://www.kbase101.com/question/24502.html 在Linux 3.11.0-13上 - 在双插槽Xeon X5650六核板上运行的通用,htop显示不同的kwo ...

  2. (转)shell解析命令行的过程以及eval命令

    shell解析命令行的过程以及eval命令   本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过 ...

  3. 管理员账户权限不足 解决方案 类似没有XXX权限之类的问题解决方法

  4. 显示器分辨率不同,部分winform控件在其他机器上显示不全

    在开发机器上效果如下: 而到其他电脑上效果如下: 解决办法: 将窗体的AutoScaleMode属性设置为None,尝试一下,应该可以了. 关于AutoScaleMode的属性,可以参考 http:/ ...

  5. web项目无法被Eclipse的Tomcat识别的解决办法

    Eclipse 导入外部项目无法识别为web项目并且无法在部署到tomcat下 1.进入项目目录,找到 .project 文件: 2.找到 <natures> 代码段,在里面加入如下标签内 ...

  6. MySQL慢查询分析工具pt-query-digest详解

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  7. SQL Server2005修改计算机名后不能发布订阅

    在一台安装有SQL Server2005的计算机上,更改计算机名后,在发布订阅的时候提示如下错误报告: 由于需要需要配置一个发布订阅,可是一直报告:" sql server 复制需要有实际的 ...

  8. Struts2_HelloWorld_6

    为 eclipse 在编写 xml配置文件时提供提示,需要加上dtd或xls的标签定义文件的路径,具体操作: 1.Window——Preferences——XML Catalog 2.添加 dtd 文 ...

  9. 01、Spark安装与配置

    01.Spark安装与配置 1.hadoop回顾 Hadoop是分布式计算引擎,含有四大模块,common.hdfs.mapreduce和yarn. 2.并发和并行 并发通常指针对单个节点的应对多个请 ...

  10. 浩顺晶密K-5 打卡时间设置

    公司有一台浩顺晶密K-5打卡设备,因为时间异常需要重新调整,设备外部就几个按钮,全部按了一遍发现没有任何变化,所以肯定是哪里操作不对,然后用钥匙打开这个设置,上面有一排文字,分别是设置.+.-.确认等 ...