开发工具: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. notepad++查看Log

    安装步骤:打开notepad++,插件→ Plugin Manager→ Show Plugin Manager→ Avaliable→ 等待插件列表刷新出来后,选中Android Logger插件→ ...

  2. VM虚拟机安装centos7

    一 安装centos7 下面地址下载基础版 CentOS-7-x86_64-Minimal-1810.iso 镜像  918M http://isoredirect.centos.org/centos ...

  3. React.js 小书 Lesson2 - 前端组件化(一):从一个简单的例子讲起

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson2 转载请注明出处,保留原文链接和作者信息. 很多课程一上来就给大家如何配置环境.怎么写 Re ...

  4. MySQL设置允许用户远程登录

    . //登录数据库 mysql -u root -pvmwaremysql>use mysql; //%为所有ip都可以远程访问 mysql>update user set host = ...

  5. Python学习笔记--语音处理初步

    语音处理最基础的部分就是如何对音频文件进行处理. 声音的物理意义:声音是一种纵波,纵波是质点的振动方向与传播方向同轴的波.如敲锣时,锣的振动方向与波的传播方向就是一致的,所以声波是纵波.纵波是波动的一 ...

  6. 温习SQL语句

    作为一名使用C#语言开发人员,就很难逃脱与SQLSERVER打交道,虽说我们是开发人员,但我想说的是,对数据库的操作还是应该时不时的拿出来温习一番.下面那就是我见过的一道有趣的SQL题目,与你们一起分 ...

  7. Spring课程 Spring入门篇 1-2Spring简介

    课程链接: 1 Spring是什么? 2 为什么是Spring 3 Spring的作用: 4 适用范围 1 Spring是什么? a 开源框架 b 轻量级的控制反转(Ioc)和面向切面编程(AOP)的 ...

  8. ZR国庆Round2解题报告

    心路历程 预计得分:100 + 10 - 20 + 10 = 120 实际得分:100 + 0 + 10 = 110 感觉这场打的挺稳的.开场秒掉A题,写+调差不多1h 然后刚T3暴力,刚完还有2h左 ...

  9. EditText中光标的位置设置

    CharSequence text = userName.getText();              if (text instanceof Spannable) {                ...

  10. iis 部署网站常见问题

    环境IIS6.0,win2003server 首先打开iis,左边菜单选中”Web服务扩展“,讲Active Server Pages 和 ASP.NET v2.0.50727设置为允许 1.创建网站 ...