1、在pom.xml配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2、设置PageHelper初始化,可以采用两种方式
a)在application.properties内
#pagehelper.helperDialect= mysql
#pagehelper.row-bounds-with-count=true
#pagehelper.reasonable=true
#pagehelper.support-methods-arguments=true
#pagehelper.params=count=countSql
b)在代码内进行设置
@Configuration //这个注解不能忘记
public class PageHelperConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
//数据库
p.setProperty("helperDialect", "mysql");
/**
*该参数默认为false
*设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
*和startPage中的pageNum效果一样
*/
p.setProperty("offsetAsPageNum", "true");
/**
*该参数默认为false
*设置为true时,使用RowBounds分页会进行count查询
*/
p.setProperty("rowBoundsWithCount", "true");
/**
*3.3.0版本可用 - 分页参数合理化,默认false禁用
*启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
*禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据
*/
p.setProperty("reasonable", "true");
p.setProperty("params","count=countSql");
pageHelper.setProperties(p);
return pageHelper;
}
}
3、代码内进行分页
@Override
public Page<WhiteIp> findByPage(int pageNo, int pageSize) {
try {
PageHelper.startPage(pageNo, pageSize);
return whiteIpMapper.findByPage();
}catch (Exception e){
throw new ServiceException(e.getMessage());
}
}
返回的是Page接口,可以得到分页基础信息
private int pageNum;
private int pageSize;
private int startRow;
private int endRow;
private long total;
private int pages;
private boolean count;
private Boolean reasonable;
private Boolean pageSizeZero;
private String countColumn;
private String orderBy;
private boolean orderByOnly;
网友评论