我的辦法是讓實現分頁的類繼承HibernateDaoSupport這個類,然後才能利用這個類的 getHibernateTemplate().find()方法查詢得到的結果集與hibernate的Query類的setFirstResult()和setMaxResults()去實現。核心代碼如下:
接口裏的方法主要就是根據傳入參數拼成查詢條件,增加一個頁的參數page
public interface GetServiceCase {
public Pages getPages();
public Collection getServiceCaseByPage(String keyword, String createDate,
String statusId, String typeId, String id, String operatorId,
String area, String page);
}
具體實現類
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
public class GetServiceCaseImpl extends HibernateDaoSupport implements
GetServiceCase {
Pages thepages = null;//這個類屬性用來保存頁信息
public Collection getServiceCaseByPage(String keyword, String createDate,
String statusId, String typeId, String id, String operatorId,
String area, String page) {
boolean whereAdded = false;
String hql = "from ServiceCase scase ";
if (keyword != null && !keyword.trim().equals("")) {
hql = hql + " where scase.name = /'" + keyword + "/' ";
whereAdded = true;
}
if (createDate != null && !createDate.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " to_char(scase.createDate, /'yyyy-mm-dd/') = /'"
+ createDate + "/' ";
}
if (statusId != null && !statusId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.status.id = " + statusId;
}
if (typeId != null && !typeId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.type.id = " + typeId;
}
if (id != null && !id.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.id = " + id;
}
if (operatorId != null && !operatorId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.worker.id = " + operatorId;
}
if (area != null && !area.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.area = /'" + area + "/' ";//查詢語句
}
//System.out.println("the sql is :" + hql);
int ipage = 1;
try {
ipage = Integer.parseInt(page);
} catch (NumberFormatException e) {//對傳入的page做好防備
ipage = 1;
e.printStackTrace();
}
int startRow = 0;//開始行
int totalpage = 1;//總頁數
int totalrecord = 0;//總記錄數
int pagesize = 20;//頁的大小
List list = new ArrayList();
try {
list = getHibernateTemplate().find(hql);//這個方法返回list
} catch (DataAccessException e) {
e.printStackTrace();
}
if (list.size() > 0) {
totalrecord = list.size();
//System.out.println(" get record size = " + list.size());
}else{
return null;//沒有記錄就直接返回
}
if(totalrecord != 0 && totalrecord < pagesize){
pagesize = totalrecord;
}
if (totalrecord % pagesize == 0) {
totalpage = totalrecord / pagesize;
} else {
totalpage = totalrecord / pagesize + 1;
}
//System.out.println("pagesize = " + pagesize);
//System.out.println("totalpage = " + totalpage);
if (ipage > totalpage) {
ipage = totalpage;
}
if (ipage < 1) {
ipage = 1;
}
if (ipage % totalpage == 0) {
startRow = (ipage - 1) * pagesize;
pagesize = totalrecord - startRow;//若記錄不足一頁,頁大小以實際爲準
} else {
startRow = (ipage - 1) * pagesize;//設置開始行
}
thepages = new Pages();
thepages.setThepage(String.valueOf(ipage));
thepages.setTotalpage(String.valueOf(totalpage));
try {
Query query = null;
Session session = getSession();
query = session.createQuery(hql);
query.setFirstResult(startRow);
query.setMaxResults(pagesize);
list = query.list();
return list;
} catch (HibernateException e) {
e.printStackTrace();
}
return null;
}
public Pages getPages() {
return thepages;
}
}
保存頁信息的類
public class Pages {
String thepage;
String totalpage;
/**
* @return Returns the thepage.
*/
public String getThepage() {
return thepage;
}
/**
* @param thepage The thepage to set.
*/
public void setThepage(String thepage) {
this.thepage = thepage;
}
/**
* @return Returns the totalpage.
*/
public String getTotalpage() {
return totalpage;
}
/**
* @param totalpage The totalpage to set.
*/
public void setTotalpage(String totalpage) {
this.totalpage = totalpage;
}
}
action中的部分代碼
public ActionForward process(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) {
DynaActionForm form = (DynaActionForm) arg1;
if (doAction != null && !doAction.equals("")) { // 提交動作
form.set("doAction", null);
String keyword = (String)form.get("keyword");
String createDate = (String)form.get("createDate");
String statusId = (String)form.get("statusId");
String typeId = (String)form.get("typeId");
String id = (String)form.get("id");
String operatorId = (String)form.get("operatorId");
String area = (String)form.get("area");
GetServiceCase getServiceCase = (GetServiceCase)SpringServiceUtil.getServiceObject(arg2,"GetServiceCase");
Collection list = getServiceCase.getServiceCaseByPage(keyword,
createDate, statusId, typeId, id, operatorId, area,page);
if(list != null){
Pages apage = getServiceCase.getPages();
arg2.setAttribute("page",apage.getThepage());
arg2.setAttribute("totalpage",apage.getTotalpage());
arg2.setAttribute("case_list", list);
arg2.setAttribute("keyword",keyword);
arg2.setAttribute("createDate",createDate);
arg2.setAttribute("statusId",statusId);
arg2.setAttribute("typeId",typeId);
arg2.setAttribute("id",id);
arg2.setAttribute("operatorId",operatorId);
arg2.setAttribute("area",area);
}
return arg0.findForward("search_result");
} else { // 初始化頁面
return arg0.findForward("init");
}
}
JSP中的部分實現
<logic:present name="case_list">
<%
int thepage = Integer.parseInt((String)request.getAttribute("page"));
int tpage = Integer.parseInt((String)request.getAttribute("totalpage"));
%>
<p>
第<%=thepage%>頁
頁數:<font color="red"><bean:write name="page"/>/<bean:write name="totalpage"/></font>
共<bean:write name="totalpage"/>頁
<logic:notEqual name="page" value="1">
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=1" >首頁</a>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<%=thepage - 1%>" >上一頁</a>
</logic:notEqual>
<%
if(thepage != tpage){
%>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<%=thepage + 1%>" >下一頁</a>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<bean:write name="totalpage" />" >尾頁</a>
<%
}
%>
輸入頁數:<input type="text" name="page" size="3"/>
<input type=submit name="doAction" value="go">
<input type=hidden name="keyword" value="<bean:write name="keyword"/>">
<input type=hidden name="createDate" value="<bean:write name="createDate"/>">
<input type=hidden name="statusId" value="<bean:write name="statusId"/>">
<input type=hidden name="typeId" value="<bean:write name="typeId"/>">
<input type=hidden name="id" value="<bean:write name="id"/>">
<input type=hidden name="operatorId" value="<bean:write name="operatorId"/>">
<input type=hidden name="area" value="<bean:write name="area"/>">
</logic:present>