不带条件的简单分页查询。
主要用到:
Query query = getSessionDao().createQuery(hql1);
及两个分页条件:
query.setFirstResult(firstPage*pageSize);
query.setMaxResults(pageSize);
/**
* 不带条件的简单分页查询
* @author lxh
* @version 1.0
*
*/
public class QueryListByPage extends HibernateDaoImpl{
/**
* * 简单的分页的查询数据列表
*
* @param modelName
* 实体类名
* @param firstPage
* 查询第几页
* @param pageSize
* 每页显示几条数据
* @return 一页的数据列表,和总页数
*/
@SuppressWarnings("unchecked")
public QueryResult findAllList(String modelName,int firstPage,int pageSize) {
String hql1="from "+modelName;
String hql2="select count(*) from "+modelName;
Query query = getSessionDao().createQuery(hql1);
query.setFirstResult(firstPage*pageSize);
query.setMaxResults(pageSize);
Long count = (Long) getSessionDao().createQuery(hql2).uniqueResult();
int totalPageNum=count.intValue()/pageSize+1;//总页数
List list = query.list();
// 返回结果
return new QueryResult(totalPageNum, list);
/**
*
* Map map = new HashMap();
* map.put("xx",list);
* map.put("xxx",totalPageNum);
* JSONObject jobj = JSONTUtil.toObject(map);
*
*/
}
}
import java.util.List;
public class QueryResult {
private int totalPageNum; // 总记录数
private List list; // 一页的数据
public QueryResult(int totalPageNum, List list) {
this.totalPageNum = totalPageNum;
this.list = list;
}
public int getTotalPageNum() {
return totalPageNum;
}
public void setTotalPageNum(int totalPageNum) {
this.totalPageNum = totalPageNum;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
时间: 2024-10-22 04:15:29