springboot2集成pagehelper超级简单,本示例直接抄袭官方示例,仅将数据库由H2改成MySQL而已。

1、 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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>qinfeng.zheng</groupId>
<artifactId>learn-pagequery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learn-pagequery</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<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>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2. application.peroperties

#pagehelper
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true #mysql
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://120.79.xx.xx:3306/test?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 1212212

3. 实体类

public class Country implements Serializable {
private static final long serialVersionUID = 6569081236403751407L; private int id;
private String countryname;
private String countrycode; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCountryname() {
return countryname;
} public void setCountryname(String countryname) {
this.countryname = countryname;
} public String getCountrycode() {
return countrycode;
} public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}

4,mapper接口类

@Mapper
public interface CountryMapper {
@Select("select * from country")
List<Country> findAll(); }

5. cotroller类

@RestController
public class CountryController {
@Autowired
private CountryMapper countryMapper; @GetMapping("/findAll")
public List<Country> findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Country> countries = countryMapper.findAll(); Page page = (Page) countries;
System.out.println("每页展示条数:" + page.getPageSize());
System.out.println("总条数:" + page.getTotal());
System.out.println("当前页:" + page.getPageNum());
System.out.println("总页数:" + page.getPages()); return countries;
}
}

7. 测试数据

  直接抄官方数据

  

drop table country if exists;

