3个回答
2013-09-12
展开全部
首先得来个实体类(存储分页的信息,我起名称叫Page)
import java.util.ArrayList;
import java.util.List;public class Page {
private int totalPage = 1;/**
* 前一页
*/
private int prePage = 1;/**
* 下一页
*/
private int nextPage = 1;/**
* 总记录数
*/
private int totalRec = 0;/**
* 默认每页记录数
*/
private final int defaultPageSize = 10;/**
* 每页记录数
*/
private int pageSize = defaultPageSize;/**
* 当前页码
*/
private int pageIndex = 1;/**
* 全部页码,从1开始
*/
private int[] pageNumbers;public int getPageIndex() {
return pageIndex;
}public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex > 0 ? pageIndex : 1;
}public int getNextPage() {
return nextPage;
}public void setNextPage(int nextPage) {
this.nextPage = nextPage > this.totalPage ? this.totalPage : nextPage;
}public int getPageSize() {
return pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize > 0 ? pageSize : 10;
}public int getPrePage() {
return prePage;
}public void setPrePage(int prePage) {
this.prePage = prePage < 1 ? 1 : prePage;
}public int getTotalPage() {
return totalPage;
}public void setTotalPage(int totalPage) {
this.totalPage = totalPage > 0 ? totalPage : 1;
}public int getTotalRec() {
return totalRec;
}public void setTotalRec(int totalRec) {
this.totalRec = totalRec > -1 ? totalRec : 0;
}public int[] getPageNumbers() {
return pageNumbers;
}public void setPageNumbers(int[] pageNumbers) {
this.pageNumbers = pageNumbers;
}
}
==============实体类有了,我们得在写一个公共的DAO方法(也就是每一个用到分页的地方都会调用这个)================public List findPageByQuery(final String queryString,
final Object[] parameters, final Page pageInfo) {
return (List) getHibernateTemplate().execute(new HibernateCallback()//这里使用了匿名内部类
{
public Object doInHibernate(Session session)//Spring进行事务维护 省去每次创建session和关闭session
throws HibernateException
{
Query query = session.createQuery(queryString);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
query.setParameter(i, parameters[i]);
}
}
ScrollableResults sr = query.scroll();
sr.last();
int totalCount = sr.getRowNumber();
int startIndex = (pageInfo.getPageIndex() - 1)
* pageInfo.getPageSize();
query.setMaxResults(pageInfo.getPageSize());
query.setFirstResult(startIndex);
int totalRec = totalCount + 1;
pageInfo.setTotalRec(totalRec);
int totalPage = (totalRec % pageInfo.getPageSize() == 0) ? (totalRec / pageInfo
.getPageSize())
: (totalRec / pageInfo.getPageSize()) + 1;
int[] pageNumbers = new int[totalPage];
for (int i = 0; i < totalPage; i++)
{
pageNumbers[i] = (i + 1);
}
pageInfo.setPageNumbers(pageNumbers);
pageInfo.setTotalPage(totalPage);
pageInfo.setPageSize(pageInfo.getPageSize());
pageInfo.setPageIndex(pageInfo.getPageIndex());
pageInfo.setPrePage(pageInfo.getPageIndex() - 1);
pageInfo.setNextPage(pageInfo.getPageIndex() + 1);
return query.list();
}
}, true);
}
=================至此我们的工作就完成了一半了 ,接下了就改写具体的方法了,来调用我们的公工分页方法public List getUU(int lexample,Page pageInfo)
{
/*ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
dao = (DaoImpl) context.getBean("Dao");
System.out.print(dao);*/ //自己测试时候可以使用
List list = null;
if (pageInfo == null)
pageInfo = new Page();
try
{
hql="SELECT exa FROM Example exa WHERE 1=1 AND exa.LExample="+lexample;
list = this.getCommonDAO().findPageByQuery(hql, null, pageInfo);
} catch (Exception ex)
{
System.out.println(ex);
}
return list;
}=============接下来就是action中调用了===============public ActionForward getMore(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ExampleForm exampleForm = (ExampleForm) form;// TODO Auto-generated method stub
List list = new ArrayList();
try
{
//pageInfo = (PageInfo) request.getAttribute("pageInfo");
String type=request.getParameter("type");
//类别编号
int typeId=0;
//页码编号
String page=request.getParameter("page");
if(page==null)
{page="1";<br> }
if(type==null) {
//默认显示女的
type="2";
}
System.out.println("action...........................");
pageInfo.setPageIndex(Integer.parseInt(page));
pageInfo.setPageSize(20);
typeId=Integer.parseInt(type);
// pageInfo.setPageIndex(Integer.parseInt(request.getParameter("Index")));
list = this.exampleBiz.getUU(typeId,pageInfo);
if(list.size()!=0)
{
request.setAttribute("list", list);
request.setAttribute("pageInfo", pageInfo);
}
}catch
(Exception ex)
{
ex.printStackTrace();
}
return mapping.findForward("more");
}======下面是页面,我们的工作马上就完了。<%String nowPage ;
String type;
int total;
nowPage=request.getParameter("page");
if(nowPage==null)
{nowPage="1";<br>}
type=request.getParameter("type"); //这个是我项目中的类别,没必要的
if(type==null) {
type="2";
}
int curPage=Integer.parseInt(nowPage);
int typeId=Integer.parseInt(type);
System.out.println(typeId+"------------------------------------");
if((Page)request.getAttribute("pageInfo") == null){
total = 1;
} else {
total = ((Page)request.getAttribute("pageInfo")).getTotalPage();
if(curPage>((Page)request.getAttribute("pageInfo")).getTotalPage()) {
curPage=total;
}
}
%><div style="width:870px; margin-bottom:20px;">
<logic:notEmpty name="list">
<logic:iterate id="example" name="list">
<div class="s_ren">
<div><img src="example.account.headIcn" /></div>
<div class="style_ju"><strong>${example.account.name }</strong></div>
<div>明星指数:<span class="style_ju">254179</span></div>
<div style="float:left; background-color:#FFC189; text-align:center; height:22px; width:55px; line-height:22px;">送鲜花</div>
<div style="float:right; background-color:#9DCBE7; text-align:center;height:22px; width:55px; line-height:22px;">扔鸡蛋</div>
</div>
</logic:iterate>
</logic:notEmpty>
</div>
<div style=" border-top:1px solid #ccc; height:1px;"></div>
<div style="text-align:center">
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=1&type=<%=typeId %>">首页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=curPage-1<0?1:curPage-1%>&type=<%=typeId %>"">上一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=(curPage+1)>total?total:curPage+1%>&type=<%=typeId %>"">下一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=total %>">末页</a>
当前第<%=curPage %>页 转到第 <select id="page" onchange="window.location.href='${pageContext.request.contextPath }/example.do?method=getMore&page='+document.getElementById('page').value;&type=<%=typeId %>"> <%
for(int toPage = 1; toPage<=total; toPage++) {
%>
<option value = <%=toPage%>
<%if(toPage==curPage){ %>
selected
<%} %>
>
<%=toPage %>
</option>
<%
}
%>
</select> 页
共<%=total%>页 ${pageInfo.pageIndex}
</div>OK!,现在我们的分页就行了,赶快试试吧!
import java.util.ArrayList;
import java.util.List;public class Page {
private int totalPage = 1;/**
* 前一页
*/
private int prePage = 1;/**
* 下一页
*/
private int nextPage = 1;/**
* 总记录数
*/
private int totalRec = 0;/**
* 默认每页记录数
*/
private final int defaultPageSize = 10;/**
* 每页记录数
*/
private int pageSize = defaultPageSize;/**
* 当前页码
*/
private int pageIndex = 1;/**
* 全部页码,从1开始
*/
private int[] pageNumbers;public int getPageIndex() {
return pageIndex;
}public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex > 0 ? pageIndex : 1;
}public int getNextPage() {
return nextPage;
}public void setNextPage(int nextPage) {
this.nextPage = nextPage > this.totalPage ? this.totalPage : nextPage;
}public int getPageSize() {
return pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize > 0 ? pageSize : 10;
}public int getPrePage() {
return prePage;
}public void setPrePage(int prePage) {
this.prePage = prePage < 1 ? 1 : prePage;
}public int getTotalPage() {
return totalPage;
}public void setTotalPage(int totalPage) {
this.totalPage = totalPage > 0 ? totalPage : 1;
}public int getTotalRec() {
return totalRec;
}public void setTotalRec(int totalRec) {
this.totalRec = totalRec > -1 ? totalRec : 0;
}public int[] getPageNumbers() {
return pageNumbers;
}public void setPageNumbers(int[] pageNumbers) {
this.pageNumbers = pageNumbers;
}
}
==============实体类有了,我们得在写一个公共的DAO方法(也就是每一个用到分页的地方都会调用这个)================public List findPageByQuery(final String queryString,
final Object[] parameters, final Page pageInfo) {
return (List) getHibernateTemplate().execute(new HibernateCallback()//这里使用了匿名内部类
{
public Object doInHibernate(Session session)//Spring进行事务维护 省去每次创建session和关闭session
throws HibernateException
{
Query query = session.createQuery(queryString);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
query.setParameter(i, parameters[i]);
}
}
ScrollableResults sr = query.scroll();
sr.last();
int totalCount = sr.getRowNumber();
int startIndex = (pageInfo.getPageIndex() - 1)
* pageInfo.getPageSize();
query.setMaxResults(pageInfo.getPageSize());
query.setFirstResult(startIndex);
int totalRec = totalCount + 1;
pageInfo.setTotalRec(totalRec);
int totalPage = (totalRec % pageInfo.getPageSize() == 0) ? (totalRec / pageInfo
.getPageSize())
: (totalRec / pageInfo.getPageSize()) + 1;
int[] pageNumbers = new int[totalPage];
for (int i = 0; i < totalPage; i++)
{
pageNumbers[i] = (i + 1);
}
pageInfo.setPageNumbers(pageNumbers);
pageInfo.setTotalPage(totalPage);
pageInfo.setPageSize(pageInfo.getPageSize());
pageInfo.setPageIndex(pageInfo.getPageIndex());
pageInfo.setPrePage(pageInfo.getPageIndex() - 1);
pageInfo.setNextPage(pageInfo.getPageIndex() + 1);
return query.list();
}
}, true);
}
=================至此我们的工作就完成了一半了 ,接下了就改写具体的方法了,来调用我们的公工分页方法public List getUU(int lexample,Page pageInfo)
{
/*ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
dao = (DaoImpl) context.getBean("Dao");
System.out.print(dao);*/ //自己测试时候可以使用
List list = null;
if (pageInfo == null)
pageInfo = new Page();
try
{
hql="SELECT exa FROM Example exa WHERE 1=1 AND exa.LExample="+lexample;
list = this.getCommonDAO().findPageByQuery(hql, null, pageInfo);
} catch (Exception ex)
{
System.out.println(ex);
}
return list;
}=============接下来就是action中调用了===============public ActionForward getMore(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ExampleForm exampleForm = (ExampleForm) form;// TODO Auto-generated method stub
List list = new ArrayList();
try
{
//pageInfo = (PageInfo) request.getAttribute("pageInfo");
String type=request.getParameter("type");
//类别编号
int typeId=0;
//页码编号
String page=request.getParameter("page");
if(page==null)
{page="1";<br> }
if(type==null) {
//默认显示女的
type="2";
}
System.out.println("action...........................");
pageInfo.setPageIndex(Integer.parseInt(page));
pageInfo.setPageSize(20);
typeId=Integer.parseInt(type);
// pageInfo.setPageIndex(Integer.parseInt(request.getParameter("Index")));
list = this.exampleBiz.getUU(typeId,pageInfo);
if(list.size()!=0)
{
request.setAttribute("list", list);
request.setAttribute("pageInfo", pageInfo);
}
}catch
(Exception ex)
{
ex.printStackTrace();
}
return mapping.findForward("more");
}======下面是页面,我们的工作马上就完了。<%String nowPage ;
String type;
int total;
nowPage=request.getParameter("page");
if(nowPage==null)
{nowPage="1";<br>}
type=request.getParameter("type"); //这个是我项目中的类别,没必要的
if(type==null) {
type="2";
}
int curPage=Integer.parseInt(nowPage);
int typeId=Integer.parseInt(type);
System.out.println(typeId+"------------------------------------");
if((Page)request.getAttribute("pageInfo") == null){
total = 1;
} else {
total = ((Page)request.getAttribute("pageInfo")).getTotalPage();
if(curPage>((Page)request.getAttribute("pageInfo")).getTotalPage()) {
curPage=total;
}
}
%><div style="width:870px; margin-bottom:20px;">
<logic:notEmpty name="list">
<logic:iterate id="example" name="list">
<div class="s_ren">
<div><img src="example.account.headIcn" /></div>
<div class="style_ju"><strong>${example.account.name }</strong></div>
<div>明星指数:<span class="style_ju">254179</span></div>
<div style="float:left; background-color:#FFC189; text-align:center; height:22px; width:55px; line-height:22px;">送鲜花</div>
<div style="float:right; background-color:#9DCBE7; text-align:center;height:22px; width:55px; line-height:22px;">扔鸡蛋</div>
</div>
</logic:iterate>
</logic:notEmpty>
</div>
<div style=" border-top:1px solid #ccc; height:1px;"></div>
<div style="text-align:center">
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=1&type=<%=typeId %>">首页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=curPage-1<0?1:curPage-1%>&type=<%=typeId %>"">上一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=(curPage+1)>total?total:curPage+1%>&type=<%=typeId %>"">下一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=total %>">末页</a>
当前第<%=curPage %>页 转到第 <select id="page" onchange="window.location.href='${pageContext.request.contextPath }/example.do?method=getMore&page='+document.getElementById('page').value;&type=<%=typeId %>"> <%
for(int toPage = 1; toPage<=total; toPage++) {
%>
<option value = <%=toPage%>
<%if(toPage==curPage){ %>
selected
<%} %>
>
<%=toPage %>
</option>
<%
}
%>
</select> 页
共<%=total%>页 ${pageInfo.pageIndex}
</div>OK!,现在我们的分页就行了,赶快试试吧!
2013-09-12
展开全部
/*
* @company jm
* @author LGQ
*/package com.sshpage.daoImpl;import java.sql.SQLException;
import java.util.*;import javax.servlet.http.HttpServletRequest;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.sshpage.dao.PaginateInterface;public class Pageinate extends HibernateDaoSupport implements PaginateInterface { private static final long serialVersionUID = 5724583235560436932L; private int count = 0; //总记录数
private int pageSize = 10; //每页显示
private int pageCount = 0;//总页数
private int page = 1; //当前页
private String totalCountSQL; // 总数sql
private String listSQL; //记录全集sql //传入表名 条件 返回 集合
@SuppressWarnings("unchecked")
public List ListSql(HttpServletRequest request, String TName, String where) {
/*
* 判断where 是否为空,不为空加上条件
*/ if (where.equals("") || where == null) {
this.listSQL = "FROM " + TName; //表总记录
this.totalCountSQL = "SELECT COUNT(*) FROM " + TName + " "; //表总记录
} else {
this.listSQL = "FROM " + TName + " " + where; //表总记录
this.totalCountSQL = "SELECT COUNT(*) FROM " + TName + " " + where; //表总记录
} String jumpPage = (String) request.getParameter("jumpPage"); //上页下页
String pageSize = request.getParameter("pageSize"); //显示条数 if (jumpPage != null && !"".equals(jumpPage) && pageSize != null
&& !"".equals(pageSize)) {
setPageSize(Integer.parseInt(pageSize));
} else {
jumpPage = "1";
} this.setCount(this.getTotalCount()); //传入总记录数 this.setPage(Integer.parseInt(jumpPage)); //转入页面 return this.getList(); } public int getCount() {
return count;
} public void setCount(int count) {
if (pageSize != 0) {
pageCount = count / pageSize; //页数 = 总记录数/每页显示
if (count % pageSize != 0) { //不能整除+1
pageCount++;
}
}
this.count = count;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getPage() {
return page;
} public void setPage(int page) {
this.page = page;
} public String getTotalCountSQL() {
return totalCountSQL;
} //存入表名
public void setTotalCountSQL(String totalCountSQL) {
this.totalCountSQL = totalCountSQL;
} public String getListSQL() {
return listSQL;
} public void setListSQL(String listSQL) {
this.listSQL = listSQL; } /*
* 查询分页
*/ public List getList() {
this.getHibernateTemplate().setCacheQueries(true); return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) { Query q = session.createQuery(getListSQL());
// q.setCacheable(true); ////激活查询缓存
//q.setCacheRegion("");//指定要使用的cacheRegion,可选 为echache.xml中指定的name
q.setFirstResult((getPage() - 1) * getPageSize()); //每页开始条数
q.setMaxResults(getPageSize()); //每页条数 return q.list(); }
});
} /*
* 查询分页 带参数
*/
public List getList(final String[] str, final Object[] ob) throws Exception {
//this.getHibernateTemplate().setCacheQueries(true); return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery(getListSQL());
if (str != null) {
for (int i = 0; i < str.length; i++) {
q.setParameter(str[i], ob[i]);
}
}
q.setFirstResult((getPage() - 1) * getPageSize());
q.setMaxResults(getPage()); System.out.println("test getList return>" + q.list().size());
return q.list();
}
});
} /*
* 取总记录条数据
*/
public int getTotalCount() {
List list = null;
this.getHibernateTemplate().setCacheQueries(true);
list = getHibernateTemplate().find(getTotalCountSQL()); int count = 0;
// Long lon;
if (list.size() > 0) {
count = Integer.parseInt(list.get(0).toString());//new Long(""+list.get(0));
}
return count;
} /*
* 取总记录条数据 带参数
*/
@SuppressWarnings("unchecked")
public int getTotalCount(String[] str, Object[] ob) throws Exception {
List list = new ArrayList();
if (str != null && str.length > 0) {
list = getHibernateTemplate().findByNamedParam(getTotalCountSQL(),
str, ob);
} else {
list = getHibernateTemplate().find(getTotalCountSQL());
}
int count = 0;
if (list.size() > 0) {
count = Integer.parseInt(list.get(0).toString());
}
return count;
} /*
* 分页工具条
*/
public String getToolMenu() { StringBuffer strb = new StringBuffer("");
int prev, next;
prev = getPage() - 1;
next = getPage() + 1;
if (getPage() > 1) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value=1;document.forms(0).submit();\">首页</a> ");
} else {
strb.append("首页 ");
//strb.append("<img src=\"images/blue16_064.gif\">");
}
if (getPage() > 1) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ prev + "';document.forms(0).submit();\">上页</a> ");
} else {
strb.append("上页 ");
}
if (getPage() < getPageCount()) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ next + "';document.forms(0).submit();\">下页</a> ");
//<a href=splitPageAction.do?jumpPage="+ next +"&&pageSize="+getPageSize()+"
} else { strb.append("下页 ");
}
if (getPageCount() > 1 && getPage() != getPageCount()) {//当前页不为末页
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ getPageCount()
+ "';document.forms(0).submit();\">末页</a>");
} else {
strb.append("末页");
}
strb.append("共" + getCount() + "条记录"); strb
.append("每页<SELECT size=1 name=pagesize onchange=\"this.form.jumpPage.value=1;"
+ "this.form.pageSize.value=this.value;this.form.submit();\">");
if (getPageSize() == 3) {
strb.append("<OPTION value=3 selected>3</OPTION>");
} else {
strb.append("<OPTION value=3>3</OPTION>");
} if (getPageSize() == 10) {
strb.append("<OPTION value=10 selected>10</OPTION>");
} else {
strb.append("<OPTION value=10>10</OPTION>");
}
if (getPageSize() == 20) {
strb.append("<OPTION value=20 selected>20</OPTION>");
} else {
strb.append("<OPTION value=20>20</OPTION>");
}
if (getPageSize() == 50) {
strb.append("<OPTION value=50 selected>50</OPTION>");
} else {
strb.append("<OPTION value=50>50</OPTION>");
}
if (getPageSize() == 100) {
strb.append("<OPTION value=100 selected>100</OPTION>");
} else {
strb.append("<OPTION value=100>100</OPTION>");
}
strb.append("</SELECT>"); strb.append("条 分" + getPageCount() + "页显示 转到");
strb
.append("<SELECT size=1 name=Pagelist onchange=\"this.form.jumpPage.value=this.value;this.form.submit();\">");
for (int i = 1; i < getPageCount() + 1; i++) {
if (i == getPage()) {
strb.append("<OPTION value=" + i + " selected>" + i
+ "</OPTION>");
} else {
strb.append("<OPTION value=" + i + ">" + i + "</OPTION>");
}
}
strb.append("</SELECT>页");
strb.append("<INPUT type=hidden value=" + getPage()
+ " name=\"pages\" > ");
strb.append("<INPUT type=hidden value=" + getPageSize()
+ " name=\"pageSize\"> "); return strb.toString();
}}
//贴个类给你过目下吧,,传入 request 表名 条件 分布 的 // 希望对你有用 同学
* @company jm
* @author LGQ
*/package com.sshpage.daoImpl;import java.sql.SQLException;
import java.util.*;import javax.servlet.http.HttpServletRequest;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.sshpage.dao.PaginateInterface;public class Pageinate extends HibernateDaoSupport implements PaginateInterface { private static final long serialVersionUID = 5724583235560436932L; private int count = 0; //总记录数
private int pageSize = 10; //每页显示
private int pageCount = 0;//总页数
private int page = 1; //当前页
private String totalCountSQL; // 总数sql
private String listSQL; //记录全集sql //传入表名 条件 返回 集合
@SuppressWarnings("unchecked")
public List ListSql(HttpServletRequest request, String TName, String where) {
/*
* 判断where 是否为空,不为空加上条件
*/ if (where.equals("") || where == null) {
this.listSQL = "FROM " + TName; //表总记录
this.totalCountSQL = "SELECT COUNT(*) FROM " + TName + " "; //表总记录
} else {
this.listSQL = "FROM " + TName + " " + where; //表总记录
this.totalCountSQL = "SELECT COUNT(*) FROM " + TName + " " + where; //表总记录
} String jumpPage = (String) request.getParameter("jumpPage"); //上页下页
String pageSize = request.getParameter("pageSize"); //显示条数 if (jumpPage != null && !"".equals(jumpPage) && pageSize != null
&& !"".equals(pageSize)) {
setPageSize(Integer.parseInt(pageSize));
} else {
jumpPage = "1";
} this.setCount(this.getTotalCount()); //传入总记录数 this.setPage(Integer.parseInt(jumpPage)); //转入页面 return this.getList(); } public int getCount() {
return count;
} public void setCount(int count) {
if (pageSize != 0) {
pageCount = count / pageSize; //页数 = 总记录数/每页显示
if (count % pageSize != 0) { //不能整除+1
pageCount++;
}
}
this.count = count;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getPage() {
return page;
} public void setPage(int page) {
this.page = page;
} public String getTotalCountSQL() {
return totalCountSQL;
} //存入表名
public void setTotalCountSQL(String totalCountSQL) {
this.totalCountSQL = totalCountSQL;
} public String getListSQL() {
return listSQL;
} public void setListSQL(String listSQL) {
this.listSQL = listSQL; } /*
* 查询分页
*/ public List getList() {
this.getHibernateTemplate().setCacheQueries(true); return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) { Query q = session.createQuery(getListSQL());
// q.setCacheable(true); ////激活查询缓存
//q.setCacheRegion("");//指定要使用的cacheRegion,可选 为echache.xml中指定的name
q.setFirstResult((getPage() - 1) * getPageSize()); //每页开始条数
q.setMaxResults(getPageSize()); //每页条数 return q.list(); }
});
} /*
* 查询分页 带参数
*/
public List getList(final String[] str, final Object[] ob) throws Exception {
//this.getHibernateTemplate().setCacheQueries(true); return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery(getListSQL());
if (str != null) {
for (int i = 0; i < str.length; i++) {
q.setParameter(str[i], ob[i]);
}
}
q.setFirstResult((getPage() - 1) * getPageSize());
q.setMaxResults(getPage()); System.out.println("test getList return>" + q.list().size());
return q.list();
}
});
} /*
* 取总记录条数据
*/
public int getTotalCount() {
List list = null;
this.getHibernateTemplate().setCacheQueries(true);
list = getHibernateTemplate().find(getTotalCountSQL()); int count = 0;
// Long lon;
if (list.size() > 0) {
count = Integer.parseInt(list.get(0).toString());//new Long(""+list.get(0));
}
return count;
} /*
* 取总记录条数据 带参数
*/
@SuppressWarnings("unchecked")
public int getTotalCount(String[] str, Object[] ob) throws Exception {
List list = new ArrayList();
if (str != null && str.length > 0) {
list = getHibernateTemplate().findByNamedParam(getTotalCountSQL(),
str, ob);
} else {
list = getHibernateTemplate().find(getTotalCountSQL());
}
int count = 0;
if (list.size() > 0) {
count = Integer.parseInt(list.get(0).toString());
}
return count;
} /*
* 分页工具条
*/
public String getToolMenu() { StringBuffer strb = new StringBuffer("");
int prev, next;
prev = getPage() - 1;
next = getPage() + 1;
if (getPage() > 1) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value=1;document.forms(0).submit();\">首页</a> ");
} else {
strb.append("首页 ");
//strb.append("<img src=\"images/blue16_064.gif\">");
}
if (getPage() > 1) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ prev + "';document.forms(0).submit();\">上页</a> ");
} else {
strb.append("上页 ");
}
if (getPage() < getPageCount()) {
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ next + "';document.forms(0).submit();\">下页</a> ");
//<a href=splitPageAction.do?jumpPage="+ next +"&&pageSize="+getPageSize()+"
} else { strb.append("下页 ");
}
if (getPageCount() > 1 && getPage() != getPageCount()) {//当前页不为末页
strb
.append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value='"
+ getPageCount()
+ "';document.forms(0).submit();\">末页</a>");
} else {
strb.append("末页");
}
strb.append("共" + getCount() + "条记录"); strb
.append("每页<SELECT size=1 name=pagesize onchange=\"this.form.jumpPage.value=1;"
+ "this.form.pageSize.value=this.value;this.form.submit();\">");
if (getPageSize() == 3) {
strb.append("<OPTION value=3 selected>3</OPTION>");
} else {
strb.append("<OPTION value=3>3</OPTION>");
} if (getPageSize() == 10) {
strb.append("<OPTION value=10 selected>10</OPTION>");
} else {
strb.append("<OPTION value=10>10</OPTION>");
}
if (getPageSize() == 20) {
strb.append("<OPTION value=20 selected>20</OPTION>");
} else {
strb.append("<OPTION value=20>20</OPTION>");
}
if (getPageSize() == 50) {
strb.append("<OPTION value=50 selected>50</OPTION>");
} else {
strb.append("<OPTION value=50>50</OPTION>");
}
if (getPageSize() == 100) {
strb.append("<OPTION value=100 selected>100</OPTION>");
} else {
strb.append("<OPTION value=100>100</OPTION>");
}
strb.append("</SELECT>"); strb.append("条 分" + getPageCount() + "页显示 转到");
strb
.append("<SELECT size=1 name=Pagelist onchange=\"this.form.jumpPage.value=this.value;this.form.submit();\">");
for (int i = 1; i < getPageCount() + 1; i++) {
if (i == getPage()) {
strb.append("<OPTION value=" + i + " selected>" + i
+ "</OPTION>");
} else {
strb.append("<OPTION value=" + i + ">" + i + "</OPTION>");
}
}
strb.append("</SELECT>页");
strb.append("<INPUT type=hidden value=" + getPage()
+ " name=\"pages\" > ");
strb.append("<INPUT type=hidden value=" + getPageSize()
+ " name=\"pageSize\"> "); return strb.toString();
}}
//贴个类给你过目下吧,,传入 request 表名 条件 分布 的 // 希望对你有用 同学
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-12
展开全部
hibernate分页Query query = session.createQuery(hql);query.setFirstResult((pageNo - 1) * pageSize);query.setMaxResult(pageSize);pageSize :每页显示的行数pageNo: 当前页数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询