关于分页的那一点事情

前段时间在学习ssh2框架中,研究有关分页的问题,现把自己的心得分享一下。

其思路是:把不变的部分与变化的部分相分开。采用的方法是运用策略模式。具体代码如下

 

package service;

import java.util.List;
public class PageBean implements java.io.Serializable {
	
	private int currentPage;
	private int totalPages;
	private int pageRows=5;
	private int totalRows;
	private boolean hasPreviousPage;
	private boolean hasNextPage;
	private PageCountable pageCounter;// 策略模式的运用,引入分页计算器
	
	public void setPageCounter(PageCountable counter)
	{
		pageCounter = counter;
	}
	public PageCountable getPageCounter()
	{
		return pageCounter;
	}
	
	public int getCurrentPage()
	{
		return currentPage;
	}

	public int getTotalPages()
	{
		return totalPages;
	}

	public int getPageRows()
	{
		return pageRows;
	}
	public void setPageRows(int pageRows)
	{
		 this.pageRows=pageRows;
	}

	public int getTotalRows()
	{
		return totalRows;
	}


	public boolean isHasPreviousPage()
	{
		return hasPreviousPage;
	}

	public boolean isHasNextPage()
	{
		return hasNextPage;
	}

	
	public void initPageBean()//初始化分页bean
	{
		this.pageRows = pageRows;
		this.totalRows = pageCounter.getTotalRows();//分页计算器中的方法,用于计算总行数
		this.currentPage = 1;
		
		if((totalRows % pageRows)==0)
		{
			totalPages = totalRows / pageRows;
			if(this.totalPages == 0) this.totalPages = 1;
		}
		else
		{
			totalPages = totalRows / pageRows + 1;
		}
		
		this.hasPreviousPage = false;
		
		if(currentPage == totalPages) hasNextPage = false;
		else hasNextPage = true;			
	
	}
	
	public List getAppointPageList(int current)//取得制定页数的内容,页码从1开始
	{
		this.currentPage = current;
		
		if(currentPage > this.totalPages) this.currentPage = this.totalPages;
		
		if(currentPage < 1) this.currentPage = 1;
		
		if(this.currentPage > 1) this.hasPreviousPage = true;
		else this.hasPreviousPage = false;
		
		if(this.currentPage < this.totalPages) this.hasNextPage = true;
		else this.hasNextPage = false;
			
		return pageCounter.findList(current,pageRows);//分页计算器中的方法,用于获取指定页码的记录
	}
}
 
package service;
import java.util.List;
public interface PageCountable {
	
	int getTotalRows();//获取总行数
	List findList(int PageNo,int PageSize);//返回制定页面的列表

}
 
package service;
import domain.*;
import dao.*;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProductCounter implements PageCountable {
	
	String queryString;// hql语句
	String[] values;//查询内容

	 private GenericDao<ProductInfo,Integer> dao;//由spring注入的dao
     
     public GenericDao getDao() {  
         return dao;  
     }  
   
     public void setDao(GenericDao dao) {  
        this.dao =dao;  
     }  


 	public String getQueryString() {
 		return queryString;
 	}

 	public void setQueryString(String queryString) {
 		this.queryString = queryString;
 	}

 	public String[] getValues() {
 		return values;
 	}

 	public void setValues(String[] values) {
 		this.values = values;
 	}
 	

	public List findList(int PageNo, int PageSize) {

		int start=(PageNo-1)*PageSize;//计算第一条记录的位置
		int limit =PageSize;//最大记录数
		return dao.find(queryString, values, start, limit);

	}

	public int getTotalRows() {
			return dao.find(queryString, values).size();
		
	}


}

 其中PageCountable是接口类,ProductCounter是它的实现类,一旦我们想进行阿不不了新的查询,比如原料,文章,只要实现了PageCountable类,就可以了,不用在编写其他复杂的代码.

如有新的建议或想法,请与我留言哈

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值