struts2 spring ,hibernate,做分页查询,action代码怎么写
2个回答
展开全部
1、定义一个分页的类PageInfo.java:
2、用hibernate进行分页。
3、Action中直接调用getPageInfo方法,返回的就是PageInfo类。
public class PageInfo {
private int totalNum; // 总记录数
private int pageNum; // 总页数
private int current = 1; // 当前页数
private int limit = 25; // 每页记录数
@SuppressWarnings("unchecked")
private List result; // 分页记录信息
private int start; // 开始记录索引号
private int type;
// private boolean first; // 是否是第一页
// private boolean last; // 是否是最后一页
/**
* @return the type .
*/
public int getType() {
return this.type;
}
/**
* @param type the type to set
*/
public void setType(int type) {
this.type = type;
}
public PageInfo() {
super();
}
@SuppressWarnings("unchecked")
public PageInfo(List result, int totalNum, int limit) {
this.result = result;
this.totalNum = totalNum;
this.limit = limit;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
this.getPageNum();
}
public int getPageNum() {
if (totalNum <= 0) {
this.pageNum = 1;
} else {
this.pageNum = totalNum % limit == 0 ? totalNum / limit : totalNum
/ limit + 1;
}
return this.pageNum;
}
public int getCurrent() {
if (totalNum == 0) {
return 1;
} else if (current <= 1) {
return 1;
} else if (current >= pageNum) {
return pageNum;
} else {
return current;
}
}
public void setCurrent(int current) {
this.current = current;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
@SuppressWarnings("unchecked")
public List getResult() {
return result;
}
@SuppressWarnings("unchecked")
public void setResult(List result) {
this.result = result;
}
public int getCurrentPageIndex() {
return (this.getCurrent() - 1) * this.getLimit();
}
public boolean isFirst() {
return getCurrent() == 1;
}
public boolean isLast() {
return getCurrent() >= getPageNum();
}
public int getStart() {
return this.start;
}
public void setStart(int start) {
this.start = start;
}
}
2、实现分页方法
//参数:hql--hql语句,start从哪一条数据开始查,limit每次查多少条数据,values为hql语句的参数值。
public class BaseHibernateDao extends HibernateDaoSupport{
public PageInfo getPageInfo(final String hql, final int start,
final int limit, final Object[] values) {
int total = getTotal(getCountSql(hql.toString()), values);
List list = (List) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) {
// 判断是否包含order by语句
Query query = session.createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query.setFirstResult(start).setMaxResults(limit)
.list();
}
});
PageInfo pageInfo = new PageInfo(list, total, limit);
return pageInfo;
}}
2、用hibernate进行分页。
3、Action中直接调用getPageInfo方法,返回的就是PageInfo类。
public class PageInfo {
private int totalNum; // 总记录数
private int pageNum; // 总页数
private int current = 1; // 当前页数
private int limit = 25; // 每页记录数
@SuppressWarnings("unchecked")
private List result; // 分页记录信息
private int start; // 开始记录索引号
private int type;
// private boolean first; // 是否是第一页
// private boolean last; // 是否是最后一页
/**
* @return the type .
*/
public int getType() {
return this.type;
}
/**
* @param type the type to set
*/
public void setType(int type) {
this.type = type;
}
public PageInfo() {
super();
}
@SuppressWarnings("unchecked")
public PageInfo(List result, int totalNum, int limit) {
this.result = result;
this.totalNum = totalNum;
this.limit = limit;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
this.getPageNum();
}
public int getPageNum() {
if (totalNum <= 0) {
this.pageNum = 1;
} else {
this.pageNum = totalNum % limit == 0 ? totalNum / limit : totalNum
/ limit + 1;
}
return this.pageNum;
}
public int getCurrent() {
if (totalNum == 0) {
return 1;
} else if (current <= 1) {
return 1;
} else if (current >= pageNum) {
return pageNum;
} else {
return current;
}
}
public void setCurrent(int current) {
this.current = current;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
@SuppressWarnings("unchecked")
public List getResult() {
return result;
}
@SuppressWarnings("unchecked")
public void setResult(List result) {
this.result = result;
}
public int getCurrentPageIndex() {
return (this.getCurrent() - 1) * this.getLimit();
}
public boolean isFirst() {
return getCurrent() == 1;
}
public boolean isLast() {
return getCurrent() >= getPageNum();
}
public int getStart() {
return this.start;
}
public void setStart(int start) {
this.start = start;
}
}
2、实现分页方法
//参数:hql--hql语句,start从哪一条数据开始查,limit每次查多少条数据,values为hql语句的参数值。
public class BaseHibernateDao extends HibernateDaoSupport{
public PageInfo getPageInfo(final String hql, final int start,
final int limit, final Object[] values) {
int total = getTotal(getCountSql(hql.toString()), values);
List list = (List) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) {
// 判断是否包含order by语句
Query query = session.createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query.setFirstResult(start).setMaxResults(limit)
.list();
}
});
PageInfo pageInfo = new PageInfo(list, total, limit);
return pageInfo;
}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询