java实现分页

平时我们用到的分页程序主要有两种分页方式:

1:用数据库进行分页方式,也就是说,每次从数据库中取得要展示的数据的记录。

2:一次从数据库中抓取到所有需要的数据记录放到缓存中,页面展示的时候根据需要从缓存中取得所需要的数据信息,然后展现给用户。

这里介绍的是第二种分页方式:

 

 

/**
 * 
 */
package com.gd.page;

/**
 * @author lgd
 *
 */
public interface PageInfo
{
	/**
	 * 不能确定总纪录数
	 */
    public static final int NO_SURE_ALL_COUNT_NUMBER = Integer.MIN_VALUE ; 

	/**
	 * 不能确定页数
	 */
	public static final int NO_SURE_PAGE_NUMBER = Integer.MIN_VALUE + 1 ;  

    /**
     * @return 总页数
     */
    public int getPageCount();
	
    /**
     * @return 当前页号
     */
    public int getPageNumber();

    /**
     * @return 每页行数
     */
    public int getPageSize();

    /**
     * @return 总计纪录数
     */
    public int getRecordCount();

    /**
     * @return 当前页纪录数
     */
    public int getRecordCountInPage();
    
}

 

/**
 * 
 */
package com.gd.page;
/**
 * @author lgd
 *
 */
public class RecordPageInfo implements PageInfo
{
    private int pageCount = 0;
    private int pageSize = 0;
    private int recordCount = 0;
    private int pageNumber = 0;
    private int recordCountInPage=0;
    /**
     * @param recordCnt 总纪录数
     * @param pageSize  分页尺寸
     */
    public RecordPageInfo(int recordCnt, int pageSize)
    {
        this.pageSize=pageSize ;
        this.recordCount =recordCnt ;
        if (recordCnt==0)
        { 
            pageCount =0 ; 
        }
        else if (pageSize<=0) 
        { 
            pageCount = 1 ; 
            pageNumber =1 ;
            recordCountInPage = recordCnt;
            this.pageSize = 0;
        } 
        else 
        {
             pageCount = (recordCnt + pageSize - 1) / pageSize ;
             pageNumber =1 ;
             makeRecordCountInpage();
        } 
    }
    /**
     * 
     * @param pageNo 
     * @return 该页应该定位到的起始行号 ,行号的起始数是 1 
     */
    public int gotoPage(int pageNo)
    {
        if (pageCount==0) 
        {
            pageNumber = 0;
            recordCountInPage = 0 ;  
            return 0 ;      
        }
        else if (pageSize <= 0)
        {
            return 1 ;
        }
        else if (pageCount < pageNo)
        {
            this.pageNumber = pageCount ;
        }
        else if (pageNo <= 0 )
        {
            this.pageNumber = 1 ;
        }
        else 
        {
            this.pageNumber = pageNo ;
        }
        makeRecordCountInpage ();
        return (pageNumber-1)*pageSize + 1;
    }
	/**
	 * 构建每页显示的数量
	 */
    private void makeRecordCountInpage()
    {
         if (pageNumber== pageCount ) //最后一页
         {
             recordCountInPage = recordCount - (pageNumber-1)*this.pageSize;  
         }
         else 
         {
             recordCountInPage = this.pageSize;
         }
    }

    /**
     * @return 页数量
     */
    public int getPageCount()
    {
        return pageCount;
    }

    /**
     * @return 当前页号
     */
    public int getPageNumber()
    {
        return pageNumber;
    }

    /**
     * @return 页大小
     */
    public int getPageSize()
    {
        return pageSize;
    }

    /**
     * @return 记录总数
     */
    public int getRecordCount()
    {
        return recordCount;
    }

    /**
     * @return 当页记录数
     */
    public int getRecordCountInPage()
    {
        return recordCountInPage;
    }
	public String toString()
	{
 		StringBuffer buffer = new StringBuffer();
		buffer.append("pageCount = " + pageCount );
		buffer.append("\npageSize = " + pageSize );
		buffer.append("\nrecordCount = " + recordCount );
		buffer.append("\npageNumber = " + pageNumber );
		buffer.append("\nrecordCountInPage = " + recordCountInPage );
		return buffer.toString();
	}
}

 

/**
 * 
 */
package com.gd.page;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author lgd
 * 分页的页内容信息,每页显示多少条信息
 */
