分页(oracle)"/>
hibernate实现分页(oracle)
先说明一下hibernate实现分页的原理,假如数据库中有50条数据,每页显示10条数据,假如我们查询第3页的数据那么应该从21开始,往后查10条数据,只需要设置起始位置和每页显示数量即可。下面我们来看一下例子.
public List getPagination(String hql,int start,int count){
Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
return query.setMaxResults(count).setFirstResult(start).list();
}
上面这个方法参数start表示起始位置,参数count表示每页显示数量。最重要的setMaxResults(),和setFirstResult(),分别是设置显示数量和起始位置。
下面是一个分页工具类。
package com.creditease.controller.utlis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Pagination {
private int currentPage=0; // 当前页
private int pageSize=10; // 每页要求最多显示的记录
private int total; // 查询 数据库 得到的所有 记录总数
private List rows; // 查询 得到 本页的数据,返回一个 装有po对象的 List
private int pageCount; // 总页数
private int startPageIndex;//开始索引:查询list时用
private int endPageIndex;//结束索引:查询list时用
private int length;//实际查询到数量
public Pagination(int currentPage, int pageSize, int total) {
this.currentPage = currentPage == 0 ? 1 : currentPage;
this.pageSize = pageSize;
this.total = total;
this.pageCount = (total+pageSize-1)/pageSize; //总页数
this.startPageIndex = (this.currentPage - 1)*pageSize;//开始下标
}
public Pagination() {
}
public int getCurrentPage() {
return currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public int getStartPageIndex() {
return startPageIndex;
}
public void setStartPageIndex(int startPageIndex) {
this.startPageIndex = startPageIndex;
}
public int getEndPageIndex() {
return endPageIndex;
}
public void setEndPageIndex(int endPageIndex) {
this.endPageIndex = endPageIndex;
}
}
分页工具类每一个属性都有注释就不解释了。
下面是我service类。
public Pagination getUserList(Pagination pagination) {
StringBuffer sb=new StringBuffer();
sb.append("select count(*) from User");
int count= this.getCount(sb.toString());//查询所以数据
pagination=new Pagination(pagination.getCurrentPage(),pagination.getPageSize(),count);
StringBuffer sbTo=new StringBuffer();
sbTo.append("from User");
List userList=this.getPagination(sbTo.toString(),pagination.getStartPageIndex(), pagination.getPageSize());
pagination.setRows(userList);
pagination.setLength(userList.size());
return pagination;
}
Action类
public String getUserList(){
String pageNum=request.getParameter("pageNum");//获得当前页
if(null != pageNum && !"".equals(pageNum)){
pagination.setCurrentPage(Integer.parseInt(pageNum));
}
pagination=userService.getUserList(pagination);
request.setAttribute("pagination",pagination);
return "userList";
}
这样hibernate分页就算完成了,我第一次不知道说明白没。
更多推荐
hibernate实现分页(oracle)
发布评论