create table country (
id int primary key auto_increment,
countryname varchar(32),
countrycode varchar(2)
); insert into country (id, countryname, countrycode) values(1,'Angola','AO');
insert into country (id, countryname, countrycode) values(2,'Afghanistan','AF');
insert into country (id, countryname, countrycode) values(3,'Albania','AL');
insert into country (id, countryname, countrycode) values(4,'Algeria','DZ');
insert into country (id, countryname, countrycode) values(5,'Andorra','AD');
insert into country (id, countryname, countrycode) values(6,'Anguilla','AI');
insert into country (id, countryname, countrycode) values(7,'Antigua and Barbuda','AG');
insert into country (id, countryname, countrycode) values(8,'Argentina','AR');
insert into country (id, countryname, countrycode) values(9,'Armenia','AM');
insert into country (id, countryname, countrycode) values(10,'Australia','AU');
insert into country (id, countryname, countrycode) values(11,'Austria','AT');
insert into country (id, countryname, countrycode) values(12,'Azerbaijan','AZ');
insert into country (id, countryname, countrycode) values(13,'Bahamas','BS');
insert into country (id, countryname, countrycode) values(14,'Bahrain','BH');
insert into country (id, countryname, countrycode) values(15,'Bangladesh','BD');
insert into country (id, countryname, countrycode) values(16,'Barbados','BB');
insert into country (id, countryname, countrycode) values(17,'Belarus','BY');
insert into country (id, countryname, countrycode) values(18,'Belgium','BE');
insert into country (id, countryname, countrycode) values(19,'Belize','BZ');
insert into country (id, countryname, countrycode) values(20,'Benin','BJ');
insert into country (id, countryname, countrycode) values(21,'Bermuda Is.','BM');
insert into country (id, countryname, countrycode) values(22,'Bolivia','BO');
insert into country (id, countryname, countrycode) values(23,'Botswana','BW');
insert into country (id, countryname, countrycode) values(24,'Brazil','BR');
insert into country (id, countryname, countrycode) values(25,'Brunei','BN');
insert into country (id, countryname, countrycode) values(26,'Bulgaria','BG');
insert into country (id, countryname, countrycode) values(27,'Burkina-faso','BF');
insert into country (id, countryname, countrycode) values(28,'Burma','MM');
insert into country (id, countryname, countrycode) values(29,'Burundi','BI');
insert into country (id, countryname, countrycode) values(30,'Cameroon','CM');
insert into country (id, countryname, countrycode) values(31,'Canada','CA');
insert into country (id, countryname, countrycode) values(32,'Central African Republic','CF');
insert into country (id, countryname, countrycode) values(33,'Chad','TD');
insert into country (id, countryname, countrycode) values(34,'Chile','CL');
insert into country (id, countryname, countrycode) values(35,'China','CN');
insert into country (id, countryname, countrycode) values(36,'Colombia','CO');
insert into country (id, countryname, countrycode) values(37,'Congo','CG');
insert into country (id, countryname, countrycode) values(38,'Cook Is.','CK');
insert into country (id, countryname, countrycode) values(39,'Costa Rica','CR');
insert into country (id, countryname, countrycode) values(40,'Cuba','CU');
insert into country (id, countryname, countrycode) values(41,'Cyprus','CY');
insert into country (id, countryname, countrycode) values(42,'Czech Republic','CZ');
insert into country (id, countryname, countrycode) values(43,'Denmark','DK');
insert into country (id, countryname, countrycode) values(44,'Djibouti','DJ');
insert into country (id, countryname, countrycode) values(45,'Dominica Rep.','DO');
insert into country (id, countryname, countrycode) values(46,'Ecuador','EC');
insert into country (id, countryname, countrycode) values(47,'Egypt','EG');
insert into country (id, countryname, countrycode) values(48,'EI Salvador','SV');
insert into country (id, countryname, countrycode) values(49,'Estonia','EE');
insert into country (id, countryname, countrycode) values(50,'Ethiopia','ET');
insert into country (id, countryname, countrycode) values(51,'Fiji','FJ');
insert into country (id, countryname, countrycode) values(52,'Finland','FI');
insert into country (id, countryname, countrycode) values(53,'France','FR');
insert into country (id, countryname, countrycode) values(54,'French Guiana','GF');
insert into country (id, countryname, countrycode) values(55,'Gabon','GA');
insert into country (id, countryname, countrycode) values(56,'Gambia','GM');
insert into country (id, countryname, countrycode) values(57,'Georgia','GE');
insert into country (id, countryname, countrycode) values(58,'Germany','DE');
insert into country (id, countryname, countrycode) values(59,'Ghana','GH');
insert into country (id, countryname, countrycode) values(60,'Gibraltar','GI');
insert into country (id, countryname, countrycode) values(61,'Greece','GR');
insert into country (id, countryname, countrycode) values(62,'Grenada','GD');
insert into country (id, countryname, countrycode) values(63,'Guam','GU');
insert into country (id, countryname, countrycode) values(64,'Guatemala','GT');
insert into country (id, countryname, countrycode) values(65,'Guinea','GN');
insert into country (id, countryname, countrycode) values(66,'Guyana','GY');
insert into country (id, countryname, countrycode) values(67,'Haiti','HT');
insert into country (id, countryname, countrycode) values(68,'Honduras','HN');
insert into country (id, countryname, countrycode) values(69,'Hongkong','HK');
insert into country (id, countryname, countrycode) values(70,'Hungary','HU');
insert into country (id, countryname, countrycode) values(71,'Iceland','IS');
insert into country (id, countryname, countrycode) values(72,'India','IN');
insert into country (id, countryname, countrycode) values(73,'Indonesia','ID');
insert into country (id, countryname, countrycode) values(74,'Iran','IR');
insert into country (id, countryname, countrycode) values(75,'Iraq','IQ');
insert into country (id, countryname, countrycode) values(76,'Ireland','IE');
insert into country (id, countryname, countrycode) values(77,'Israel','IL');
insert into country (id, countryname, countrycode) values(78,'Italy','IT');
insert into country (id, countryname, countrycode) values(79,'Jamaica','JM');
insert into country (id, countryname, countrycode) values(80,'Japan','JP');
insert into country (id, countryname, countrycode) values(81,'Jordan','JO');
insert into country (id, countryname, countrycode) values(82,'Kampuchea (Cambodia )','KH');
insert into country (id, countryname, countrycode) values(83,'Kazakstan','KZ');
insert into country (id, countryname, countrycode) values(84,'Kenya','KE');
insert into country (id, countryname, countrycode) values(85,'Korea','KR');
insert into country (id, countryname, countrycode) values(86,'Kuwait','KW');
insert into country (id, countryname, countrycode) values(87,'Kyrgyzstan','KG');
insert into country (id, countryname, countrycode) values(88,'Laos','LA');
insert into country (id, countryname, countrycode) values(89,'Latvia','LV');
insert into country (id, countryname, countrycode) values(90,'Lebanon','LB');
insert into country (id, countryname, countrycode) values(91,'Lesotho','LS');
insert into country (id, countryname, countrycode) values(92,'Liberia','LR');
insert into country (id, countryname, countrycode) values(93,'Libya','LY');
insert into country (id, countryname, countrycode) values(94,'Liechtenstein','LI');
insert into country (id, countryname, countrycode) values(95,'Lithuania','LT');
insert into country (id, countryname, countrycode) values(96,'Luxembourg','LU');
insert into country (id, countryname, countrycode) values(97,'Macao','MO');
insert into country (id, countryname, countrycode) values(98,'Madagascar','MG');
insert into country (id, countryname, countrycode) values(99,'Malawi','MW');
insert into country (id, countryname, countrycode) values(100,'Malaysia','MY');
insert into country (id, countryname, countrycode) values(101,'Maldives','MV');
insert into country (id, countryname, countrycode) values(102,'Mali','ML');
insert into country (id, countryname, countrycode) values(103,'Malta','MT');
insert into country (id, countryname, countrycode) values(104,'Mauritius','MU');
insert into country (id, countryname, countrycode) values(105,'Mexico','MX');
insert into country (id, countryname, countrycode) values(106,'Moldova, Republic of','MD');
insert into country (id, countryname, countrycode) values(107,'Monaco','MC');
insert into country (id, countryname, countrycode) values(108,'Mongolia','MN');
insert into country (id, countryname, countrycode) values(109,'Montserrat Is','MS');
insert into country (id, countryname, countrycode) values(110,'Morocco','MA');
insert into country (id, countryname, countrycode) values(111,'Mozambique','MZ');
insert into country (id, countryname, countrycode) values(112,'Namibia','NA');
insert into country (id, countryname, countrycode) values(113,'Nauru','NR');
insert into country (id, countryname, countrycode) values(114,'Nepal','NP');
insert into country (id, countryname, countrycode) values(115,'Netherlands','NL');
insert into country (id, countryname, countrycode) values(116,'New Zealand','NZ');
insert into country (id, countryname, countrycode) values(117,'Nicaragua','NI');
insert into country (id, countryname, countrycode) values(118,'Niger','NE');
insert into country (id, countryname, countrycode) values(119,'Nigeria','NG');
insert into country (id, countryname, countrycode) values(120,'North Korea','KP');
insert into country (id, countryname, countrycode) values(121,'Norway','NO');
insert into country (id, countryname, countrycode) values(122,'Oman','OM');
insert into country (id, countryname, countrycode) values(123,'Pakistan','PK');
insert into country (id, countryname, countrycode) values(124,'Panama','PA');
insert into country (id, countryname, countrycode) values(125,'Papua New Cuinea','PG');
insert into country (id, countryname, countrycode) values(126,'Paraguay','PY');
insert into country (id, countryname, countrycode) values(127,'Peru','PE');
insert into country (id, countryname, countrycode) values(128,'Philippines','PH');
insert into country (id, countryname, countrycode) values(129,'Poland','PL');
insert into country (id, countryname, countrycode) values(130,'French Polynesia','PF');
insert into country (id, countryname, countrycode) values(131,'Portugal','PT');
insert into country (id, countryname, countrycode) values(132,'Puerto Rico','PR');
insert into country (id, countryname, countrycode) values(133,'Qatar','QA');
insert into country (id, countryname, countrycode) values(134,'Romania','RO');
insert into country (id, countryname, countrycode) values(135,'Russia','RU');
insert into country (id, countryname, countrycode) values(136,'Saint Lueia','LC');
insert into country (id, countryname, countrycode) values(137,'Saint Vincent','VC');
insert into country (id, countryname, countrycode) values(138,'San Marino','SM');
insert into country (id, countryname, countrycode) values(139,'Sao Tome and Principe','ST');
insert into country (id, countryname, countrycode) values(140,'Saudi Arabia','SA');
insert into country (id, countryname, countrycode) values(141,'Senegal','SN');
insert into country (id, countryname, countrycode) values(142,'Seychelles','SC');
insert into country (id, countryname, countrycode) values(143,'Sierra Leone','SL');
insert into country (id, countryname, countrycode) values(144,'Singapore','SG');
insert into country (id, countryname, countrycode) values(145,'Slovakia','SK');
insert into country (id, countryname, countrycode) values(146,'Slovenia','SI');
insert into country (id, countryname, countrycode) values(147,'Solomon Is','SB');
insert into country (id, countryname, countrycode) values(148,'Somali','SO');
insert into country (id, countryname, countrycode) values(149,'South Africa','ZA');
insert into country (id, countryname, countrycode) values(150,'Spain','ES');
insert into country (id, countryname, countrycode) values(151,'Sri Lanka','LK');
insert into country (id, countryname, countrycode) values(152,'St.Lucia','LC');
insert into country (id, countryname, countrycode) values(153,'St.Vincent','VC');
insert into country (id, countryname, countrycode) values(154,'Sudan','SD');
insert into country (id, countryname, countrycode) values(155,'Suriname','SR');
insert into country (id, countryname, countrycode) values(156,'Swaziland','SZ');
insert into country (id, countryname, countrycode) values(157,'Sweden','SE');
insert into country (id, countryname, countrycode) values(158,'Switzerland','CH');
insert into country (id, countryname, countrycode) values(159,'Syria','SY');
insert into country (id, countryname, countrycode) values(160,'Taiwan','TW');
insert into country (id, countryname, countrycode) values(161,'Tajikstan','TJ');
insert into country (id, countryname, countrycode) values(162,'Tanzania','TZ');
insert into country (id, countryname, countrycode) values(163,'Thailand','TH');
insert into country (id, countryname, countrycode) values(164,'Togo','TG');
insert into country (id, countryname, countrycode) values(165,'Tonga','TO');
insert into country (id, countryname, countrycode) values(166,'Trinidad and Tobago','TT');
insert into country (id, countryname, countrycode) values(167,'Tunisia','TN');
insert into country (id, countryname, countrycode) values(168,'Turkey','TR');
insert into country (id, countryname, countrycode) values(169,'Turkmenistan','TM');
insert into country (id, countryname, countrycode) values(170,'Uganda','UG');
insert into country (id, countryname, countrycode) values(171,'Ukraine','UA');
insert into country (id, countryname, countrycode) values(172,'United Arab Emirates','AE');
insert into country (id, countryname, countrycode) values(173,'United Kiongdom','GB');
insert into country (id, countryname, countrycode) values(174,'United States of America','US');
insert into country (id, countryname, countrycode) values(175,'Uruguay','UY');
insert into country (id, countryname, countrycode) values(176,'Uzbekistan','UZ');
insert into country (id, countryname, countrycode) values(177,'Venezuela','VE');
insert into country (id, countryname, countrycode) values(178,'Vietnam','VN');
insert into country (id, countryname, countrycode) values(179,'Yemen','YE');
insert into country (id, countryname, countrycode) values(180,'Yugoslavia','YU');
insert into country (id, countryname, countrycode) values(181,'Zimbabwe','ZW');
insert into country (id, countryname, countrycode) values(182,'Zaire','ZR');
insert into country (id, countryname, countrycode) values(183,'Zambia','ZM');

