1.首先先导入pom.xml文件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2.往
配置中添加以下配置(都是默认的不用修改):
# 配置pagehelper
pagehelper.offsetAsPageNum=true
pagehelper.rowBoundsWithCount=true
pagehelper.pageSizeZero=true
pagehelper.reasonable=false
pagehelper.params=pageNum=pageHelperStart;pageSize=pageHelperRows;
pagehelper.supportMethodsArguments=false
3.添加一个工具类内容如下(不用修改):
public class PageResult implements Serializable {
// {total:1000, rows : [{},{}]}
/** 总记录数 */
private long total;
/** 分页数据 */
private List<?> rows;
public PageResult(){}
public PageResult(long total, List<?> rows) {
this.total = total;
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
4.随便的写了一个简单模糊查询(这是我自己的查询语句你们自己写自己的)
5.然后去
配置一下数据(这里也是自己的内容)内容如下:
6.然后去
这里的返回PageResult就是第 3 步骤的类名
7.然后去
这里内容如下(这里就是你的逻辑代码了最核心的地方):
这里因为我的字段比较多所以代码比较多
public PageResult findAll2(String userName, String characterName, String institutionalCode, String phoneNumber, String channel,Integer currentPage, Integer pageVolume) {
try {
//蓝色这里都是if判断所以你们可以只写一个
if(userName==null || "".equals(userName) || "null".equals(userName)){
userName="";
}
if(characterName==null || "".equals(characterName) || "null".equals(characterName)){
characterName="";
}
if(institutionalCode==null || "".equals(institutionalCode) || "null".equals(institutionalCode)){
institutionalCode="";
}
if(phoneNumber==null || "".equals(phoneNumber) || "null".equals(phoneNumber)){
phoneNumber="";
}
if(channel==null || "".equals(channel) || "null".equals(channel)){
channel="";
}
if(currentPage==null || "".equals(currentPage) || "null".equals(currentPage) || currentPage==0){
currentPage=1;
}
if(pageVolume==null || "".equals(pageVolume) || "null".equals(pageVolume) || pageVolume==0){
pageVolume=1;
}
//紫色这里就是把从外面传进来的参数赋给一个确定的数
String finalUserName = userName;
String finalCharacterName = characterName;
String finalInstitutionalCode = institutionalCode;
String finalPhoneNumber = phoneNumber;
String finalChannel = channel;
// 开启分页 //这里就是你传进来的当前页数 和 每页的大小
PageInfo<User> pageInfo = PageHelper.startPage(currentPage, pageVolume)
.doSelectPageInfo(new ISelect() {
@Override
public void doSelect() {
//这里就是紫色哪里的确定值,usermapper是dao城的接口 userMapper.findAll2(finalUserName, finalCharacterName, finalInstitutionalCode, finalPhoneNumber, finalChannel);
}
}); //这两个步骤3中的get方法 第一个就是总页数 第二个就是数据
return new PageResult(pageInfo.getTotal(), pageInfo.getList());
}catch (Exception e){
throw new RuntimeException(e);
}
}
8.然后去
这里就比较简单了(这里也是自己的内容)
然后就完事了