hibernate实现分页(oracle)

编程入门 行业动态 更新时间:2024-10-10 07:28:11

hibernate实现<a href=https://www.elefans.com/category/jswz/34/1769545.html style=分页(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)

本文发布于:2024-02-19 18:40:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   hibernate   oracle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!