好,一切准备就绪,启动springboot项目,浏览器访问,注意controller中打印

第一次请求:

controller打印:

第二次请求:

controller打印:

OK ,验证完毕! 完美!!!

补充:

  在做分页功能时,前端分页插件一般都需要一些诸如当前页数,总条数,总共多少页之类的数据,这时可以引用PageInfo这个对象,它完全能够满足插件的分页要求。

 @GetMapping("/findAll")
public PageInfo<Country> findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Country> countries = countryMapper.findAll(); // Page page = (Page) countries;
// System.out.println("每页展示条数:" + page.getPageSize());
// System.out.println("总条数:" + page.getTotal());
// System.out.println("当前页:" + page.getPageNum());
// System.out.println("总页数:" + page.getPages());
PageInfo<Country> result = new PageInfo<>(countries);
return result;
}

请求测试:

查看PageInfo类的源码,不难发现,它对前端分页插件的属性支持还是很全面的

    private int pageNum;  // 当前页码
private int pageSize; // 每页展示的多少条数据
private int size; // 数据总条数
private int startRow;
private int endRow;
private int pages; // 总共多少页
private int prePage; // 前一页
private int nextPage; // 后一页
private boolean isFirstPage;
private boolean isLastPage;
private boolean hasPreviousPage;
private boolean hasNextPage;
private int navigatePages;
private int[] navigatepageNums;
private int navigateFirstPage;
private int navigateLastPage;

