SSM和Springboot中的分页实现

管理员

SpringBoot实现

引入依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
     <version>5.1.2</version>
</dependency>

实体封装

需要分页的实体继承该实体,用以接收分页的参数

public class BasePageReqListMsg {
    
    protected final static int SIZE = 20;
    protected final static int NO = 1;
 
    protected int pageNum=NO;
    protected int pageSize = SIZE;
 
    public static int getInitSize() {
        return SIZE;
    }
 
 
    public int getPageNum() {
        return pageNum;
    }
 
 
    public int getPageSize() {
        return pageSize;
    }
 
  
    public int getStartIndex() {
        return (getPageNum() - 1) * this.pageSize;
    }
    
    public int getEndIndex() {
        return getPageNum() * this.pageSize;
    }
}

封装返回分页实体

该实体将分页数据封装,返给前端

@Data
public class PageResult<T> {
 
    private Integer pageNum;
 
    private Integer pageSize;
 
    private Long total;
 
    private List<T> list;
 
    public PageResult() {
 
    }
 
    public PageResult(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }
 
    public static PageResult getResult(int pageNum, int pageSize, long total, List result) {
        return new PageResult(pageNum, pageSize, total, result);
    }
 
    public static PageResult getResult(int pageNum, int pageSize) {
        return new PageResult(pageNum, pageSize, 0L, new ArrayList());
    }
}

分页查询

public PageResult<User> queryByParamPageList(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<User> users=  userMapper.selectByParam(queryDTO);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), users);
    }

SSM实现

查询语句为
SELECT * FROM table WHERE 查询条件 ORDER BY 排序条件 LIMIT ((页码-1)*页大小),页大小;

第一步:实现分页功能

首先要创建一个page类

package com.lnsf.tmall.pojo;

public class Page {
    //总条数
    private Integer totalNumber;
    //总页数
    private Integer totalPage;
    //当前页数
    private Integer currentPage;
    //数据库中limit的参数,从第几条开始取
    private Integer dbIndex;
    //数据库中limit的参数,总共取几条
    private Integer dbNumber;
    //每页显示几条
    private Integer pageNumber=8;


    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(Integer totalNumber) {
        this.totalNumber = totalNumber;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
        this.count();
    }

    public void count(){
    /**
     * 根据总条数计算总页数
     */
        if (this.totalNumber%this.pageNumber>0){
    /**
     * 如果总条数小于每页显示的条数,那么总页数为1
     */
            if (this.totalNumber<this.pageNumber){
                this.totalPage=1;
            }
            else{
                this.totalPage=(this.totalNumber/this.pageNumber)+1;
            }

        }
        else { this.totalPage=this.totalNumber/this.pageNumber; }
    // 设置当前页数
    // 总页数小于当前页数,应将当前页数设置为总页数
        if(this.totalPage < this.currentPage) {
            this.currentPage = this.totalPage;
        }
    // 当前页数小于1设置为1
        if(this.currentPage < 1) {
            this.currentPage = 1;
        }
    /**
     * 设置limit参数
     */
        this.dbIndex = (this.currentPage-1)*this.pageNumber;//从第几条开始查询
        this.dbNumber = this.pageNumber;//每页显示的条数
    }





    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(Integer dbIndex) {
        this.dbIndex = dbIndex;
    }

    public Integer getDbNumber() {
        return dbNumber;
    }

    public void setDbNumber(Integer dbNumber) {
        this.dbNumber = dbNumber;
    }



    public Page() {

    }

    public Page(Integer totalNumber, Integer totalPage, Integer currentPage, Integer dbIndex, Integer dbNumber, Integer pageNumber) {
        this.totalNumber = totalNumber;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.dbIndex = dbIndex;
        this.dbNumber = dbNumber;
        this.pageNumber = pageNumber;
    }

    @Override
    public String toString() {
        return "Page{" +
                "totalNumber=" + totalNumber +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                ", dbIndex=" + dbIndex +
                ", dbNumber=" + dbNumber +
                ", pageNumber=" + pageNumber +
                '}';
    }
}

第二步:在usermapper.xml中创建sql语句

 List<User> page(@Param("page") Page page);

 Integer count();

xml:

  <!--查询所有用户,并分页显示-->
  <select id="page" parameterType="com.lnsf.tmall.pojo.Page" resultMap="BaseResultMap">
    SELECT * FROM user
    ORDER BY uid limit #{page.dbIndex},#{page.dbNumber}
  </select>

  <!--总条数-->
  <select id="count"  resultType="java.lang.Integer">
    SELECT count(*) FROM user
  </select>

第三步:service层:实现获取对应页数所展示的List

List<User> showByPage(Integer start, Page page);
impl:

   @Override
    public List<User> showByPage(Integer start, Page page) {
        Integer totalCount =userMapper.count();
        page.setTotalNumber(totalCount);
        page.setCurrentPage(start);
        List<User> users=userMapper.page(page);
        return users;
    }

第三步:controller层

  @RequestMapping(value = "users",method = RequestMethod.GET)
    public String allUsers(Model m,Integer start){
        if (start == null)
            start=1;
        Page page=new Page();
        List<User> users = userService.showByPage(start,page);
        m.addAttribute("page",page);
        m.addAttribute("us",users);
        return "admin/listUser";
    }