前段时间在学习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类,就可以了,不用在编写其他复杂的代码.
如有新的建议或想法,请与我留言哈