public class PagedList implements List,PageInfo
{
    
    private final PageInfo info;

	private final List list ; 
	/**
	 * 
	 * @param pageSize 
	 * @param pageNo
	 * @param list
	 * @return PagedList
	 */
	public static final PagedList getInstance(int pageSize, int pageNo, List list)
	{
		RecordPageInfo pageInfo = new RecordPageInfo(list.size() ,pageSize);
		int fromIndex =pageInfo.gotoPage(pageNo) - 1;
		if (fromIndex<0)
		{
			return new PagedList(pageInfo, Collections.EMPTY_LIST);
		}
		int toIndex  = fromIndex + pageSize  ;
		if (toIndex > list.size()) toIndex = list.size()  ; 
		return new PagedList(pageInfo, list.subList(fromIndex, toIndex));
	}

	public static final PagedList EMPTY_PAGEDLIST = new PagedList(new  RecordPageInfo(0,0),new ArrayList(0));

	/**
     * @param info
     * @param list
     * 
     */
	public PagedList(PageInfo info, List list) 
    {
        super();
        this.info = info ;
        this.list = list; 
    }
    public int getPageCount()
    {
        
        return info.getPageCount();
    }
    public int getPageNumber()
    {
        return info.getPageNumber();
    }
    public int getPageSize()
    {
        return info.getPageSize() ;
    }
    public int getRecordCount()
    {
        return info.getRecordCount();
    }
    public int getRecordCountInPage()
    {
        return size();
    }
	public void add(int index, Object element)
	{
		list.add(index, element);
	}
	public boolean add(Object o)
	{
		return list.add(o);
	}
	public boolean addAll(int index, Collection c)
	{
		return list.addAll(index, c);
	}
	public boolean addAll(Collection c)
	{
		return list.addAll(c);
	}
	public void clear()
	{
		list.clear();
	}
	public boolean contains(Object o)
	{
		return list.contains(o);
	}
	public boolean containsAll(Collection c)
	{
		return list.containsAll(c);
	}
	public boolean equals(Object obj)
	{
		return list.equals(obj);
	}
	public Object get(int index)
	{
		return list.get(index);
	}
	public int hashCode()
	{
		return list.hashCode();
	}

	public int indexOf(Object o)
	{
		return list.indexOf(o);
	}

	public boolean isEmpty()
	{
		return list.isEmpty();
	}

	public Iterator iterator()
	{
		return list.iterator();
	}

	public int lastIndexOf(Object o)
	{
		return list.lastIndexOf(o);
	}

	public ListIterator listIterator()
	{
		return list.listIterator();
	}
	public ListIterator listIterator(int index)
	{
		return list.listIterator(index);
	}

	public Object remove(int index)
	{
		return list.remove(index);
	}
	public boolean remove(Object o)
	{
		return list.remove(o);
	}

	public boolean removeAll(Collection c)
	{
		return list.removeAll(c);
	}
	public boolean retainAll(Collection c)
	{
		return list.retainAll(c);
	}
	public Object set(int index, Object element)
	{
		return list.set(index, element);
	}
	public int size()
	{
		return list.size();
	}
	public List subList(int fromIndex, int toIndex)
	{
		return list.subList(fromIndex, toIndex);
	}
	public Object[] toArray()
	{
		return list.toArray();
	}
	public Object[] toArray(Object[] a)
	{
		return list.toArray(a);
	}
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("PageInfo :\n" + info.toString());
		buffer.append("ListSize() : " + list.size());
		if (list.size() > 0)
		{
			buffer.append("ListElementType : " + list.get(0).getClass().getName());
		}
		return  buffer.toString();
	}
}

 

/**
 * 
 */
package com.gd.page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lgd
 *
 */
public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		    List<Integer> list = init();
		    int pageNo=1;
		    //10表示每页10条记录,pageNo表示页号,list记录集合
		    PagedList pagelist=PagedList.getInstance(10,pageNo,list);
		    for(Object p:pagelist){
		    	System.out.println(p);
		    }
	}
	private static List<Integer> init() {
		List<Integer>  list=new ArrayList<Integer>();
		list.add(0);
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		list.add(6);
		list.add(7);
		list.add(8);
		list.add(9);
		list.add(10);
		list.add(11);
		return list;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值