zoukankan      html  css  js  c++  java
  • java 分页对象以及数据库分页查询

    import java.util.List;
    
    public class Pager<T> {
        
        private Integer pageSize;
        
        private Integer totalRecord;
        
        private Integer totalPage;
        
        private Integer currentPage;
        
        private List<T> list;
        
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getTotalRecord() {
            return totalRecord;
        }
    
        public void setTotalRecord(Integer totalRecord) {
            this.totalRecord = totalRecord;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public List<T> getList() {
            return list;
        }
    
        public void setList(List<T> list) {
            this.list = list;
        }
    
    
        public Pager(Integer pageNo,Integer pageSize,List<T> sourceList){
             if(sourceList==null){
                 return;
             }
             
             //总记录数
             this.totalRecord = sourceList.size();
             
             //每页显示多小条数据
             this.pageSize = pageSize;
             
             //总页数
             this.totalPage = this.totalRecord % this.pageSize == 0?this.totalRecord/this.pageSize:this.totalRecord/this.pageSize+1;
             
             //当前第几页
             if(this.totalPage < pageNo){
                 this.currentPage = this.totalPage;
             }else{
                 this.currentPage = pageNo;
             }
             
             
             //起始索引 将此字段作为数据库分页查询的开始索引
             Integer startIndex = this.pageSize * (this.currentPage - 1);
    }

    MySQL数据库实现分页查询的SQL语句写法!

    一:分页需求:

    客户端通过传递start(页码),limit(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道MySql数据库提供了分页的函数limit m,n,但是该函数的用法和我们的需求不一样,所以就需要我们根据实际情况去改写适合我们自己的分页语句,具体的分析如下:

    比如:

    查询第1条到第10条的数据的sql是:select * from table limit 0,10;   ->对应我们的需求就是查询第一页的数据:select * from table limit (1-1)*10,10;

    查询第10条到第20条的数据的sql是:select * from table limit 10,20;  ->对应我们的需求就是查询第二页的数据:select * from table limit (2-1)*10,10;

    查询第20条到第30条的数据的sql是:select * from table limit 20,30;  ->对应我们的需求就是查询第三页的数据:select * from table limit (3-1)*10,10;

    二:通过上面的分析,可以得出符合我们自己需求的分页sql格式是:select * from table limit (currentPage-1)*limit,limit; 其中currentPage是页码,limit是每页显示的条数。


      

  • 相关阅读:
    方法
    数组
    Scanner类+Random
    运算符2
    运算符1
    Linux中Oracle的安装
    redis安装常见错误
    redis常用命令
    Linux中redis安装
    修改Oracle字符集
  • 原文地址:https://www.cnblogs.com/Andrew520/p/9934616.html
Copyright © 2011-2022 走看看