Hibernate 分页

若-相惜 2010-01-27 10:29:39
我做的项目里需要用到分页,
谁给我说一个比较通用的分页实例,
需要模糊查询和带参的查询,
谢了
...全文
111 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingherry 2011-11-22
  • 打赏
  • 举报
回复
3L设置占位符有何作用?讲讲呗
QQde00 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 java2000_net 的回复:]
楼上写的不错,如果修改一下更好

Java code// 通过给定的hql、页数(pageNo)、最大行数(maxRow),args为hql语句中占位符的实际值,分页查询数据 @SuppressWarnings("unchecked")public List getPageList(final String hql,final Integer pageNo,final Integer maxRow,final Object[] args) {// 这个方法返回结果是个Object,强转为需要类型return (List) template.execute(new HibernateCallback() {public Object doInHibernate(Session session)throws HibernateException, SQLException {// 创建一个query并设置分页的参数 Query query= session.createQuery(hql).setFirstResult(
(pageNo-1)* maxRow).setMaxResults(maxRow);// 若hql语句中没有使用占位符,则返回查询结果if (args!=null){// 若hql语句中使用了占位符,将这些参数逐个放入hql中,并返回查询结果for (int i=0; i< args.length; i++) {
query.setParameter(i, args[i]);
}
}return query.list();
}
});
}

[/Quote]

有道理!
Z_FEI 2010-01-28
  • 打赏
  • 举报
回复
会写hql语句,知道分页的一个公式就能根据自己的需要写出来了。尤其是hibernate还有两个分页比用的方法setFirstResult、setMaxResults!
老紫竹 2010-01-28
  • 打赏
  • 举报
回复
楼上写的不错,如果修改一下更好


// 通过给定的hql、页数(pageNo)、最大行数(maxRow),args为hql语句中占位符的实际值,分页查询数据
@SuppressWarnings("unchecked")
public List getPageList(final String hql, final Integer pageNo,
final Integer maxRow, final Object[] args) {
// 这个方法返回结果是个Object,强转为需要类型
return (List) template.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// 创建一个query并设置分页的参数
Query query = session.createQuery(hql).setFirstResult(
(pageNo - 1) * maxRow).setMaxResults(maxRow);
// 若hql语句中没有使用占位符,则返回查询结果
if (args != null){
// 若hql语句中使用了占位符,将这些参数逐个放入hql中,并返回查询结果
for (int i = 0; i < args.length; i++) {
query.setParameter(i, args[i]);
}
}
return query.list();
}
});
}

xuexijava 2010-01-28
  • 打赏
  • 举报
回复
mark
QQde00 2010-01-28
  • 打赏
  • 举报
回复
给你一个我自己写的

// 通过给定的hql、页数(pageNo)、最大行数(maxRow),args为hql语句中占位符的实际值,分页查询数据
@SuppressWarnings("unchecked")
public List getPageList(final String hql, final Integer pageNo,
final Integer maxRow, final Object[] args) {
// 这个方法返回结果是个Object,强转为需要类型
return (List) template.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// 创建一个query并设置分页的参数
Query query = session.createQuery(hql).setFirstResult(
(pageNo - 1) * maxRow).setMaxResults(maxRow);
// 若hql语句中没有使用占位符,则返回查询结果
if (args == null)
return query.list();
// 若hql语句中使用了占位符,将这些参数逐个放入hql中,并返回查询结果
for (int i = 0; i < args.length; i++) {
query.setParameter(i, args[i]);
}
return query.list();
}
});
}
reandyner 2010-01-28
  • 打赏
  • 举报
