MySql教程

Mysql分页、SSM项目分页实战

本文主要是介绍Mysql分页、SSM项目分页实战,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、Mysql分页

记住几个万年不变的公式就完了

(1)limit分页公式:limit (curPage-1)*pageSize,pageSize curPage是当前第几页;pageSize是一页多少条记录 sql语句: select * from student limit (curPage-1)*pageSize,pageSize;

(2)总页数公式: int totalPageNum = (totalRecord +pageSize - 1) / pageSize; totalRecord是总记录数;pageSize是一页分多少条记录

(3)总记录数: count( * ) sql语句: select count( * ) from student;

二、SSM项目分页实战

1、首先,我们先准备一个分页用到的工具类

public class PageUtil<T> {
          
   

    private int pageIndex;//当前页码
    private int pageSize; //页面大小
    private int totalcount; //总条数
    private int pageCount; //总页数
    private List<T> records; //分页的数据

    private int numberStart; //开始序号
    private int numberEnd; //结束序号
    private List<Integer> numbers=new ArrayList<Integer>(); //序号

    public PageUtil(int pageIndex, int pageSize, int totalcount, List<T> records) {
          
   
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalcount = totalcount;
        this.records = records;
        //人工计算总页数
        this.pageCount=(this.totalcount%this.pageSize==0)?(this.totalcount/this.pageSize):(this.totalcount/this.pageSize+1);

        //给序号赋值
        if(this.pageCount<=10){
          
   
            this.numberStart=1;
            this.numberEnd=this.pageCount;
        }else{
          
   
            this.numberStart=this.pageIndex-4;
            this.numberEnd=this.pageIndex+5;
            if(this.numberStart<1){
          
   
                this.numberStart=1;
                this.numberEnd=10;
            }
            if(this.numberEnd>this.pageCount){
          
   
                this.numberStart=this.pageCount-9;
                this.numberEnd=this.pageCount;
            }
        }
        for(int i=numberStart;i<=numberEnd;i++){
          
   
            numbers.add(i);
        }

    }

    public int getPageIndex() {
          
   
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
          
   
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
          
   
        return pageSize;
    }

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

    public int getTotalcount() {
          
   
        return totalcount;
    }

    public void setTotalcount(int totalcount) {
          
   
        this.totalcount = totalcount;
    }

    public int getPageCount() {
          
   
        return pageCount;
    }

    public void setPageCount(int pageCount) {
          
   
        this.pageCount = pageCount;
    }

    public List<T> getRecords() {
          
   
        return records;
    }

    public void setRecords(List<T> records) {
          
   
        this.records = records;
    }

    public int getNumberStart() {
          
   
        return numberStart;
    }

    public void setNumberStart(int numberStart) {
          
   
        this.numberStart = numberStart;
    }

    public int getNumberEnd() {
          
   
        return numberEnd;
    }

    public void setNumberEnd(int numberEnd) {
          
   
        this.numberEnd = numberEnd;
    }

    public List<Integer> getNumbers() {
          
   
        return numbers;
    }

    public void setNumbers(List<Integer> numbers) {
          
   
        this.numbers = numbers;
    }
}

2、看一下我们数据库的数据

3、运行我们的SSM项目,打开页面,请求Controller

4、看一下,我们的Controller

5、Service层就不带大家看了,直接来到持久层

6、返回到我们的页面,主要看参数那一块是怎么取值的,这个SSM项目没有用到Jsp页面,用到的是thymeleaf模板

这篇关于Mysql分页、SSM项目分页实战的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!