spring+hibernate分页怎么做啊。有实例吗?

现在在线等!!!!!!!!!!!!!11谢了... 现在在线等!!!!!!!!!!!!!11谢了 展开
 我来答
zzw2009
推荐于2016-07-24
知道答主
回答量:3
采纳率:0%
帮助的人:0
展开全部
//PageInfo.java 定义一个分页的类
package ztdz.hibernate;

import java.util.List;

public class PageInfo {

public List list; //返回记录集
public String from; //hql from
public String where; //hql where
public String orderby; //hql orderby
public int totalCount=0; //总记录数
public int pageCount=0; //每页记录数
public int pageTotalNum=0; //总页数
public int curPage=0; //当前页
public boolean haveNext=false; //是否有下一页
public boolean havePre=false; //是否有上一页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageTotalNum() {
return pageTotalNum;
}
public void setPageTotalNum(int pageTotalNum) {
this.pageTotalNum = pageTotalNum;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public boolean isHaveNext() {
return haveNext;
}
public void setHaveNext(boolean haveNext) {
this.haveNext = haveNext;
}
public boolean isHavePre() {
return havePre;
}
public void setHavePre(boolean havePre) {
this.havePre = havePre;
}

}

//BaseDAO.java 数据访问的基础类. 分页的话使用getPage() 方法
/**
*
*/
package ztdz.hibernate.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ztdz.hibernate.PageInfo;
import ztdz.hibernate.dao.IBaseDAO;

/**
* @author zhangzhw
*
*/
public class BaseDAO extends HibernateDaoSupport implements IBaseDAO {

private static final Log log = LogFactory.getLog(BaseDAO.class);

public String from;
public String where;
public String orderBy;
public String queryString;

/**
* 实现增删改查的基本功能
*
* @author zhangzhw
* @serialData 2008年6月2日
*/

public List findAll(String string) {
// TODO Auto-generated method stub
log.debug("finding all records");
try {
return getHibernateTemplate().find(string);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public int getCount(String from, String where) {
String fromTrim = from.substring(from.indexOf("from"));

return (Integer) findRec("select count(*) " + fromTrim + " " + where)
.get(0);
}

public void attachClean(Object object) {

}

public void attachDirty(Object object) {

}

public void delete(Object object) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(object);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}

}

public List findRec(String string) {
this.queryString = string;
log.debug("finding records");
try {
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public void save(Object object) {
log.debug("saving Users instance");
try {
getHibernateTemplate().save(object);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}

}

public void merge(Object object) {
log.debug("saving Users instance");
try {
getHibernateTemplate().merge(object);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

/**
* 以下为按分页进行查询
*
* @author zhangzhw
* @serialData 2008年6月2日
*/

/**
* getPage 方法,取得第N页的信息
*/
public PageInfo getPage(String from, String where, String orderBy,
int pageNo, int pageCount) {

PageInfo pageInfo = new PageInfo();

pageInfo.from = from;
pageInfo.where = where;
pageInfo.orderby = orderBy;
pageInfo.pageCount = pageCount;

pageInfo.totalCount = getCount(from, where);

// 分页信息
if (pageInfo.totalCount % pageCount == 0) {
pageInfo.pageTotalNum = pageInfo.totalCount / pageInfo.pageCount;
if (pageInfo.pageTotalNum == 0)
pageInfo.pageTotalNum = 1;
} else
pageInfo.pageTotalNum = pageInfo.totalCount / pageCount + 1;

// 设置当前页
if (pageNo < 1)
pageInfo.curPage = 1;
else if (pageNo > pageInfo.pageTotalNum)
pageInfo.curPage = pageInfo.pageTotalNum;
else
pageInfo.curPage = pageNo;

// 设置上页下页信息
if (pageNo < pageInfo.pageTotalNum)
pageInfo.haveNext = true;
if (pageNo > 1)
pageInfo.havePre = true;

// 分页查询代码报错
// pageInfo.list=findRec(from + where + " limit " + (pageNo-1)*pageCount
// + " , " + pageCount +" " + orderBy);
// pageInfo.list=findRec(from +where + " limit 1,3 " +orderBy );
// 不报错的实现方法
pageInfo.list = findRec(from + where + orderBy, (pageInfo.curPage - 1)
* pageCount, pageCount);
return pageInfo;

}

public PageInfo getPage(String hql, int pageNo, int pageCount) {
// 分解hql 为 from where orderBy
String from, where, orderBy;
from = parseHql(hql, 1);
where = parseHql(hql, 2);
orderBy = parseHql(hql, 3);
return getPage(from, where, orderBy, pageNo, pageCount);
}

public PageInfo getPage(String hql, int pageNo) {
return getPage(hql, pageNo, 20);
}

// 附加方法,拆解hql (简单hql,不使用select 不使用 limit等)
private String parseHql(String hql, int type) {
switch (type) {
case 1:
if (hql.indexOf("where") > 0)
return hql.substring(1, hql.indexOf("where") - 1);
else if (hql.indexOf("oderby") > 0)
return hql.substring(1, hql.indexOf("orderby") - 1);
else
return null;
case 2:
if (hql.indexOf("where") > 0) {
if (hql.indexOf("orderby") > 0)
return hql.substring(hql.indexOf("where"), hql
.indexOf("orderby") - 1);
else
return hql.substring(hql.indexOf("where"));
} else
return null;

case 3:
if (hql.indexOf("orderby") > 0)
return hql.substring(hql.indexOf("orderby"));
else
return null;

}

return null;
}

// 按指定分页记录查询的方法
public List findRec(final String hql, final int firstResult,
final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {

Query query = s.createQuery(hql);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
List list = query.list();
return list;
}
});
}

public boolean transSave(final List list) {

return (Boolean) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {

try {
for (Object o : list) {
s.save(o);

}

} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}
});

}

public boolean transSave(final List[] list) {

return (Boolean) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {

try {
for (int i = 0; i < list.length; i++) {
for (Object o : list[i]) {
s.save(o);

}
}

} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}
});

}

public boolean transSave(final List[] list,boolean flag)
{
return (Boolean) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {

try {
for (int i = 0; i < list.length; i++) {
for (Object o : list[i]) {
s.saveOrUpdate(o);

}
}

} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}
});
}

}

其他配置就不写了. 如果还不懂,建议从基础学习吧.
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式