回复
<div id="divTopPageNavi" class="list_r_title_text3">
<div class='list_r_title_text3a'>
<c:choose>
<c:when test="${current-1 gt 0 }">
<a name=link_page_next
href="javascript:load('bookList.do?cid=${param.cid}&page=${current-1}&order='+$F('order_select'),$('book_list').parentNode);">
<img
onmouseover='this.src="${pageContext.request.contextPath}/images/page_up_mouseover.gif"'
onmouseout='this.src="${pageContext.request.contextPath}/images/page_up_mouseover.gif"'
src='${pageContext.request.contextPath}/images/page_up_mouseover.gif' />
</a>
</c:when>
<c:otherwise>
<img
src='${pageContext.request.contextPath}/images/page_up_gray.gif'
align='absmiddle' />
</c:otherwise>
</c:choose>
</div>
<div class='list_r_title_text3b'>
第${current }页/共${total }页
</div>
<div class='list_r_title_text3a'>
<c:choose>
<c:when test="${current lt total }">
<a name=link_page_next
href="javascript:load('bookList.do?cid=${param.cid}&page=${current+1}&order='+$F('order_select'),$('book_list').parentNode);">
<img
onmouseover='this.src="${pageContext.request.contextPath}/images/page_down_mouseover.gif"'
onmouseout='this.src="${pageContext.request.contextPath}/images/page_down.gif"'
src='${pageContext.request.contextPath}/images/page_down.gif' />
</a>
</c:when>
<c:otherwise>
<img
src='${pageContext.request.contextPath}/images/page_down_gray.gif'
align='absmiddle' />
</c:otherwise>
</c:choose>
</div>
</div>
</div>
reandyner 2010-01-28
  • 打赏
  • 举报
回复
package org.zmq.dao.hbn;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.zmq.dao.BookDao;
import org.zmq.dao.OrderItem;
import org.zmq.dao.exception.DaoException;
import org.zmq.util.HbnUtil;

public class BookDaoHbnImpl implements BookDao {
public static Logger logger = Logger.getLogger(BookDaoHbnImpl.class);

private Map<Integer, String> map = new HashMap<Integer, String>();

public BookDaoHbnImpl() {
map.put(OrderItem.ORDER_PRICE_DEFAULT, "findBook");
map.put(OrderItem.ORDER_PRICE_ASC, "findBookAsc");
map.put(OrderItem.ORDER_PRICE_DESC, "findBookDesc");

}

@Override
public List<?> findByCategoryId(Integer categoryId) throws DaoException {
Session session = HbnUtil.getSession();
List list = null;
try {
list = session.getNamedQuery("findBook").setParameter("categoryId",
categoryId).list();

} catch (HibernateException e) {
logger.error("", e);
throw new DaoException("数据库出错", e);
}
return list;
}

@Override
public List<?> findByCategoryId(Integer id, int rowPage, int page)
throws DaoException {

return this.findByCategoryId(id, rowPage, page,
OrderItem.ORDER_PRICE_DEFAULT);
}

@Override
public List<?> findByCategoryId(Integer categoryId, int rowPage, int page,
int order) throws DaoException {
Session session = HbnUtil.getSession();
List list = null;
try {
Query query = session.getNamedQuery(map.get(order)).setParameter(
"categoryId", categoryId);
query.setFirstResult((page - 1) * rowPage);
query.setMaxResults(rowPage);
list = query.list();
} catch (HibernateException e) {
logger.error("", e);
throw new DaoException("数据库出错", e);
}
return list;
}

@Override
public List<?> findById(Integer id) throws DaoException {
// TODO Auto-generated method stub
return null;
}

@Override
public int getTotalPage(Integer categoryId, int rowPage)
throws DaoException {
Session session = HbnUtil.getSession();
List list = null;
Integer total = 0;
try {

list = session.getNamedQuery("findCount").setParameter(
"categoryId", categoryId).list();
//Integer t = (Integer) list.get(0);
int t=((Long)list.get(0)).intValue();
total = t / rowPage;
if (t % rowPage != 0) {
total++;
}
} catch (HibernateException e) {
logger.error("", e);
throw new DaoException("数据库出错", e);
}
return total;
}

}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