数据量很大的情况下,关于分页的通用实现,需要保证效率

chn217 2004-10-12 09:54:19
有源代码的优先

Web页面谢绝。只需底层代码。多谢
...全文
182 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
baffling 2004-11-02
  • 打赏
  • 举报
回复
gz
chn217 2004-10-12
  • 打赏
  • 举报
回复
哦,能够具体告诉我主要阅读哪几个类么?多谢
sagittarius1979 2004-10-12
  • 打赏
  • 举报
回复
可以参考hibernate的分页实现方法.
chn217 2004-10-12
  • 打赏
  • 举报
回复
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* @author fisher
*
* <pre>
*
* Title: QueryManager.java
*
* Description: Provide with a common pagination
*
* </pre>
*/
public class PaginationManager {

// The cache contains records
// capacity: the capacity of the list
// startIndex: the current start index in list
// endIndex: the current end index in list
// currentIndex: the
private List records = new ArrayList();
private int capacity = 1000;
private int startIndex = 0;
private int endIndex = 0;
private int currentIndex = 0;

// The hander for pagination
private Paginable handler = null;

// The number of records each page contains
private int pageSize;



public PaginationManager(Paginable handler) {
this(handler, 1000, 40);
}

public PaginationManager(Paginable handler, int pageSize) {
this(handler, 1000, pageSize);
}

/**
* Construct a PaginationManager instance
*
* @param handler The handler mapulate pagination
* @param capacity The capacity of records buffer
* @param pageSize The size of each page
*/
public PaginationManager(Paginable handler, int capacity, int pageSize) {
this.handler = handler;
this.capacity = capacity;
this.pageSize = pageSize;
}


//private int recordCount;

public int getRecordCount()
throws SQLException {
return handler.getRecordCount();
}

public int getPageCount()
throws SQLException {
//Fetch record count
int recordCount = getRecordCount();

if (recordCount % pageSize != 0) {
return (int) (recordCount / pageSize) + 1;
}
else {
return recordCount / pageSize;
}
}

public List getNextPage()
throws SQLException {
List retList = new ArrayList();

// fetch limited number records from currentIndex
if (currentIndex + pageSize <= endIndex) {
retList.addAll(records.subList(currentIndex, currentIndex + pageSize));
}
// If the cursor is near to endIndex
else {
retList.addAll(records.subList(currentIndex, endIndex));
getBlock(endIndex, capacity);

}
//
return retList;
}

public List getPage(int pageIndex) {
List retList = new ArrayList();

return retList;
}

public boolean hasNext()
throws SQLException {

return currentIndex < getRecordCount();
}



private void getBlock(int startIndex, int num)
throws SQLException {
//Fetch records
records = handler.getBlock(startIndex, num);
//Update the startIndex and endIndex
this.startIndex = startIndex;
this.endIndex = startIndex + records.size();
}


///////////////////////Test////////////////////

private static void p(Object obj) {
System.out.println(obj.toString());
}

public static void main(String[] args) {
PaginationManager manager = new PaginationManager(null);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