springboot+pageHelper实现分页
1. 前置条件
阅读本文前假定你已知晓springboot的基本配置及使用。
2. 具体实施
2.1 项目搭建
本案例基于springboot2.1.13版本进行搭建,引入的依赖主要有Spring Boot DevTools、MyBatis Framework、MySQL Driver、Spring Web、druid、log4j等。
2.2 pageHelper的引入及配置
- 在pom.xml中添加如下依赖
<!--pageHelper插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
- 在application.yml中做如下配置
#pageHelper配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
2.3 在项目中使用
具体而言,pageHelper的使用是在service层进行的。我们一步步解析在案例中的具体使用。
2.3.1 数据表
CREATE TABLE `t_goods` (
`id` bigint(20) NOT NULL COMMENT '商品id',
`category_id` bigint(20) DEFAULT NULL COMMENT '分类id',
`item_type` varchar(100) DEFAULT NULL COMMENT '商品系列',
`title` varchar(100) DEFAULT NULL COMMENT '商品标题',
`sell_point` varchar(150) DEFAULT NULL COMMENT '商品卖点',
`price` bigint(20) DEFAULT NULL COMMENT '商品单价',
`num` int(10) DEFAULT NULL COMMENT '库存数量',
`barcode` varchar(30) DEFAULT NULL COMMENT '条形码',
`image` varchar(500) DEFAULT NULL COMMENT '图片路径',
`status` int(1) DEFAULT '1' COMMENT '商品状态 1:上架 2:下架 3:删除',
`priority` int(10) DEFAULT NULL COMMENT '显示优先级',
`created_time` datetime DEFAULT NULL COMMENT '创建时间',
&