PageHelper在springboot+mybatis框架中如何使用
一、思路将分页所需的内容都放到一个实体类中分页数据所需要的实体类!内包含页码,页大小,总条数,总页数,起始行pagehelpr提供了这个类pageInfo,不需要我们自己创建二、主要逻辑select*from表名limit起始行,展示几条数据#第n页每页展示五条数据select*from表名limit(n-1)*5,5#每页展示多少条pageSize3#总共有多少条totalselectcount(*)from表名#总页数pagespages=total%pagesSize==0?total/pgeSiz
一、思路
将分页所需的内容都放到一个实体类中
分页数据所需要的实体类!内包含页码,页大小,总条数,总页数,起始行
pagehelpr提供了这个类 pageInfo,不需要我们自己创建
二、主要逻辑
select * from 表名 limit 起始行,展示几条数据
#第n页 每页展示五条数据
select * from 表名 limit (n-1)*5,5
#每页展示多少条 pageSize
3
#总共有多少条
total
select count(*) from 表名
#总页数
pages
pages=total%pagesSize==0?total/pgeSize:total/pageSize+1;
#当前页
pageNum
三、步骤
1.引入pagehelper依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.2.5
#pagehelper分页插件配置 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql
2.bean实体类
用户实体:
package com.qianfeng.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Register {
private Integer id;
private String userName;
private String passWord;
private String rePassWord;
private String idCard;
private String gender;
}返回前端的实体类:包括查到的数据和分页数据
package com.qianfeng.bean;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.List;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class RegPage {
private PageInfo pageInfo;
private List registers;
} 2.mapper层:
package com.qianfeng.mapper;
import com.qianfeng.bean.Register;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PageDao {
List getAll(Integer startRow,Integer pageSize);
long getCount();
} 3.service层:
package com.qianfeng.service;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
public interface RegService {
RegPage getAll(PageInfo pageInfo);
}4.serviceImpl:
package com.qianfeng.service.serviceImpl;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
import com.qianfeng.bean.Register;
import com.qianfeng.mapper.PageDao;
import com.qianfeng.service.RegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RegServiceImpl implements RegService {
@Autowired
private PageDao pageDao;
@Override
public RegPage getAll(PageInfo pageInfo) {
List all = pageDao.getAll(pageInfo.getStartRow(), pageInfo.getPageSize());//分页后的数据
long count = pageDao.getCount();//总记录条数
pageInfo.setTotal(count);
//总页数
int pages= (int) (pageInfo.getTotal()%pageInfo.getPageSize()==0?pageInfo.getTotal()/pageInfo.getPageSize():pageInfo.getTotal()/pageInfo.getPageSize()+1);
pageInfo.setPages(pages);
RegPage regPage = new RegPage();
regPage.setPageInfo(pageInfo);
regPage.setRegisters(all);
return regPage;
}
} 5.handler层:
package com.qianfeng.handler;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
import com.qianfeng.service.RegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RegHandler {
@Autowired
private RegService regService;
@RequestMapping("/page/{pageNum}")
public RegPage regPage(@PathVariable("pageNum") Integer pageNum){
System.out.println(".........");
PageInfo pageInfo = new PageInfo();
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(3);
pageInfo.setStartRow((pageNum-1)*pageInfo.getPageSize());
System.out.println("startRow" + pageInfo.getStartRow());
return regService.getAll(pageInfo);
}
}6.mapper.xml
7.application.yaml
spring: datasource: url: jdbc:mysql:///map?serverTimezone=Asia/Shanghai&useSSL=false username: root password: 123 driver-class-name: com.mysql.jdbc.Driver druid: aop-patterns: com.qianfeng.* #监控SpringBean filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙) stat-view-servlet: # 配置监控页功能 enabled: true login-username: admin login-password: admin resetEnable: false web-stat-filter: # 监控web enabled: true urlPattern: /* exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' filter: stat: # 对上面filters里面的stat的详细配置 slow-sql-millis: 1000 logSlowSql: true enabled: true wall: enabled: true config: drop-table-allow: false mvc: pathmatch: matching-strategy: ant_path_matcher # mybatis的配置规则 mybatis: #config-location: classpath:mapper/mybatis-config.xml mapper-locations: classpath:mapper/* configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # static-locations: [classpath:/haha/] # 静态资源路径自定义
8.application,properties
spring.main.allow-circular-references=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql
关于PageInfo这个类,源码如下:
public class PageInfo implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow 和endRow 不常用,这里说个具体的用法
//可以在页面中"显示startRow 到endRow 共size 条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}目录结构:

Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















