📄 pagedao.java
字号:
package com.syfxapp.util;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageDao extends HibernateDaoSupport {
/*实现数据库级的分页*/
private int totalRows;
private int totalPages;
private int firstRes;//起始记录
private int maxRes;//每页显示的记录数
private int currentPageNo;//当前页
private int nextPageNo;//下一页
private int beforePageNo;//前一页
private int firstPageNo=1;//首页
private int lastPageNo;//最后一页
private String hql;
public void init(String hql,int currentPageNo,int maxRes){
this.hql=hql;
this.maxRes=maxRes;
this.currentPageNo=currentPageNo;
this.firstRes=(currentPageNo-1)*this.maxRes;
Session session = getSession();
Query q =session.createQuery(hql);
List l = q.list();
this.totalRows=l.size();//获取总的记录数
this.totalPages = totalRows/maxRes;
if(totalRows%maxRes!=0)
this.totalPages=this.totalPages+1;
if(currentPageNo<totalPages){
nextPageNo=currentPageNo+1;
}else{
nextPageNo=currentPageNo;
}
if(currentPageNo>1){
beforePageNo=currentPageNo-1;
}else{
beforePageNo=currentPageNo;
}
this.lastPageNo=this.totalPages;
this.releaseSession(session);
}
public List execQuery(){
Session session = getSession();
Query q =session.createQuery(this.hql);
q.setFirstResult(firstRes);
q.setMaxResults(maxRes);
List list = q.list();
this.releaseSession(session);
return list;
}
public int getTotalRows() {
return totalRows;
}
public int getTotalPages() {
return totalPages;
}
public int getFirstRes() {
return firstRes;
}
public int getMaxRes() {
return maxRes;
}
public int getCurrentPageNo() {
return currentPageNo;
}
public int getNextPageNo() {
return nextPageNo;
}
public int getBeforePageNo() {
return beforePageNo;
}
public int getFirstPageNo() {
return firstPageNo;
}
public int getLastPageNo() {
return lastPageNo;
}
public void setLastPageNo(int lastPageNo) {
this.lastPageNo = lastPageNo;
}
public String getHql() {
return hql;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -