用hibernate实现数据库底层分页 (转自ejunnet)

2023-10-11 21:45

1、创建SessionFactory,为了减轻服务器负担,保证一个应用中仅生成一个SessionFactory即可.

package  dao;

import  org.hibernate.cfg. * ;
import  org.hibernate. * ;

public   class  MySessionFactory  ... {
    
private static Configuration config=null;
    
private static SessionFactory sf=null;
    
private MySessionFactory()...{
        config
=new Configuration().configure();
        sf
=config.buildSessionFactory();
    }

    
    
public static SessionFactory getSessionFactory()...{
        
if(sf==null)...{
            
new MySessionFactory();
        }

        
return sf;
    }

    
}

 2、实现分页过程
方法getTotalPage()得到总页数, getObject()将所查询的数据封装到list里!

 

package  dao;

import  java.util.List;

import  org.hibernate.Query;
import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;

public   class  pageList  ... {
    
// 分页实现代码:
    
// 得到总页数 pagesize为一个页面显示的记录数
    public int getTotalPage(String hql, int pagesize) ...{
        SessionFactory sf 
= MySessionFactory.getSessionFactory();
        Session session 
= sf.openSession();
        Transaction ts 
= session.beginTransaction();
        Query query 
= session.createQuery(hql);
        List list 
= query.list();
        
int totalrs = list.size();
        
int totalpage = 0;
        
if (totalrs % pagesize > 0...{
            totalpage 
= totalrs / pagesize + 1;
        }
 else ...{
            totalpage 
= totalrs / pagesize;
        }

        ts.commit();
        session.close();
        
return totalpage;
    }


    
// 分页list
    public List getObject(String hql, int page, int pagesize) ...{
        
if (page < 1)page = 1;
        SessionFactory sf 
= MySessionFactory.getSessionFactory();
        Session session 
= sf.openSession();
        Transaction ts 
= session.beginTransaction();
        Query query 
= session.createQuery(hql);
        query.setFirstResult(page 
* pagesize - pagesize);
        query.setMaxResults(pagesize);
        List list 
= query.list();
        ts.commit();
        
// session.close();
        return list;
    }


    
// 分页代码完
}

代码中page为请求的页面,pagesize为一个页面显示的记录数。

3、应用实例:

 

<% ... @ page language="java" contentType="text/html; charset=GBK"
    pageEncoding
="GBK"
%>
<% ... @ page import="dao.*,po.*,java.util.*" %>
<% ... @ taglib uri="http://www.gavelz.com/struts/tags-bean"
    prefix
="bean"
%>
<% ... @ taglib uri="http://www.gavelz.com/struts/tags-html"
    prefix
="html"
%>
<% ...
    
String nowpage = request.getParameter("page");
//如果请求业面为空或第一次访问时,页面设为1
    
if (nowpage == null || nowpage.equals(""))nowpage = "1";
//将nowpage转为整型
    
int mypage = Integer.parseInt(nowpage);
    
if (mypage <= 0)mypage = 1;
//页面记录数设为10
    
int pagesize = 10;
    
String hql = "from ExamStudent";
    pageList pl 
= new pageList();
    
int totalpage = pl.getTotalPage(hql, pagesize);
//当请求页面大于总页数,则将当前请求页面设为最大页数
    
if (mypage > totalpage)mypage = totalpage;
    ArrayList list 
= (ArrayList) pl.getObject(hql, mypage,pagesize);
    Iterator it 
= list.iterator();
%>
< html >
< head >
< title > JSP for AddStudentForm form title >
head >
< body >
< table  border ="1" >
    
< tr >
        
< td > 学号 td >
        
< td > 姓名 td >
        
< td > 性别 td >
        
< td > 电话 td >
        
< td  colspan ="2" > 操作 td >
    
tr >
    
<% ...
            
while (it.hasNext()) {
            ExamStudent es 
= (ExamStudent) www.gavelz.com();
    
%>
    
< tr >
        
< td > <% = es.getStudentClassid() %> td >
        
< td > <% = es.getStudentName() %> td >
        
< td > <% = es.getStudentSex() %> td >
        
< td > <% = es.getStudentTel() %> td >
        
< td >< href ="updateStudent.jsp?id=<%=es.getStudentId()%>" > 修改 a > td >
        
< td >< href ="/exam/deleteStudent?id=<%=es.getStudentId()%>" > 删除 a > td >
    
tr >
    
<% ...
        }
        list.clear();
    
%>
    
< tr >
        
< td  colspan ="6" >< href ="addStudent.jsp?page=1" > 首页 a > || < a
            
href ="addStudent.jsp?page=<%=mypage-1%>" > 上一页 a > || < a
            
href ="addStudent.jsp?page=<%=mypage+1%>" > 下一页 a > || < a
            
href ="addStudent.jsp?page=<%=totalpage%>" > 末页 a > td >
    
tr >
table >
body >
html >

 

页面中page ,nowpage,mypage均为当前请求页面,只是数据类型不同。

//完  第一次用hibernate做分页,如有错误或不好的地方请多多指教!!