首页 > 其他分享 >SSM和Springboot中的分页实现

SSM和Springboot中的分页实现

时间:2022-11-24 18:00:57浏览次数:48  
标签:return Springboot pageSize int page SSM Integer public 分页

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";
    }

标签:return,Springboot,pageSize,int,page,SSM,Integer,public,分页
From: https://www.cnblogs.com/guozhiqiang/p/16922715.html

相关文章

  • springboot 项目打为war包
    1.springboot项目打包配置:首先在启动类目录下新增打包类  启动类继承自SpringBootServletInitializer方可正常部署至常规tomcat下,其主要能够起到web.xml的作用(web.xm......
  • 汇总:基于SSM封装的一个迅速开发项目的底层框架集成项目
    本章开始会记录封装的全过程,包括集成一些常用的框架工具集成、MVC架构的设计等(目录内容待更新……)下面就从零开始介绍开发的整个过程,首先先使用springboot创建maven项目,pom......
  • 关于事务-springboot多个service互相调用的事务处理方式
    遇到问题在一个service的方法A中,调用另一个service的方法B,方法A和方法B均存在数据库插入操作,并且@Transaction注解也都加了,但是当B方法中抛出异常时,A中的插入语句还是能够......
  • SpringBoot+Vue在Linux系统部署说明文档
    部署手册目录部署手册 11. 环境说明 22. SpringBoot项目打包为war包 23. 前端项目打包 54. 虚拟机安装jdk1.8,配置相关环境 55. 虚拟机安装MySQL8.0,配置相关环境 56. 虚拟......
  • SpringBoot整合mybatis plus报错:net.sf.jsqlparser.schema.Column, is available from
     Anattemptwasmadetocallthemethodnet.sf.jsqlparser.schema.Column.withColumnName(Ljava/lang/String;)Lnet/sf/jsqlparser/schema/Column;butitdoesnote......
  • springboot的 application-dev.yml配置
    server:port:8888servlet:context-path:/pshdhxcompression:enabled:truemime-types:application/javascript,applic......
  • springboot+vue+element-ui实现前后端的全部校验
    1、前端校验el-form的表单校验<el-form:model="ruleForm"status-icon:rules="rules"ref="ruleForm"label-width="100px"class="demo-ruleForm"><el-form-itemlabel......
  • SSM基础知识
    SSM整合前面我们已经把Mybatis、Spring和SpringMVC三个框架进行了学习,那现在我们把这三个框架整合在一起,来完成我们的业务功能开发,具体如何来整合,我们一步步来学习。流程......
  • 前端分页
     handleCurrentChangeDetail(val,datas){      this.detailPage.currentPage=val      letlist=datas&&datas.length?datas:thi......
  • springboot整合Elasticsearch
    springboot   2.2.5.RELEASEElasticsearch   7.17.6Elasticsearch下载地址https://www.elastic.co/cn/downloads/elasticsearch还有个大概的对应版本图: ......