/** 施杨的分页daoimpl类
**/packagecom.sy.dao.impl;importjava.sql.SQLException;importjava.util.List;importorg.hibernate.HibernateException;importorg.hibernate.Query;importorg.hibernate.Session;importorg.springframework.orm.hibernate3.HibernateCallback;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importcom.sy.dao.BaseDao;importcom.sy.util.Page;
@SuppressWarnings("unchecked")publicclassPageDaoImplextendsHibernateDaoSupport {privateString hql;publicPage page;publicintstart;publicBaseDao dao;publicvoidsetDao(BaseDao dao) {this.dao=dao;
}publicvoidinit(intstart,String tableName){//ͨ��init����ʵ����ij�ʼ��page=newPage();this.hql="from"+tableName;this.start=start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}publicintgetRowCount(){
List list=dao.find(hql);if(list.isEmpty()){return0;
}returnlist.size();
}publicPage getPage(){
List list=(List)getHibernateTemplate().execute(newHibernateCallback(){publicObject doInHibernate(Session session)throwsHibernateException, SQLException {
Query query=session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());returnquery.list();
}
});
page.setList(list);returnpage;
}publicvoidsetPreOrNextBoolean() {if(page.getCurrentPage()<=1) {
page.setHasPreviousPage(false);
}else{
page.setHasPreviousPage(true);
}if(page.getCurrentPage()>=page.getTotalPage()) {
page.setHasNextPage(false);
}else{
page.setHasNextPage(true);
}
}publicvoidsetCurrentPage() {if(start<1) {
page.setCurrentPage(1);
}if(start>page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}publicvoidsetPrePage() {
page.setPrePage(page.getCurrentPage()-1);
}publicvoidsetNextPage() {
page.setNextPage(page.getCurrentPage()+1);
}publicvoidsetTotalPage() {introwCount=getRowCount();intpageSize=page.getPageSize();if(rowCount>pageSize) {if(rowCount%pageSize==0) {
page.setTotalPage(rowCount/pageSize);
}else{
page.setTotalPage(1+(rowCount/pageSize));
}
}else{
page.setTotalPage(1);
}
}publicvoidsetRowCount() {
page.setRowCount(getRowCount());
}publicintgetStartIndex() {intstartIndex=0;if(start<0) {
startIndex=0;
}else{if(start>page.getTotalPage()) {
startIndex=page.getPageSize()*(page.getTotalPage()-1);
}else{
startIndex=page.getPageSize()*(start-1);
}
}returnstartIndex;
}
}