好, 暂时就到这里吧,以后再补充!

springboot2集成pagehelper的更多相关文章

  1. Activiti(二) springBoot2集成activiti,集成activiti在线设计器

    摘要 本篇随笔主要记录springBoot2集成activiti流程引擎,并且嵌入activiti的在线设计器,可以通过浏览器直接编辑出我们需要的流程,不需要通过eclipse或者IDEA的actiB ...

  2. SpringBoot集成PageHelper时出现“在系统中发现了多个分页插件,请检查系统配置!”

    近日在项目中使用SpringBoot集成PageHelper后,跑单元测试时出现了"在系统中发现了多个分页插件,请检查系统配置!"这个问题. 如下图所示: org.mybatis. ...

  3. SpringBoot项目集成PageHelper使用

    SpringBoot项目集成PageHelper使用 一.开始 ​ 地址:https://github.com/pagehelper/Mybatis-PageHelper ​ 在spring boot ...

  4. UidGenerator springboot2集成篇

    uid-generator 官网集成文档: https://github.com/baidu/uid-generator/blob/master/README.zh_cn.md 由于并没有提供spri ...

  5. springboot集成PageHelper,支持springboot2.0以上版本

    第一步:pom文件还是需要引入依赖 <!--mybatis的分页插件--> <dependency> <groupId>com.github.pagehelper& ...

  6. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  7. SpringBoot2 集成三种连接池 c3p0 hikari druid

    Hikari 1.首先集成 hikari springboot默认集成,只需要简单的配置即可 1.1 首先导入包 <dependency> <groupId>com.zaxxe ...

  8. spring-boot集成PageHelper和通用Mapper

    前提条件:已经集成mybatis 代码生成步骤: 添加依赖 <dependency> <groupId>tk.mybatis</groupId> <artif ...

  9. SpringBoot2 集成日志,复杂业务下的自定义实现

    本文源码:GitHub·点这里 || GitEE·点这里 一.日志体系集成 1.日志管理 在系统的开发中,最关键的一个组件工具就是日志,日志打印方便问题排查,或者生产事故回溯,日志记录用来监控并分析系 ...

