首先:我在model(domain)先封装一个Page类
package com.bay.model;
public class Page ...{
private int totalRows; //总行数
private int pageSize = 12; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
public Page(int _totalRows) ...{
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0)...{
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() ...{
return startRow;
}
public int getTotalPages() ...{
return totalPages;
}
public int getCurrentPage() ...{
return currentPage;
}
public int getPageSize() ...{
return pageSize;
}
public void setTotalRows(int totalRows) ...{
this.totalRows = totalRows;
}
public void setStartRow(int startRow) ...{
this.startRow = startRow;
}
public void setTotalPages(int totalPages) ...{
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) ...{
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) ...{
this.pageSize = pageSize;
}
public int getTotalRows() ...{
return totalRows;
}
public void first() ...{
currentPage = 1;
startRow = 0;
}
public void previous() ...{
if (currentPage == 1) ...{
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() ...{
if (currentPage < totalPages) ...{
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() ...{
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) ...{
currentPage = _currentPage;
if (currentPage > totalPages) ...{
last();
}
}
}
public class Page ...{
private int totalRows; //总行数
private int pageSize = 12; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
public Page(int _totalRows) ...{
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0)...{
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() ...{
return startRow;
}
public int getTotalPages() ...{
return totalPages;
}
public int getCurrentPage() ...{
return currentPage;
}
public int getPageSize() ...{
return pageSize;
}
public void setTotalRows(int totalRows) ...{
this.totalRows = totalRows;
}
public void setStartRow(int startRow) ...{
this.startRow = startRow;
}
public void setTotalPages(int totalPages) ...{
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) ...{
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) ...{
this.pageSize = pageSize;
}
public int getTotalRows() ...{
return totalRows;
}
public void first() ...{
currentPage = 1;
startRow = 0;
}
public void previous() ...{
if (currentPage == 1) ...{
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() ...{
if (currentPage < totalPages) ...{
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() ...{
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) ...{
currentPage = _currentPage;
if (currentPage > totalPages) ...{
last();
}
}
}
本分页是针对本项目的product做的分页,DAO Interface 代码如下:
package com.bay.dao;
import java.util.List;
import com.bay.model.Product;
public interface ProductDAO extends DAO ...{
public List getProductByCatalogId(int pagesize,int currow,String CatalogId);
public Product getProductByProductId(String ProductId);
public List searchProductListByKeyword(String keyword);
public int getCount(String CatalogId);
}
import java.util.List;
import com.bay.model.Product;
public interface ProductDAO extends DAO ...{
public List getProductByCatalogId(int pagesize,int currow,String CatalogId);
public Product getProductByProductId(String ProductId);
public List searchProductListByKeyword(String keyword);
public int getCount(String CatalogId);
}
DAO实现类:
package com.bay.dao.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bay.dao.ProductDAO;
import com.bay.model.Product;
public class ProductDAOHibernate extends HibernateDaoSupport implements ProductDAO ...{
public List getProductByCatalogId(int pagesize,int currow,String CatalogId) ...{
String querySentence = "from Product where catalog_id="+CatalogId+"";
Session session=this.getHibernateTemplate().getSessionFactory() .openSession();
Query query = session.createQuery(querySentence);
query.setFirstResult(currow)
.setMaxResults(pagesize);
List list = query.list();
session.close();
return list;
}
public Product getProductByProductId(String ProductId)...{
List productList=getHibernateTemplate().find("from Product where product_id="+ProductId+" ");
if(productList.size()>0)...{
return (Product)productList.get(0);
}
return null;
}
public List searchProductListByKeyword(String keyword)...{
Session session=this.getHibernateTemplate().getSessionFactory() .openSession();
Query q=session.createQuery("from Product where product_name=?");
q.setParameter(0, keyword);
List qlist=q.list();
session.close();
return qlist;
}
public int getCount(String CatalogId)
...{
String querySentence =" select count(*) from Product where catalog_id="+CatalogId+"";
List list =
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bay.dao.ProductDAO;
import com.bay.model.Product;
public class ProductDAOHibernate extends HibernateDaoSupport implements ProductDAO ...{
public List getProductByCatalogId(int pagesize,int currow,String CatalogId) ...{
String querySentence = "from Product where catalog_id="+CatalogId+"";
Session session=this.getHibernateTemplate().getSessionFactory() .openSession();
Query query = session.createQuery(querySentence);
query.setFirstResult(currow)
.setMaxResults(pagesize);
List list = query.list();
session.close();
return list;
}
public Product getProductByProductId(String ProductId)...{
List productList=getHibernateTemplate().find("from Product where product_id="+ProductId+" ");
if(productList.size()>0)...{
return (Product)productList.get(0);
}
return null;
}
public List searchProductListByKeyword(String keyword)...{
Session session=this.getHibernateTemplate().getSessionFactory() .openSession();
Query q=session.createQuery("from Product where product_name=?");
q.setParameter(0, keyword);
List qlist=q.list();
session.close();
return qlist;
}
public int getCount(String CatalogId)
...{
String querySentence =" select count(*) from Product where catalog_id="+CatalogId+"";
List list =