利用Hibernate提供的分页分页(Hibernate+Spring+Struts)
来源:互联网 发布:软件实施工程师工资 编辑:程序博客网 时间:2024/05/17 20:23
首先:我在model(domain)先封装一个Page类
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 代码如下:
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实现类:
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 =this.getHibernateTemplate().find(querySentence);
Integer rows = (Integer) list.get(0);
return rows;
}
}
接下来就是service的接口
import java.util.List;
import com.bay.model.Page;
import com.bay.model.Product;
public interface ProductManage ...{
public Product getProductById(String productId);
public List getProductByCatalogId(int pagesize,int currow,String catalogId);
public List searchProductListByKeyword(String keyword);
public int getCount(String CatalogId);
public Page getPager(String currentPage,String pageMethod,int totalRows);
}
service的接口的实现类
import java.util.List;
import com.bay.dao.ProductDAO;
import com.bay.model.Page;
import com.bay.model.Product;
import com.bay.service.ProductManage;
public class ProductManageImpl extends BaseServiceImpl implements ProductManage ...{
private ProductDAO productDAO=null;
public void setProductDAO(ProductDAO productDAO) ...{
this.productDAO = productDAO;
}
public Product getProductById(String productId)...{
return productDAO.getProductByProductId(productId);
}
public List getProductByCatalogId(int pagesize,int currow,String catalogId)...{
return productDAO.getProductByCatalogId( pagesize,currow,catalogId);
}
public List searchProductListByKeyword(String keyword)...{
return productDAO.searchProductListByKeyword(keyword);
}
public int getCount(String CatalogId)...{
return productDAO.getCount(CatalogId);
}
public Page getPager(String currentPage,String pageMethod,int totalRows)...{
//定义pager对象,用于传到页面
Page pager = new Page(totalRows);
//如果当前页号为空,表示为首次查询该页
//如果不为空,则刷新page对象,输入当前页号等信息
if (currentPage != null) ...{
pager.refresh(Integer.parseInt(currentPage));
}
//获取当前执行的方法,首页,前一页,后一页,尾页。
if (pageMethod != null) ...{
if (pageMethod.equals("first")) ...{
pager.first();
} else if (pageMethod.equals("previous")) ...{
pager.previous();
} else if (pageMethod.equals("next")) ...{
pager.next();
} else if (pageMethod.equals("last")) ...{
pager.last();
}
}
return pager;
}}
最后就是Action了:
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.bay.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.bay.model.*;
/** *//**
* MyEclipse Struts
* Creation date: 01-10-2007
*
* XDoclet definition:
* @struts.action
*/
public class MainAction extends BaseAction ...{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
try...{
//取得Top页面的动态信息
getTopInfo(request,response);
//取到product信息
String catalogid=request.getParameter("catalogid");
if(catalogid==null||catalogid.equals(""))...{
catalogid="1";
}
int totalRows;//记录总行数
String currentPage = request.getParameter("currentPage");
String pageMethod = request.getParameter("pageMethod");
log.info("#######"+currentPage+"@@@@@@@@@@@"+pageMethod);
totalRows=getService().getProductManage().getCount(catalogid);
Page p=getService().getProductManage().getPager(currentPage, pageMethod, totalRows);
List productlist=getService().getProductManage().getProductByCatalogId(p.getPageSize(),p.getStartRow(),catalogid);
request.getSession().setAttribute("productlist",productlist);
request.getSession().setAttribute("page",p);
//如果用户是点击注销链接,则将登录用户信息移除出去
log.debug("DAASAsdas");
return mapping.findForward("success");
}catch(Exception e)...{
e.printStackTrace();
request.setAttribute("exception", e);
return mapping.findForward("error");
}
}}
web层:
<table width="751" border="0" cellpadding="0" cellspacing="0" align=center >
<tr height="50"><td></td><td align=right height="20">
第<bean:write name="page" property="currentPage"/>页
共<bean:write name="page" property="totalPages" />页
<html:link action="/jsp/main.do?pageMethod=first"
paramName="page" paramProperty="currentPage" paramId="currentPage">首页</html:link>
<html:link action="/jsp/main.do?pageMethod=previous"
paramName="page" paramProperty="currentPage" paramId="currentPage">上一页</html:link>
<html:link action="/jsp/main.do?pageMethod=next"
paramName="page" paramProperty="currentPage" paramId="currentPage">下一页</html:link>
<html:link action="/jsp/main.do?pageMethod=last"
paramName="page" paramProperty="currentPage" paramId="currentPage">尾页</html:link>
</td>
</tr>
</table>
各位,请多指教了.Email:wmr208@hotmail.com
- 利用Hibernate提供的分页分页(Hibernate+Spring+Struts)
- spring+struts+hibernate分页
- spring+struts+hibernate分页
- spring+struts+hibernate分页 完整版
- Struts+Hibernate分页的实现
- struts+hibernate的分页技术
- struts+hibernate 分页的实现
- 分页 STRUTS+HIBERNATE+ORACLE分页
- Spring,hibernate,struts的结合做分页的例子
- struts+spring+hibernate通用分页方法
- struts+spring+hibernate通用分页方法
- Spring+Hibernate+Struts技术的一个分页程序,转载
- 基于Spring+Hibernate+Struts框架分页的一种实现
- Spring+Hibernate+Struts技术的一个分页程序
- struts+hibernate+spring分页的简单实现(一)
- struts+hibernate+spring分页的简单实现(二)
- Spring+Hibernate+Struts技术的一个分页程序
- Spring+Hibernate+Struts技术的一个分页程序
- WEB打印
- ASP.Net实现将Word转换PDF格式
- Asp.net 2.0的异常处理需要考虑的问题
- xml parser源代码
- Svchost.exe是病毒的两种情况
- 利用Hibernate提供的分页分页(Hibernate+Spring+Struts)
- 全局临时表的使用
- [原创] TAHI报文“制作”简介
- 历届JOLT大奖回顾
- Tangram自由版本发布
- ASP.NET 2.0打造购物车和支付系统之二
- Myeclipse中Struts+hibernate的困惑
- 进度。。。。
- asp.net整合你的文本编辑器