這是EasyJWeb文檔中樣本3(添刪改查)中相關記錄分頁顯示的部分代碼:
public class userManageAction extends AbstractCrudAction {
public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
....
package com.easyjf.web.tools;
import java.util.Collection;
import java.util.List;
public interface IPageList {
public List getResult();//取回分頁的結果
public void setQuery(IQuery q);//設定查詢處理器
public int getPages();//返回總頁數
public int getRowCount();//返回總記錄數
public int getCurrentPage();//返回當前頁
public void doList(int pageSize,int pageNo,String totalSQL,String queryHQL);//執行分頁處理
public void doList(int pageSize,int pageNo,String totalSQL,String queryHQL,Collection paraValues);//執行分頁處理
}
import java.util.*;
/**
* 實現通過調用IQuery實現分頁處理
* @author 蔡世友
*
*/
public class PageList implements IPageList{
private int rowCount;//記錄數
private int pages;//總頁數
private int currentPage;//實際頁數
private List result;
private IQuery query;
public PageList()
{
}
public PageList(IQuery q)
{
this.query=q;
}
public void setQuery(IQuery q)
{
query=q;
}
public List getResult()
{
return result;
}
public void doList(int pageSize, int pageNo, String totalSQL, String queryHQL) {
List rs=null;
int total=query.getRows(totalSQL);
if(total>0){
this.rowCount=total;
this.pages=(this.rowCount + pageSize - 1) / pageSize; //記算總頁數
int intPageNo=(pageNo>this.pages?this.pages:pageNo);
if(intPageNo<1)intPageNo=1;
this.currentPage=intPageNo;
if(pageSize>0){
query.setFirstResult( (intPageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
rs=query.getResult(queryHQL);
}
result=rs;
}
public void doList(int pageSize, int pageNo, String totalSQL, String queryHQL,Collection paraValues) {
List rs=null;
query.setParaValues(paraValues);
int total=query.getRows(totalSQL);
if(total>0){
this.rowCount=total;
this.pages=(this.rowCount + pageSize - 1) / pageSize; //記算總頁數
int intPageNo=(pageNo>this.pages?this.pages:pageNo);
if(intPageNo<1)intPageNo=1;
this.currentPage=intPageNo;
if(pageSize>0){
query.setFirstResult( (intPageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
rs=query.getResult(queryHQL);
}
result=rs;
}
public int getPages() {
return pages;
}
public int getRowCount() {
return rowCount;
}
public int getCurrentPage() {
return currentPage;
}
}
import org.hibernate.Query;
import org.hibernate.Session;
import com.easyjf.web.tools.IQuery;
public class DbQuery implements IQuery {
private Session session;
private int begin;
private int max;
private List paraValues;
public DbQuery(Session session)
{
this.session=session;
}
public void setParaValues(Collection paraValues) {
this.paraValues=(List)paraValues;
// System.out.println("參數":paraValues.size();)
}
public int getRows(String conditing) {
Query query1=session.createQuery(conditing);
if(paraValues!=null){
for(int i=0;i<paraValues.size();i++)
{
query1.setParameter(i,paraValues.get(i));
}
}
int total=((Integer)query1.uniqueResult()).intValue();
return total;
}
public List getResult(String conditing) {
Query query=session.createQuery(conditing);
if(paraValues!=null){
for(int i=0;i<paraValues.size();i++)
{
query.setParameter(i,paraValues.get(i));
}
}
if(begin>0)query.setFirstResult(begin);
if(max>0)query.setMaxResults(max);
return query.list();
}
public void setFirstResult(int begin) {
this.begin=begin;
}
public void setMaxResults(int max) {
this.max=max;
}
public List getResult(String conditing, int begin, int max) {
Query query=session.createQuery(conditing);
if(begin>0)query.setFirstResult(begin);
if(max>0)query.setMaxResults(max);
return query.list();
}
public void setParaValues(List paraValues) {
this.paraValues=paraValues;
}
}
public class ListQuery implements IQuery {
private int begin=0;
private int max=0;
private List list=null;
public ListQuery()
{
}
public ListQuery(List l)
{
if(l!=null){
this.list=l;
this.max=l.size();
}
}
public void initList(List l)
{
this.list=l;
this.max=l.size();
}
public int getRows(String conditing) {
return (list==null?0:list.size());
}
public List getResult(String conditing) {
return list.subList(begin,begin+max>list.size()?list.size():begin+max);
}
public void setFirstResult(int begin) {
this.begin=list.size()<begin?list.size():begin;
}
public void setMaxResults(int max) {
this.max=max;
}
public List getResult(String conditing, int begin, int max) {
return list;
}
public void setParaValues(Collection paraValues) {
// TODO Auto-generated method stub
}
}