随机推荐

  1. 《图解设计模式》读书笔记1-1 Iterator模式

    目录 迭代器模式的类图 类图的解释 迭代器模式的代码 解释 原因 思想 迭代器模式的类图 类图的解释 名称 说明 Aggregate 集合接口,有提供迭代器的方法 Iterator 迭代器接口,提供迭 ...

  2. 【转】时间序列分析——基于R,王燕

    <时间序列分析——基于R>王燕,读书笔记 笔记: 一.检验: 1.平稳性检验: 图检验方法:     时序图检验:该序列有明显的趋势性或周期性,则不是平稳序列     自相关图检验:(ac ...

  3. C++ 测量程序执行时间的办法

    #include <time.h> clock_t start = clock(); //时间起始 /*待测试代码*/ clock_t end = clock(); //时间测试结束 co ...

  4. Vagrant 手册之 Vagrantfile - Vagrant 设置 config.vagrant

    原文地址 配置的命名空间:config.vagrant config.vagrant 中的设置修改 Vagrant 自身的行为. 1. 可用设置 config.vagrant.host 设置运行 Va ...

  5. vue+element-ui国际化(i18n)

    1. 下载element-ui和vue-i18n: npm i element-ui --save   npm i vue-i18n –save 2.  创建一个  i18n 文件夹, 在main.j ...

  6. Linux命令整理-Kali

    网络相关 桥接模式下无法联网:设置桥接网卡地址为指定网卡(如dual band) route -n 查看网关/子网掩码 虚拟机中屏幕太小-设置中调节分辨率 DNS配置:cat /etc/resolv. ...

  7. cannot open shared object file: No such file or directory解决

    cannot open shared object file: No such file or directory解决   ./move_db: error while loading shared ...

  8. CSS世界中的“盒子”

    1.块级元素 HTML标签通常被分为两类:块级元素和内联元素. “块级元素”和“display为block的元素”不是同一个概念.例如<li>元素默认的display值为list-item ...

  9. tensorflow实现一个神经网络简单CNN网络

    本例子用到了minst数据库,通过训练CNN网络,实现手写数字的预测. 首先先把数据集读取到程序中(MNIST数据集大约12MB,如果没在文件夹中找到就会自动下载): mnist = input_da ...

  10. CGAffineTransform 图像处理类

    CGAffineTransform 介绍 概述 CGAffineTransform是一个用于处理形变的类,其可以改变控件的平移.缩放.旋转等,其坐标系统采用的是二维坐标系,即向右为x轴正方向,向下为y ...