《分页的一种实现》-记忆啊,记忆!|你好Blog
你好Blog-记忆啊,记忆!

记忆啊,记忆!
http://www.nihaoblog.com/548.html 

05-5-25

分页的一种实现   (JAVA)

转载来自:落魄江湖载酒行
原文出处:http://www.matrix.org.cn/blog/gh_aiyz/
小妖 发表于:05-05-25 18:00

只要做过数据量大的系统的人,基本都会被分页的问题困扰一段时间,而且几乎最后都各有各的实现方式。每个系统/每种数据库都会有所差异。

分页的实现大致就是两种,一种每次从数据库取出一页,并显示;一种取出数据缓存,每次从内存中拿出一页来显示。前者空间效率高,后者时间效率高。

我的一种分页实现方式(in Java),取两种方式的折衷——缓存部分数据。

几个类:
(1) Page:代表一页数据
(2) PageController: 翻页控制
(3) DataFetcher: 负责从数据库抓取数据,是一个抽象类,可针对每个数据库系统实现一个,比如SQLServerDataFetcher,OracleDataFetcher.

Page类

/*
* Created on 2005-4-9
*/
package com.loading.dbaccess;

import java.util.Iterator;
import java.util.List;

/**
* @author stanley
*
*/
public class Page {
private List items;//Page Data
private boolean isLastPage;
private boolean isFirstPage;
private int pageMaxSize;
private int pageNumber;
/**
* @return Returns the isFirstPage.
*/
public boolean isFirstPage() {
return isFirstPage;
}
/**
* @param isFirstPage The isFirstPage to set.
*/
public void setFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}
/**
* @return Returns the isLastPage.
*/
public boolean isLastPage() {
return isLastPage;
}
/**
* @param isLastPage The isLastPage to set.
*/
public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
/**
* @return Returns the pageNumber.
*/
public int getPageNumber() {
return pageNumber;
}
/**
* @param pageNumber The pageNumber to set.
*/
void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
/**
* @return Returns the pageMaxSize.
*/
public int getPageMaxSize() {
return pageMaxSize;
}
/**
* @param pageMaxSize The pageMaxSize to set.
*/
public void setPageMaxSize(int pageMaxSize) {
this.pageMaxSize = pageMaxSize;
}
/**
* @return Returns the page size.
*/
public int getPageSize() {
return items==null?0:items.size();
}
/**
* @return Returns the page iterator.
*/
public Iterator getIterator() {
if(items==null) return null;
return items.iterator();
}

/**
* @return Returns the items.
*/
public List getItems() {
return items;
}
/**
* @param items The items to set.
*/
public void setItems(List items) {
this.items = items;
}
}

类PageController
/*
* Created on 2005-4-9
*/
package com.loading.dbaccess;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;


/**
* @author stanley.ding
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PageController {

/**
* @author stanley
*
*/

//default values
public static int DEFAULT_CACHED_PAGES = 1;
public static int DEFAULT_PAGE_SIZE = 20;

//page control variables
private int totalSize;
private int cachedPageCount;
private int pageSize;
private int lastPageSize;
private int pageCount;//page count
private int cachedFirstPageNo;//1,2,3.....

private List cachedItems;//catched data

//data fetcher,
DataFetcher fetcher = null;

//have fetched data from database?
boolean isInitialized = false;

public PageController(DataFetcher fetcher, int pageSize,int cachedPageCount)
{
this.pageSize = pageSize;
this.cachedPageCount = cachedPageCount;
}

public PageController(DataFetcher fetcher,int pageSize)
{
this(fetcher,pageSize,DEFAULT_CACHED_PAGES);
}

public PageController(DataFetcher fetcher)
{
this(fetcher,DEFAULT_PAGE_SIZE);
}

public boolean initialize(Connection conn) throws SQLException
{
this.totalSize = fetcher.doGetTotal(conn);
this.isInitialized = true;
this.pageCount = (this.totalSize-1) / this.pageSize + 1;

return this.isInitialized;
}
public Page getPage(Connection connection,int pageNumber) throws SQLException
{
Page page = new Page();

page.setPageNumber(pageNumber);
page.setFirstPage(pageNumber==1);
page.setLastPage(pageNumber==pageCount);
page.setPageMaxSize(pageSize);

if(!isInCache(pageNumber))
{
fetchFromDB(pageNumber, connection);
}

page.setItems(getItemsFromCache(pageNumber));

return page;
}
protected boolean isInCache(int pageNumber)
{
return pageNumber>cachedFirstPageNo && pageNumber<(cachedFirstPageNo+cachedPageCount)
&& cachedItems !=null && cachedItems.size()/pageSize>(pageNumber-cachedFirstPageNo+1);
}
protected List getItemsFromCache(int pageNumber)
{
ArrayList sList = new ArrayList();

ListIterator iterator = cachedItems.listIterator( (pageNumber-cachedFirstPageNo)*pageSize );

int i = 0;
while(i
{
i++;
sList.add(iterator.next());
}

return sList;
}

protected void fetchFromDB(int pageNumber,Connection connection)
{
//To Do:implement this method
//fetch each ROW into a Hashmap(key-field name,value-field value)
//these Hashmaps are insert into a List
}
/**
* @return Returns the totalSize.
*/
public int getTotalSize() {
return totalSize;
}
/**
* @return Returns the cachedPageCount.
*/
public int getCachedPageCount() {
return cachedPageCount;
}
/**
* @return Returns the lastPageSize.
*/
public int getLastPageSize() {
return lastPageSize;
}
/**
* @return Returns the pageCount.
*/
public int getPageCount() {
return pageCount;
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @return Returns the isInitialized.
*/
public boolean isInitialized() {
return isInitialized;
}
}

类 DataFetcher

/*
* Created on 2005-4-10
*/
package com.loading.dbaccess;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
* @author stanley
*
*/
abstract public class DataFetcher {
//database access variables
private String tableName;
private List fieldNames;
private String whereClause;
private String orderByClause;
private boolean isDistinct;

public DataFetcher(String tableName, List fieldNames, String whereClause, String orderByClause , boolean isDistinct)
{
this.tableName = tableName;
this.fieldNames = fieldNames;
this.isDistinct = isDistinct;
this.orderByClause = orderByClause;
this.whereClause = whereClause;
}

abstract public List doFetch(Connection connection, int firstRecord, int maxRecords) throws SQLException;
abstract public int doGetTotal(Connection connection ) throws SQLException;
}

版权声明:如本文牵涉版权问题,"你好Blog"不承担相关责任,请版权拥有者直接与文章作者联系解决。谢谢!
引用通告地址(TrackBack Ping Url)
复制引用地址 http://www.nihaoblog.com/trackback.action?itemId=8432
复制引用地址 http://www.nihaoblog.com/trackback.action?itemId=8432
*评 论 人 记忆
*电子邮件 公开Email
*评论内容
(少于256字)
新发的评论置于第1页 评论1