Springboot2中如何集成pagehelper
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.2.1.RELEASE</version> <relativepath></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(); }</country>
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; } }</country></country>
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; }</country></country></country>
请求测试:
查看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的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Jasypt介绍Jasypt是一个java库,它允许开发员以最少的努力为他/她的项目添加基本的加密功能,并且不需要对加密工作原理有深入的了解用于单向和双向加密的高安全性、基于标准的加密技术。加密密码,文本,数字,二进制文件...适合集成到基于Spring的应用程序中,开放API,用于任何JCE提供程序...添加如下依赖:com.github.ulisesbocchiojasypt-spring-boot-starter2.1.1Jasypt好处保护我们的系统安全,即使代码泄露,也可以保证数据源的

使用场景1、下单成功,30分钟未支付。支付超时,自动取消订单2、订单签收,签收后7天未进行评价。订单超时未评价,系统默认好评3、下单成功,商家5分钟未接单,订单取消4、配送超时,推送短信提醒……对于延时比较长的场景、实时性不高的场景,我们可以采用任务调度的方式定时轮询处理。如:xxl-job今天我们采

一、Redis实现分布式锁原理为什么需要分布式锁在聊分布式锁之前,有必要先解释一下,为什么需要分布式锁。与分布式锁相对就的是单机锁,我们在写多线程程序时,避免同时操作一个共享变量产生数据问题,通常会使用一把锁来互斥以保证共享变量的正确性,其使用范围是在同一个进程中。如果换做是多个进程,需要同时操作一个共享资源,如何互斥呢?现在的业务应用通常是微服务架构,这也意味着一个应用会部署多个进程,多个进程如果需要修改MySQL中的同一行记录,为了避免操作乱序导致脏数据,此时就需要引入分布式锁了。想要实现分

springboot读取文件,打成jar包后访问不到最新开发出现一种情况,springboot打成jar包后读取不到文件,原因是打包之后,文件的虚拟路径是无效的,只能通过流去读取。文件在resources下publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

在Springboot+Mybatis-plus不使用SQL语句进行多表添加操作我所遇到的问题准备工作在测试环境下模拟思维分解一下:创建出一个带有参数的BrandDTO对象模拟对后台传递参数我所遇到的问题我们都知道,在我们使用Mybatis-plus中进行多表操作是极其困难的,如果你不使用Mybatis-plus-join这一类的工具,你只能去配置对应的Mapper.xml文件,配置又臭又长的ResultMap,然后再去写对应的sql语句,这种方法虽然看上去很麻烦,但具有很高的灵活性,可以让我们

SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

1、自定义RedisTemplate1.1、RedisAPI默认序列化机制基于API的Redis缓存实现是使用RedisTemplate模板进行数据缓存操作的,这里打开RedisTemplate类,查看该类的源码信息publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations,BeanClassLoaderAware{//声明了key、value的各种序列化方式,初始值为空@NullableprivateRedisSe

在项目中,很多时候需要用到一些配置信息,这些信息在测试环境和生产环境下可能会有不同的配置,后面根据实际业务情况有可能还需要再做修改。我们不能将这些配置在代码中写死,最好是写到配置文件中,比如可以把这些信息写到application.yml文件中。那么,怎么在代码里获取或者使用这个地址呢?有2个方法。方法一:我们可以通过@Value注解的${key}即可获取配置文件(application.yml)中和key对应的value值,这个方法适用于微服务比较少的情形方法二:在实际项目中,遇到业务繁琐,逻
