jdbc实现分页,需要前端传当前页码

1.封装一个公共实体类用于返回:实体数据,当前页,总页数,总条数,每页多少条

public class PageInfo<T> {
  
    //一页显示的记录数
    private int numPerPage=10; 
    //记录总数
    private int totalRows; 
    //总页数
    private int totalPages; 
    //当前页码
    private int currentPage; 
    
    private List<T> list;
    
    // 总数统计
    private T sumObj;
    
    
    
	
	public T getSumObj() {
		return sumObj;
	}
	public void setSumObj(T sumObj) {
		this.sumObj = sumObj;
	}
	public PageInfo() {     
        this.totalRows = 0;
        this.totalPages = 0;
        this.currentPage = 1;
    }
    public PageInfo(Integer currentPage, int totalRows) {
        if(currentPage==null)currentPage=1;
        setCurrentPage(currentPage);
        this.totalRows = totalRows;
    }
    public PageInfo(Integer currentPage){
        if(currentPage==null)currentPage=1;
        setCurrentPage(currentPage);
    }
    public int getCurrentPage() {
        return currentPage;
    } 

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage <= 0 ? 1 : currentPage;
    }

    public int getNumPerPage() {
        return numPerPage;
    }

    public void setNumPerPage(int numPerPage) {
        this.numPerPage = numPerPage;
    }

    public int getTotalPages() {
        if (this.totalPages == 0) {
            this.totalPages = this.totalRows / this.numPerPage + (this.totalRows % this.numPerPage == 0 ? 0 : 1);
        }
        return this.totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
    public int getTotalRows() {
        return totalRows;
    }
    public int getStartIndex() {
        return (currentPage - 1) * numPerPage;
    }
    
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

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

    public List<T> getList() {
        return list;
    }
    
   
}

  2.思路:

1)先计算总条数,每页条数可以固定,当前页数,总页数:

select count(*)from (自己写的查询语句) a;//计算出总条数,然后通过总条数和每页条数计算出总页数,当前页是前端传递过来的。将计算出来的这些数据放入PageInfo<T>的页数相关字段中

2)获取对应页数的T(实体类):

通过mysql的limit字段:

开始页数:

public int getStartIndex() {
return (currentPage - 1) * numPerPage;
}

条数:取每页条数。

3)将T封装到PageInfo<T>的private List<T> list中;

4)返回PageInfo<T>

3.具体方法:

public <T> PageInfo<T> getObjectsByPage(Class<T> cls,String sql,RowMapper<T> rowMapper,PageInfo<T> pageInfo,Object[] objects){
if(null==pageInfo){
pageInfo=new PageInfo<T>();
}
if(pageInfo.getTotalRows()<=0){
//总条数
Integer total= this.getCount(sql, objects);
pageInfo.setSumObj(sumObj);
//Integer total=getObjectCount(cls,sql, objects);
pageInfo.setTotalRows(total);//总条数:sum子查询出来的
//总页数
Integer totalPages= (int) Math.ceil((double)total/(double)pageInfo.getNumPerPage());
pageInfo.setTotalPages(totalPages);
}
List<T> list=getObjectList(cls,sql, objects, pageInfo, rowMapper);
pageInfo.setList(list);
return pageInfo; 
}

private Integer getCount(String sql,Object... objects){
StringBuffer count=new StringBuffer("select ");
String sqlStr=sql.toLowerCase();

count.append(" count(*) id ");

count.append(" from (");
count.append(sqlStr);
count.append(") temp_for_page_count");


return getAccess(null).findInteger(count.toString(), objects);
}

private <T> List<T> getObjectList(Class<T> cls,String sql,Object[] objects,PageInfo<T> pageInfo,RowMapper<T> rowMapper){
StringBuffer limit=new StringBuffer(sql);
limit.append(" limit ").append(pageInfo.getStartIndex()).append(",").append(pageInfo.getNumPerPage());
List<T> list=getAccess(cls).find(limit.toString(),rowMapper,objects);
return list;

}
View Code

jdbc查询语句:

 public <T> List<T> find(String sql, RowMapper<T> rowMapper, Object... params) {
        StopWatch watch = new StopWatch();
        try {
            return this.jdbcTemplate.query(sql, params, rowMapper);
        } finally {
            this.logger.debug(FIND_SQL_LOG, new Object[] { sql, params, Long.valueOf(watch.elapsedTime()) });
            if (watch.elapsedTime() > SLOW_SQL_TIME_LENGTH)
                this.logger.warn(SLOW_SQL_LOG, new Object[] { sql, Long.valueOf(watch.elapsedTime()) });
        }
    }
View Code

 

posted on 2018-06-28 17:43  栖梧  阅读(355)  评论(0编辑  收藏  举报