谁能给个Hibernate分页的方法
2个回答
2013-12-24
展开全部
Hibernate为分页处理提供了良好的处理方法,下面仅举例说明
1.DAO.java中增加如下方法:
public List findByHql(String hql,int first,int amount )
{
Query query = this.getSession().createQuery(hql);
query.setFirstResult(first);
query.setMaxResults(amount);
List li = query.list();
return li;
}
2.JSP调用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.pjwqh.hibernate.dao.*"%>
<%@ page import="com.pjwqh.table.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>蜗牛小屋</title>
<script type="text/javascript">
function goto(){
var page=window.document.getElementById("page").value;
window.document.location.href="index.jsp?first="+page*5;
}
</script>
</head>
<body >
<%
Message m=new Message();
MessageDAO mdao=new MessageDAO();
String mhql="from Message order by id desc";
int first=0;//初始记录
int amount=5;//每页显示记录数
int rows=mdao.findAll().size();//总记录数
System.out.println("总记录数:"+rows);
int curPage;//当前第几页
int countPage;//共多少也
if(request.getParameter("first")!=null){
String temFirst=(String)request.getParameter("first");
first=Integer.parseInt(temFirst);
if(first<0){
first=0;
%>
<script type="text/javascript">window.alert("已经是第一页了");window.document.location.href="javascript:history.go(-1)";</script>
<%
}
if(first>rows)
{
%>
<script type="text/javascript">window.alert("已经是最后一页了");window.document.location.href="javascript:history.go(-1)";</script>
<%
}
}
if(rows%amount==0){
countPage=rows/amount;
}else{
countPage=rows/amount+1;
}
curPage=first/amount+1;
List mli=mdao.findByHql(mhql,first,amount);
Iterator mit=mli.iterator();
while(mit.hasNext())
{
m=(Message)mit.next();
//该你自己发挥了^_^
}
%>
<div> 当前第<%=curPage %>页,共<%=countPage %>页
跳到第
<select name="page" id="page" onChange="javascript:goto();">
<option ></option>
<%for(int i=0;i<countPage;i++)
{
%>
<option value=<%=i %>><%=i+1 %></option>
<%
}
%>
</select>
页 <a href="index.jsp?first=<%=first-amount %>">上一页</a> <a href="index.jsp?first=<%=first+amount %>下一页</a> </div>
</body>
</html>
1.DAO.java中增加如下方法:
public List findByHql(String hql,int first,int amount )
{
Query query = this.getSession().createQuery(hql);
query.setFirstResult(first);
query.setMaxResults(amount);
List li = query.list();
return li;
}
2.JSP调用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.pjwqh.hibernate.dao.*"%>
<%@ page import="com.pjwqh.table.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>蜗牛小屋</title>
<script type="text/javascript">
function goto(){
var page=window.document.getElementById("page").value;
window.document.location.href="index.jsp?first="+page*5;
}
</script>
</head>
<body >
<%
Message m=new Message();
MessageDAO mdao=new MessageDAO();
String mhql="from Message order by id desc";
int first=0;//初始记录
int amount=5;//每页显示记录数
int rows=mdao.findAll().size();//总记录数
System.out.println("总记录数:"+rows);
int curPage;//当前第几页
int countPage;//共多少也
if(request.getParameter("first")!=null){
String temFirst=(String)request.getParameter("first");
first=Integer.parseInt(temFirst);
if(first<0){
first=0;
%>
<script type="text/javascript">window.alert("已经是第一页了");window.document.location.href="javascript:history.go(-1)";</script>
<%
}
if(first>rows)
{
%>
<script type="text/javascript">window.alert("已经是最后一页了");window.document.location.href="javascript:history.go(-1)";</script>
<%
}
}
if(rows%amount==0){
countPage=rows/amount;
}else{
countPage=rows/amount+1;
}
curPage=first/amount+1;
List mli=mdao.findByHql(mhql,first,amount);
Iterator mit=mli.iterator();
while(mit.hasNext())
{
m=(Message)mit.next();
//该你自己发挥了^_^
}
%>
<div> 当前第<%=curPage %>页,共<%=countPage %>页
跳到第
<select name="page" id="page" onChange="javascript:goto();">
<option ></option>
<%for(int i=0;i<countPage;i++)
{
%>
<option value=<%=i %>><%=i+1 %></option>
<%
}
%>
</select>
页 <a href="index.jsp?first=<%=first-amount %>">上一页</a> <a href="index.jsp?first=<%=first+amount %>下一页</a> </div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询