温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot2中如何集成pagehelper

发布时间:2022-04-06 15:58:32 阅读:156 作者:iii 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“Springboot2中如何集成pagehelper”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Springboot2中如何集成pagehelper”文章能帮助大家解决问题。

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

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

第一次请求:

Springboot2中如何集成pagehelper

controller打印:

Springboot2中如何集成pagehelper

第二次请求:

Springboot2中如何集成pagehelper

controller打印:

Springboot2中如何集成pagehelper

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

补充:

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

@GetMapping("/findAll")
  public PageInfo<CountryfindAll(@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;
  }

请求测试:

Springboot2中如何集成pagehelper

查看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”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://www.jb51.net/web/56607.html

AI

开发者交流群×