`
mywebcode
  • 浏览: 1001530 次
文章分类
社区版块
存档分类
最新评论

分页类

 
阅读更多
public class Page {
	// 1.每页显示数量(everyPage)
	private int everyPage;
	// 2.总记录数(totalCount)
	private int totalCount;
	// 3.总页数(totalPage)
	private int totalPage;
	// 4.当前页(currentPage)
	private int currentPage;
	// 5.起始点(beginIndex)
	private int beginIndex;
	// 6.是否有上一页(hasPrePage)
	private boolean hasPrePage;
	// 7.是否有下一页(hasNextPage)
	private boolean hasNextPage;

	public Page(int everyPage, int totalCount, int totalPage, int currentPage,
			int beginIndex, boolean hasPrePage, boolean hasNextPage) {
		this.everyPage = everyPage;
		this.totalCount = totalCount;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.beginIndex = beginIndex;
		this.hasPrePage = hasPrePage;
		this.hasNextPage = hasNextPage;
	}

	//构造函数,默认
	public Page(){}
	
	//构造方法,对所有属性进行设置
	
	
	public int getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public boolean isHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

}

/*
 * 分页信息辅助类
 */
public class PageUtil {
	
	public static Page createPage(int everyPage,int totalCount,int currentPage) {
		everyPage = getEveryPage(everyPage);
		currentPage = getCurrentPage(currentPage);
		int totalPage = getTotalPage(everyPage, totalCount);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(everyPage, totalCount, totalPage, currentPage,
				beginIndex, hasPrePage,  hasNextPage);
	}
	
	public static Page createPage(Page page,int totalCount) {
		int everyPage = getEveryPage(page.getEveryPage());
		int currentPage = getCurrentPage(page.getCurrentPage());
		int totalPage = getTotalPage(everyPage, totalCount);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(everyPage, totalCount, totalPage, currentPage,
				beginIndex, hasPrePage,  hasNextPage);
	}
	
	//设置每页显示记录数
	public static int getEveryPage(int everyPage) {
		return everyPage == 0 ? 10 : everyPage;
	}
	
	//设置当前页
	public static int getCurrentPage(int currentPage) {
		return currentPage == 0 ? 1 : currentPage;
	}
	
	//设置总页数,需要总记录数,每页显示多少
	public static int getTotalPage(int everyPage,int totalCount) {
		int totalPage = 0;
		if(totalCount % everyPage == 0) {
			totalPage = totalCount / everyPage;
		} else {
			totalPage = totalCount / everyPage + 1;
		}
		return totalPage;
	}
	
	//设置起始点,需要每页显示多少,当前页
	public static int getBeginIndex(int everyPage,int currentPage) {
		return (currentPage - 1) * everyPage;
	}
	
	//设置是否有上一页,需要当前页
	public static boolean getHasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}
	
	//设置是否有下一个,需要总页数和当前页
	public static boolean getHasNextPage(int totalPage, int currentPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}
	
}


进行测试:

public class Test {
	public static void main(String[] args) {
		Page page = PageUtil.createPage(5, 34, 7);
		System.out.println(page.getBeginIndex());
		System.out.println(page.getCurrentPage());
		System.out.println(page.getEveryPage());
		System.out.println(page.getTotalCount());
		System.out.println(page.getTotalPage());
		System.out.println(page.isHasNextPage());
		System.out.println(page.isHasPrePage());
	}
}


分享到:
评论

相关推荐

    叶子asp分页类(好用)

    名称: 叶子asp分页类 Name: ShowoPage(vbs class) RCSfile: ReadMe.txt Revision: 0.12.20051114.f Author: Yehe(叶子) Released: 2005-11-14 09:40:13 Descript: ASP分页类,支持access/mssql/mysql/sqlite ...

    分页 jsp 分页类 java

    分页 jsp分页类 java代码 web 分页 分页代码

    php封装的page分页类.zip

    主要介绍了php封装的page分页类,结合完整实例形式分析了php数值运算与字符串操作实现分页功能的相关技巧,需要的朋友可以参考下。

    叶子asp分页类0.12

    名称: 叶子asp分页类 Name: ShowoPage(vbs class) RCSfile: ReadMe.txt Revision: 0.12.20051114.f Author: Yehe(叶子) Released: 2005-11-14 09:40:13 Descript: ASP分页类,支持access/mssql/mysql/sqlite ...

    一个很好的分页类php分页类

    一个很好的分页类一个很好的分页类一个很好的分页类一个很好的分页类一个很好的分页类一个很好的分页类

    PHP超强分页类

    PHP超强分页类,分页类,仿百度分页类,多种样式的分页类

    php完美分页类

    一款适合各种分页的分页类,可以直接根据构造函数的提示直接使用,分页导航直接调用最后的那个函数,非常完美的分页,考虑到各种情况.

    asp.net 分页类

    asp.net 分页类。跟VS自带的控件分页比起来,分页类更灵活,性能更高。

    asp静态生成 分页类

    asp 静态生成 分页类 asp 静态生成 分页类

    jsp分页类 自己的写的

    jsp分页类 自己的写的 servletjsp分页类 自己的写的 servletjsp分页类 自己的写的 servletjsp分页类 自己的写的 servlet

    asp分页类,套用方便,收藏

    asp分页类,套用方便,值得收藏

    php pdo分页类

    php5推出的pdo统一数据接口,为了方便数据的分页写的pdo分页类。

    php分页类工具类

    这是一个功能强大,并且易于扩展的分页类,该类将分页的各个元素单独分开模块化,用户可以随心所欲的拼接出自己想要的分页效果,更是添加了控制分页样式的功能,可以让你独立设置分页各元素的样式,也集成了集中已经...

    asp分页类,很详细,很快

    asp分页类,asp分页类,很详细,很快

    经典的java分页类

    经典的java分页类,快来下载吧!!!!!

    万能的php分页类实例代码

    万能的php分页类实例代码,万能的php分页类实例代码,万能的php分页类实例代码

    php数据分页类

    分页类的设计不仅可以让用户读取到表中所有数据,而且每次只从数据库服务器中读取一点点数据,既能提高数据库的反应速度,又可以提高页面加载速度,所以说分页程序是Web开发的一个重要组成部分,此段代码本人测试...

    高效asp分页类,带例子

    高效asp分页类,带例子,1000000条数据分页几乎没有停顿,速度非常快

    PHP 分页类. 多语言.

    PHP 分页类. 多语言. 常用的普通的分页类.

Global site tag (gtag.js) - Google Analytics