日期:2014-05-18  浏览次数:20709 次

求分页的代码
求分页的代码   最好能用Hibernate来实现

------解决方案--------------------
一段代码参考,ssh框架中分页的方法.
public List queryProducts(String fieldname,String value, int pageSize, int startRow) {
final int pageSize1 = pageSize;
final int startRow1 = startRow;
final String sql = " FROM Products where " + fieldname + " like '% " + value + " % ' " + " ORDER BY gameNameCn " ;
return this .getHibernateTemplate().executeFind( new HibernateCallback() {

public List doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(sql);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
} );
}
------解决方案--------------------
楼上真恶心
------解决方案--------------------
public List findallByList(Page page){
Session session=HibernateSessionFactory.getSession();
String hql= "from com.tb.hrm.work.vo.TWorkexp where state= '0 ' ";
Query query=session.createQuery(hql);
query.setFirstResult(page.getMaxSize()*(page.getCurrentPage()-1));
query.setMaxResults(page.getMaxSize());
List list=query.list();
HibernateSessionFactory.closeSession();
return list;
}


package com.tb.hrm.common;

public class Page {
private int maxSize;
private int totalPage;
private int totalSize;
private int currentPage;
private int lastPage;
private int nextPage;
public Page(){

}
public Page(int maxSize,int totalSize,int currentPage){
this.maxSize=maxSize;
this.totalSize = totalSize;
this.totalPage =(totalSize+maxSize-1)/maxSize;
this.currentPage = currentPage;
}
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getLastPage() {
return currentPage-1;
}
public void setLastPage(int lastPage) {
this.lastPage = currentPage-1;
}
public int getNextPage() {
return currentPage+1;
}
public void setNextPage(int nextPage) {
this.nextPage = currentPage+1;
}
}


public class WorkDispatchAction extends DispatchAction {
public ActionForward findall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO 自动生成方法存根
String currentPage=request.getParameter( "currentPage ");
int i=1;
int maxSize=2;
TWorkexpDAO workexpDAO=new TWorkexpDAO();
List listSize=workexpDAO.findall();
int totalPage=(listSize.size()+maxSize-1)/2;
if(currentPage!=null){
i=Integer.par