用hibernate实现数据库底层分页 (转自ejunnet)
来源:互联网 发布:mac电脑杀毒 编辑:程序博客网 时间:2024/05/16 10:26
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;
}
}
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;
}
// 分页代码完
}
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://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%...
String nowpage = request.getParameter("page");
pageEncoding="GBK"%>
<%...@ page import="dao.*,po.*,java.util.*"%>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%...
String nowpage = request.getParameter("page");
//如果请求业面为空或第一次访问时,页面设为1
if (nowpage == null || nowpage.equals(""))nowpage = "1";
if (nowpage == null || nowpage.equals(""))nowpage = "1";
//将nowpage转为整型
int mypage = Integer.parseInt(nowpage);
if (mypage <= 0)mypage = 1;
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);
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) it.next();
%>
<tr>
<td><%=es.getStudentClassid()%></td>
<td><%=es.getStudentName()%></td>
<td><%=es.getStudentSex()%></td>
<td><%=es.getStudentTel()%></td>
<td><a href="updateStudent.jsp?id=<%=es.getStudentId()%>">修改</a></td>
<td><a href="/exam/deleteStudent?id=<%=es.getStudentId()%>">删除</a></td>
</tr>
<%...
}
list.clear();
%>
<tr>
<td colspan="6"><a 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>
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) it.next();
%>
<tr>
<td><%=es.getStudentClassid()%></td>
<td><%=es.getStudentName()%></td>
<td><%=es.getStudentSex()%></td>
<td><%=es.getStudentTel()%></td>
<td><a href="updateStudent.jsp?id=<%=es.getStudentId()%>">修改</a></td>
<td><a href="/exam/deleteStudent?id=<%=es.getStudentId()%>">删除</a></td>
</tr>
<%...
}
list.clear();
%>
<tr>
<td colspan="6"><a 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做分页,如有错误或不好的地方请多多指教!!
- 用hibernate实现数据库底层分页 (转自ejunnet)
- 用hibernate实现数据库底层分页
- 黑马程序员:hibernate分页底层的实现
- 用hibernate分页查询的底层方法
- 用hibernate实现分页
- spring+hibernate+pager taglib实现分页(底层也做了分页)(一)
- spring+hibernate+pager taglib实现分页(底层也做了分页)(二)
- spring+hibernate+pager taglib实现分页(底层也做了分页)(三)
- 用数据库实现分页
- Hibernate 实现分页查询 [转]
- 数据库索引底层实现
- Hibernate 自带分页 bug
- hibernate数据库分页
- Hibernate数据库分页显示
- Hibernate数据库分页显示
- hibernate数据库分页查询
- JSP实现分页功能(转自其他地方)
- 利用hibernate + extjs来实现数据库的分页
- WML语言基础(WAP建站) 一
- WEB20 的本质和创新, 勇敢的新世界
- 类与模块差异范例1
- Web2.0中的排行榜
- Beyond Java
- 用hibernate实现数据库底层分页 (转自ejunnet)
- 完整的链条
- 这周的任务!
- 同一页面中多条记录的提交
- 找工拼搏经历(IT专业)
- 70个JAVA问答
- 元素周期表
- 祝哥哥生日快乐
- 获取SqlServer 2000中字段的备注信息--downmoon