一、分页查询的优势:
1.数据清晰直观
2.页面不在冗长
3.不受数据量的限制
分页查询的实现方法
1.编写分页查询sql语句
2.编写分页查询方法
3.实现分页查询显示
二、分页的两个部分
1.数据分页查询
2.数据分页显示
三、数据分页查询的实现步骤
1.确定每页显示的数据数量
2.确定需要显示的数据总数量
//编写查询信息总数量的sql语句
String sql="select count(*) from detail";
//通过JDBC进行SQL语句执行
Object[] paramas={};
ResultSet rs=this.executeSQL(sql,params);
//获取总记录数
totalCount=rs.getInt(1);
3.计算显示的页数
//总页数
private int totalPageCount=1;
//页面大小,每页显示的记录数
private int pageSize=0;
//记录总数
private int recordCount=0;
//设置总页数
private void setTotalPageCountByRs(){
if(this.recordCount%this.pageSize==0){
this.totalPageCount=this.recordCount/this.pageSize;
}else if(this.recordCount%this.pageSize>0){
this.totalPageCount=this.recordCount/this.pageSize+1;
}else{
this.totalPageCount=0;
}
}
4.编写分页查询SQL语句
select *
from(select book.*,rownum r from (select * from bs_books)book where rownum <= (page_NO*page_books) )temp where temp.r > ((page_NO-1)*page_books);5.实现分页查询
List<News> newsList-new ArrayList<News>();
//编写分页查询SQL语句
String sql="select id,title,author,createdate from
(select id,title,author,createdate,rowmun rn from detail)
a where a.rn>=? and a.rn<=?";
Page page=new Page();
page.setCurrPageNo(pageNo);//设置当前页码
page.setPageSize(pageSize);//每页显示记录数
//计算sql语句的起始记录数以及结束记录数的行数
int startRow=page.getStartRow();
int endRow=page.getEndRow();
Object[] params={startRow,endRow};
ResultSet rs=this.executeSQL(sql,params);
数据分页查询的实现的关键点:
1.计算总页数
如果总记录数能被每页显示记录数整除:
总页数=总记录数/每页显示记录数
如果总记录数不能被每页显示记录数整除:
总页数=总记录数/每页显示记录数+1
2.计算分页查询时的起始记录数与结束记录数
起始记录数:(当前页码-1)*每页显示的记录数+1
结束记录数:当前页码*每页显示的记录数
四、数据分页显示
在项目中的使用方法是:
使用JDBC操作数据库
在Dao层:
Dao类:
//获取新闻总数 public int getNewsCount(); //分页查询列表 public ListgetPageNewsList(int pageNo, int pageSize);
DaoImpl类:
1 //获取新闻总数 2 @Override 3 public int getNewsCount() { 4 // TODO Auto-generated method stub 5 int count = 0; 6 String sql = "select count(*) as count from news_detail"; 7 Object[] params = {}; 8 if(this.getConnection()){ 9 ResultSet rs = this.executeSQL(sql, params);10 try {11 if(rs.next()){12 count = rs.getInt("count");13 }14 } catch (SQLException e) {15 // TODO Auto-generated catch block16 e.printStackTrace();17 }finally{18 this.closeResource();19 }20 21 }22 return count;23 }24 //分页查询列表25 @Override26 public ListgetPageNewsList(int pageNo, int pageSize) {27 // TODO Auto-generated method stub28 List newsList = new ArrayList (); //MySQL数据库的查询29 String sql = "SELECT id,title,author,createDate FROM news_detail " +30 "ORDER BY createDate DESC LIMIT ?,?";31 pageNo = (pageNo-1)*pageSize;32 //sql的参数,两个问号处33 Object[] params = {pageNo,pageSize};34 35 if(this.getConnection()){36 ResultSet rs = this.executeSQL(sql, params);37 try {38 while(rs.next()){39 News news = new News();40 news.setId(rs.getInt("id"));41 news.setTitle(rs.getString("title"));42 news.setAuthor(rs.getString("author"));43 news.setCreateDate(rs.getTimestamp("createDate"));44 45 newsList.add(news);46 47 }48 } catch (SQLException e) {49 // TODO Auto-generated catch block50 e.printStackTrace();51 }finally{52 this.closeResource();53 }54 55 }56 57 return newsList;58 }
Biz层:
Biz类:
//获取新闻信息总数量 public int getNewsCount(); //获取分页列表 public ListgetPageNewsList(int pageNo,int pageSize);
BizImpl类:
@Override public int getNewsCount() { // TODO Auto-generated method stub return newsDao.getNewsCount(); } @Override public ListgetPageNewsList(int pageNo, int pageSize) { // TODO Auto-generated method stub return newsDao.getPageNewsList(pageNo, pageSize);
util工具类:
//分页查询的工具类,在前台分页显示时使用public class PageSupport { //总页数 private int totalPageCount = 1; //总记录数 private int totalCount = 0; //当前页码 private int currentPageNo = 1; //页面容量 private int pageSize = 0; //set、get方法 public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { //总记录数大于零,就可以调用计算总页数的方法了 if(totalCount > 0){ this.totalCount = totalCount; //设置总页数 this.setTotalPageCountByRs(); } } public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { if(currentPageNo > 0){ this.currentPageNo = currentPageNo; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if(pageSize > 0){ this.pageSize = pageSize; } } //计算总页数的方法 public void setTotalPageCountByRs(){ if(this.totalCount % this.pageSize == 0){ this.totalPageCount = this.totalCount / this.pageSize; }else if(this.totalCount % this.pageSize > 0){ this.totalPageCount = this.totalCount / this.pageSize + 1; }else{ this.totalPageCount = 0; } } }