SpringBoot实现分页(PageHelper插件的使用)

  • 首先我们需要在SpringBoot项目的pom.xml文件中加载PageHelper插件

  
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.3
  
  • 第二步配置PageHelper

这里可以有两种方法进行配置:

一种是在SpringBoot项目的启动类中使用java代码进行配置

第二种就是在application.yml中进行配置(两种配置都差不多,这里贴上第二种配置)

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

自定义一个PageInfo类(这一步是必须的用于把数据转为JSON类型,传输到前台)

package com.example.park.util;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public class PageInfo implements Serializable {
    //当前页
    private int pageNum;
    //每页的数量
    private int pageSize;
    //总记录数
    private long total;
    //总页数
    private int pages;
    //结果集
    private List list;
    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;


    public PageInfo() {
    }

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageInfo(List list) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.list = page;
            this.total = page.getTotal();
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();

            this.pages = 1;
            this.list = list;
            this.total = list.size();
        }
        if (list instanceof Collection) {
            //判断页面边界
            judgePageBoudary();
        }
    }

    /**
     * 判定页面边界
     */
    private void judgePageBoudary() {
        isFirstPage = pageNum == 1;
        isLastPage = pageNum == pages;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public boolean isIsFirstPage() {
        return isFirstPage;
    }

    public void setIsFirstPage(boolean isFirstPage) {
        this.isFirstPage = isFirstPage;
    }

    public boolean isIsLastPage() {
        return isLastPage;
    }

    public void setIsLastPage(boolean isLastPage) {
        this.isLastPage = isLastPage;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("PageInfo{");
        sb.append("pageNum=").append(pageNum);
        sb.append(", pageSize=").append(pageSize);
        sb.append(", total=").append(total);
        sb.append(", pages=").append(pages);
        sb.append(", list=").append(list);
        sb.append(", isFirstPage=").append(isFirstPage);
        sb.append(", isLastPage=").append(isLastPage);
        sb.append(", navigatepageNums=");
        sb.append('}');
        return sb.toString();
    }
}
  • 三步我们就可以使用PageHelper插件

Mapper.xml




    

Dao.java

@Mapper
public interface ParkInfoDao {
    Page getAll();
}

Service.java

public interface ParkInfoService {
    Page getAll(Integer pageNum, Integer pageSize);
}

ServiceImpl.java

@Service
public class ParkInfoServiceImpl implements ParkInfoService {

    @Resource
    private ParkInfoDao parkInfoDao;

    @Override
    public Page getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return parkInfoDao.getAll();
    }
}

测试分页获取数据

    @Resource
    private ParkInfoService parkInfoService;
    @Test
    public void contextLoads() {
        Page page =parkInfoService.getAll(1,2);
        PageInfo pageInfo = new PageInfo<>(page);
        String data = JSON.toJSONString(pageInfo);
        System.out.println(data);
    }

输出的数据为(供参考)

{
"isFirstPage":true,
"isLastPage":true,
"list":[{"address":"","comment":"测试数        据","freePlot":699,"orderPlot":10,"parkName":"","parkNo":1,"phone":"11111111","status":"使用中","totalPlot":999,"userPlot":300,"zoneCompany":""},
{"address":"","comment":"测试数据","freePlot":749,"orderPlot":10,"parkName":"","parkNo":2,"phone":"22222222","status":"使用中","totalPlot":999,"userPlot":250,"zoneCompany":""}],
"pageNum":1,
"pageSize":2,
"pages":1,
"total":2
}

 

你可能感兴趣的:(技术)