新手一枚,初次接触分页查询,还是借鉴了网上的大神的案例完成的,在实现过程中碰到了一些问题,相信对于很多初学者来说还是很有用的。
本例采用s2sh框架实现,前面的Bean层,xml配置就不一一展示了。不是完整实例,仅供参考。
ContentBean.java部分代码:
private Integer contentid;
private UserBean userBean;
private String title;
private String contenturl;
private Integer isfront;
private String pictureurl;
private Date time;
private Integer count;
private Integer ispublish;
private Set menuBeans = new HashSet(0);
private Set organizationBeans = new HashSet(0);
dao层:
1、ContentDao.java
//查找所有菜单的所有内容,并分页
public ContentQueryResult FindAllMenuContent(int firstindex);
2、ContentDaoImpl.java
public ContentQueryResult FindAllMenuContent(int firstindex) {
ContentQueryResult contentQueryResult=new ContentQueryResult(); //查询的结果集
String hql="select menu.contentBeans from MenuBean menu";
Query query = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(hql); //生成query查询对象
contentQueryResult.setTotalrecord(query.list().size()); //设置结果集中的totalrecord属性(总记录数)的值
query.setFirstResult(firstindex).setMaxResults(10); //设置查询对象每次查询时的起始位置和每次查询的条数
contentQueryResult.setResultlist(query.list()); //将查询对象查询的结果录入结果集的resultlist属性中
return contentQueryResult;
}
service层:
1、ContentService.java
public ContentQueryResult FindAllMenuContent(int firstindex);
2、ContentServiceImpl.java
public class ContentServiceImpl implements ContentService {
private ContentDao contentdao;
public ContentDao getContentdao() {
return contentdao;
}
public void setContentdao(ContentDao contentdao) {
this.contentdao = contentdao;
}
public ContentQueryResult FindAllMenuContent(int firstindex) {
return contentdao.FindAllMenuContent(firstindex);
}
}
Action:
1、ContentAction.java
public class MenuAction {
private ContentService contentservice;
private Integer page;
public ContentService getContentservice() {
return contentservice;
}
public void setContentservice(ContentService contentservice) {
this.contentservice = contentservice;
}
public Integer getPage() {
return page=(page==null||page<1?1:page);
}
public void setPage(Integer page) {
this.page = page;
}
}
Spring注入:
1、applicationContext.xml
<bean id="contentdao" class="Dao.ContentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="contentservice" class="Service.ContentServiceImpl">
<property name="contentdao" ref="contentdao"></property>
</bean>
<bean id="contentaction" class="Action.ContentAction">
<property name="contentservice" ref="contentservice"></property>
</bean>
分页工具类:
1、pageIndex.java
public class PageIndex {
private long startindex;
private long endindex;
public PageIndex(long startindex, long endindex) {
this.startindex = startindex;
this.endindex = endindex;
}
public long getStartindex() {
return startindex;
}
public void setStartindex(long startindex) {
this.startindex = startindex;
}
public long getEndindex() {
return endindex;
}
public void setEndindex(long endindex) {
this.endindex = endindex;
}
public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
long endpage = currentPage+viewpagecount/2;
if(startpage<1){
startpage = 1;
if(totalpage>=viewpagecount)
endpage = viewpagecount;
else
endpage = totalpage;
}
if(endpage>totalpage){
endpage = totalpage;
if((endpage-viewpagecount)>0)
startpage = endpage-viewpagecount+1;
else
startpage = 1;
}
return new PageIndex(startpage, endpage);
}
}
1、ContentPageView
public class ContentPageView {
/** 总数据 **/
private List<ContentBean> records;
/** 页码开始索引和结束索引 **/
private PageIndex pageindex;
/** 总页数 **/
private long totalpage;
/** 每页显示记录数 **/
private int maxresult;
/** 当前页 **/
private int currentpage;
/** 总记录数 **/
private long totalrecord;
/** 页码数量 **/
private int pagecode;
/** 要获取记录的开始索引 **/
public int getFirstResult() {
return (this.currentpage-1)*this.maxresult;
}
public int getPagecode() {
return pagecode;
}
public void setPagecode(int pagecode) {
this.pagecode = pagecode;
}
public ContentPageView(int maxresult, int currentpage) {
this.maxresult = maxresult;
this.currentpage = currentpage;
}
public void setQueryResult(ContentQueryResult qr){
setTotalrecord(qr.getTotalrecord());
setRecords(qr.getResultlist());
}
public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
}
public List<ContentBean> getRecords() {
return records;
}
public void setRecords(List<ContentBean> records) {
this.records = records;
}
public PageIndex getPageindex() {
return pageindex;
}
public long getTotalpage() {
return totalpage;
}
public void setTotalpage(long totalpage) {
this.totalpage = totalpage;
this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
}
public int getMaxresult() {
return maxresult;
}
public int getCurrentpage() {
return currentpage;
}
}
查询数据集:
1、ContentQueryResult.java
public class ContentQueryResult {
/** 获得总的记录 **/
private List<ContentBean> resultlist;
/** 获得总的记录数 **/
private long totalrecord;
public List<ContentBean> getResultlist() {
return resultlist;
}
public void setResultlist(List<ContentBean> resultlist) {
this.resultlist = resultlist;
}
public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
}
}
jsp页面
1、fenye.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
.........
<body>
当前页:第${studentpageView.currentpage}页 |
总记录数:${contentpageView.totalrecord}条 | 每页显示:${contentpageView.maxresult}条 |
总页数:${contentpageView.totalpage}页</font>
<c:forEach begin="${contentpageView.pageindex.startindex}"
end="${contentpageView.pageindex.endindex}" var="wp">
<c:if test="${contentpageView.currentpage==wp}">
<b><font color="red">第${wp}页</font></b>
</c:if>
<c:if test="${contentpageView.currentpage!=wp}">
<a href="javascript:topage('${wp}')" class="a03">第${wp}页</a>
</c:if>
</c:forEach>
</body>
2、content.jsp
........
<table width="95%" border=1 align=center cellPadding=0 cellSpacing=0 borderColor=#ffffff style="FONT-SIZE: 10pt">
<tr>
<td height=28 align=center vAlign="middle" noWrap >
<jsp:include page="/menu/contentfenye.jsp"></jsp:include>
</td>
</tr>
</table>