使用Hibernate分页问题
使用hibernate调用自己的分页类 第一页数据出来了 但是没有下一页的数据 页码也没有 走断点总记录数和页码的分组数都计算出来了
这是分页类
package cn.jbit.renting.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Java Bean
* @author Administrator
*
*/
public class Pager implements Serializable{
//public static final int PAGE_RECORD = 6;
public static final int GROUP_RECORD = 5;//显示几组
public static int page_record;//每页显示几条数据
private int currentPage;// 当前页码
private int pageCount;// 总页数
private int recordCount;// 总记录数
private int currentGroup;// 当前组数
private int groupCount;// 总组数
private List<Integer> groupList;// 页面中需要的分组页索引
public int prevPage;
public int nextPage;
public int getPage_record() {
return page_record;
}
public void setPage_record(int page_record) {
this.page_record = page_record;
}
// 构造函数
public Pager(int page){
currentPage = page;
}
// 获取当前页页码
public int getCurrentPage(){
return currentPage;
}
// 获取下一页页码
public int getNextPage(){
if(currentPage < pageCount)
return currentPage+1;
else
return pageCount;
}
// 获取上一页页码
public int getPrevPage(){
if(currentPage <= 1)
return 1;
else
return currentPage - 1;
}
// 获取当前组数
public int getCurrentGroup(){
return currentGroup;
}
public List<Integer> getGroupList(){
// 计算当前组数
currentGroup = (int)Math.ceil((float)currentPage / GROUP_RECORD);
groupList = new ArrayList<Integer>();
int start = (currentGroup-1) * GROUP_RECORD;
int end = (start + GROUP_RECORD) < pageCount ? start + GROUP_RECORD : pageCount;
for (int i = start + 1; i <= end; i++) {
groupList.add(i);
}
return groupList;
}
// 获取总页数
public int getPageCount(){
return pageCount;
}
// 设置总记录数
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
pageCount = (int)Math.ceil(((float)recordCount / page_record));
}
// 获取总记录数
public int getRecordCount() {
return recordCount;
}
}
方法调用
/***
* 分页
*/
public List<House> SearchpageHouse(Pager pager) {
if(pager.getRecordCount() == 0){
pager.setRecordCount(getHouse().size());//总共多少条记录
}
String hql="from House";
Query query=session.createQuery(hql);
int start=(pager.getCurrentPage() - 1) * Pager.page_record;//起始位置
query.setFirstResult(start);
query.setMaxResults(start + Pager.page_record);//结束为止
List<House> list=query.list();
return list;
}
servlet里的调用
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//HouseBiz house=new HouseBizImpl();
//List<House> houses=house.getHouse();
//request.setAttribute("houses", houses);
//request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
HouseBiz hb=new HouseBizImpl();
String page=request.getParameter("page");
int p=1;
if(page!=null&&!(page.equals("")))
{
p=Integer.parseInt(page);
}
Pager pager=new Pager(p);
pager.setPage_record(3);//每页显示三条记录
//int size=2;
//List<House> list=hb.getpageHouse(index, size);
List<House> list=hb.SearchpageHouse(pager);
//int count=hb.getHouse().size();
//int topage=(count%size==0)?(count/size):(count/size+1);
//request.setAttribute("topage", topage);//最大页码
request.setAttribute("houses", list);
//request.setAttribute("index", index);//当前页码
request.setAttribute("paper", pager);
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
jsp页面
<table>
<c:forEach items="${houses}" var="houses">
<tr >
<td rowspan="3" style="width:50px"><img src="images/thumb_house.gif" /></td>
<td style="width:400px">${houses.title}</td>
<c:if test="${user!=null}">
<c:if test="${houses.user_id.id==user.id}">
<td rowspan="3" style="width:100px"><a>修改</a></td>
<td rowspan="3" style="width:100px"><a href="javascript:deleteHouse(${houses.id})">删除</a></td>
</c:if>
</c:if>
</tr>
<tr>
<td style="width:400px">${houses.street_id.name}--${houses.street_id.distrct_ID.name}</td>
</tr>
<tr>
<td style="width:400px">联系电话:${houses.contact}</td>
</tr>
</c:forEach>
</table>
<!--
${index}/${topage}
<c:if test="${index>1}"><a href="GetItem">首页</a></c:if>
<c:forEach begin="1" end="${topage}" var="p">
<a href="GetItem?index=${p}">${p}</a>
</c:forEach>
<c:if test="${index<topage}"><a href="GetItem?index=${topage}">末页</a></c:if>
-->
<a href="GetItem?page=${requestScope.pager.prevPage }">上一页</a>
<c:forEach items="${requestScope.pager.groupList }" var="i">
<c:if test="${ i == pager.currentPage }">
${i}
</c:if>
<c:if test="${ i != pager.currentPage }">
<a href="GetItem?page=${i}">${i }</a>
</c:if>
</c:forEach>
<a href="GetItem?page=${requestScope.pager.nextPage }">下一页</a>
</div>
[解决办法]
提供给你一个万能分页类,只需要输入一个HQL语句即可实现分页
public class PagingModel {
private static Log logger = LogFactory.getLog(PagingModel.class);
/**
* 此hql只接受 select t from(FROM) 类的查询
* @param session
* @param hql
* @param currentPage
* @param pageSize
* @return
* @throws Exception
*/
public static PageController findPage(Session session, String hql ,long currentPage,
int pageSize) throws Exception {
logger.trace("entering");
long number = getRSCount(session, hql);
PageController page = findPage(number, session, hql, currentPage, pageSize);
logger.trace("exiting");
return page;
}
/**
* 此hql可以接受 select distinct .. from(FROM) 类的查询
* @param session
* @param hql
* @param currentPage
* @param pageSize
* @return
* @throws Exception
*/
public static PageController findPage2(Session session, String hql ,long currentPage,
int pageSize) throws Exception {
logger.trace("entering");
long number = getRSCount2(session, hql);
PageController page = findPage(number, session, hql, currentPage, pageSize);
logger.trace("exiting");
return page;
}
private static PageController findPage(long number, Session session, String hql ,long currentPage,
int pageSize) throws Exception {
logger.trace("entering");
long countPage = getMemberPageCount(number, pageSize);
if (currentPage > countPage) {
currentPage = countPage;
} else if (currentPage < 1) {
currentPage = 1L;
}
long firstNumber = (currentPage - 1) * pageSize;
if (firstNumber > number) {
firstNumber = (int)number;
}
Query query = session.createQuery(hql);
query.setFirstResult((int) firstNumber);
query.setMaxResults(pageSize);
List list = query.list();
PageController page = new PageController();
page.setTotalRecord(number);// 共多少记录
page.setCurPage((int) currentPage);// 当前页
page.setLastPage((int) countPage);// 共多少页
page.setResults(list);// 当前页的内容
logger.trace("exiting");
return page;
}
/**
* 获取当前查询共有多少页
* @param count
* @param pageCount
* @return
* @throws Exception
*/
private static long getMemberPageCount(long count, int pageSize) throws Exception{
logger.trace("entering");
long countPage = 0;
if(count % pageSize == 0){
countPage = count / pageSize;
}else{
countPage = count / pageSize + 1;
}
logger.trace("exiting");
return countPage;
}
/**
* 获取当前查询共有多少条记录
* @param session
* @param hql
* @return
* @throws Exception
*/
private static long getRSCount(Session session, String hql) throws Exception{
StringBuffer buffer = new StringBuffer();
int index = hql.indexOf("FROM") == -1 ? hql.indexOf("from") : hql.indexOf("FROM");
buffer.append("SELECT count(*) ");
buffer.append(hql.substring(index, hql.length()));
Query query = session.createQuery(buffer.toString());
Object o = query.uniqueResult();
Long l = Long.valueOf(o.toString());
logger.trace("exiting");
return l.longValue();
}
/**
* 获取当前查询共有多少条记录
* @param session
* @param hql
* @return
* @throws Exception
*/
private static long getRSCount2(Session session, String hql) throws Exception{
logger.trace("entering");
StringBuffer buffer = new StringBuffer();
int index_from = hql.indexOf("FROM") == -1 ? hql.indexOf("from") : hql.indexOf("FROM");
int index_distinct = hql.indexOf("DISTINCT") == -1 ? hql.indexOf("distinct") : hql.indexOf("DISTINCT");
int index_order = hql.indexOf("ORDER") == -1 ? hql.indexOf("order") : hql.indexOf("ORDER");
buffer.append("SELECT count(").append(hql.substring(index_distinct, index_from)).append(") ");
if (index_order < 0) {
buffer.append(hql.substring(index_from, hql.length()));
} else {
buffer.append(hql.substring(index_from, index_order));
}
Query query = session.createQuery(buffer.toString());
Object o = query.uniqueResult();
Long l = Long.valueOf(o.toString());
logger.trace("exiting");
return l.longValue();
}
}
public class PageController {
public final static int PAGE_SIZE = 10;
private List results;
private int curPage = 1;
private int perPage = 20;
private int prePage;
private int nextPage;
private int firstPage = 1;
private int lastPage;
private int recordStart = 1;
private long totalRecord = 0;
public PageController() {
}
public PageController(List results, int totalCount, Integer curPage,
Integer perPage) {
this.results = results;
if (totalCount > 0) {
this.totalRecord = totalCount;
}
if (perPage != null && perPage.intValue() > 0) {
this.perPage = perPage.intValue();
}
if (curPage != null && curPage.intValue() > 0) {
this.curPage = curPage.intValue();
}
if (this.curPage < 1) {
this.curPage = 1;
}
this.lastPage = (totalCount / this.perPage);
if (totalCount % this.perPage != 0) {
this.lastPage += 1;
}
if (this.curPage > this.lastPage) {
this.curPage = this.lastPage;
}
this.prePage = (this.curPage > 1) ? (this.curPage - 1) : 1;
this.nextPage = (this.curPage == this.lastPage) ? this.lastPage
: (this.curPage + 1);
recordStart = (this.curPage - 1) * this.perPage + 1;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPerPage() {
return perPage;
}
public void setPerPage(int perPage) {
this.perPage = perPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getRecordStart() {
return recordStart;
}
public void setRecordStart(int recordStart) {
this.recordStart = recordStart;
}
public List getResults() {
return results;
}
public void setResults(List results) {
this.results = results;
}
public long getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(long totalRecord) {
this.totalRecord = totalRecord;
}
public String getNavBar(String url, String param) {
StringBuffer str = new StringBuffer(url);
if (StringUtils.isEmpty(url)) {
return url.toString();
}
if (url.indexOf("?") != -1) {
str.append("&");
} else {
str.append("?");
}
str.append(param).append("=");
StringBuffer nav = new StringBuffer();
nav.append("共 ").append(totalRecord).append(
" 条记录 第 ").append(curPage)
.append(" 页 / 共 ").append(lastPage).append(
" 页 ");
nav.append("<a href=\"").append(str).append(this.firstPage).append(
"\">首页</a> ");
nav.append("<a href=\"").append(str).append(this.prePage).append(
"\">上一页</a> ");
nav.append("<a href=\"").append(str).append(this.nextPage).append(
"\">下一页</a> ");
nav.append("<a href=\"").append(str).append(this.lastPage).append(
"\">尾页</a> ");
return nav.toString();
}
}