一個搜索框,然后會獲取大量信息,將信息進行分頁,每一頁顯示固定條數。
mysql中使用“like”和“%%”進行模糊匹配,用“limit”進行分頁。
1.首先創建一個頁面信息的實體類,代碼如下:
import java.util.List; public class PageResult1 { private List dataList;//滿足查詢條件后的所有數據 public List getDataList() { return dataList; } public void setDataList(List dataList) { this.dataList = dataList; } //當前頁 private int currentPage; //首頁 private int firstPage=1; //尾頁 private int lastPage; //上一頁 private int prePage; //下一頁 private int nextPage; //總數 private int totalCount; //每頁條數 private int pageSize=2; public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getFirstPage() { return firstPage; } public void setFirstPage(int firstPage) { this.firstPage = firstPage; } public int getLastPage() { return lastPage; } public void setLastPage(int lastPage) { this.lastPage = lastPage; } public int getPrePage() { return prePage; } public void setPrePage(int prePage) { this.prePage = prePage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public PageResult1(List dataList, int currentPage,int pageSize, int totalCount) {//只有datalist和totalCount需要自己寫方法得到 super(); this.dataList=dataList; this.currentPage = currentPage; this.firstPage = 1; this.pageSize = pageSize; this.totalCount = totalCount; //這邊要按順序寫,用myeclipse自動生成的有參構造,順序會有問題,這樣可能無法計算出需要的數值 this.prePage = currentPage>1 ? currentPage-1 : currentPage; this.lastPage = totalCount%pageSize==0 ? totalCount/pageSize : totalCount/pageSize+1; this.nextPage = currentPage<lastPage ? currentPage+1 : currentPage; } public PageResult1() { super(); // TODO Auto-generated constructor stub } }
2.編寫dao實現類中的方法
需要兩個方法分別是:
//查詢某個商品的數量
public int findGoods(Integer number);
//分頁
public List<Goodsdetail> limit(Integer number,int currentPage,int pageSize);
public class GoodsDetailImpl implements IGoodsDetailDao{ private HibernateTemplate hibernateTemplate; private Goodsdetail goodsdetail; @Override public int findGoods(Integer number) { Session session = hibernateTemplate.getSessionFactory().getCurrentSession(); List paraList=new ArrayList<>(); StringBuffer sb=new StringBuffer("from Goodsdetail g where 1=1 "); if(number!=null){ sb.append(" and g.number = ? "); paraList.add(number); } Query query = session.createQuery(sb.toString()); for (int i = 0; i < paraList.size(); i++) { query.setParameter(i, paraList.get(i)); } List<Goodsdetail> list=query.list(); int totalCount=list.size(); return totalCount; } @Override public List<Goodsdetail> limit(Integer number, int currentPage, int pageSize) { Session session = hibernateTemplate.getSessionFactory().getCurrentSession(); List paraList=new ArrayList<>(); StringBuffer sb=new StringBuffer("from Goodsdetail g where 1=1 "); if(number!=null){ sb.append(" and g.number = ? ");//這是精確查找,如果改成模糊匹配,把“=”改成like,然后add(‘%’+number+‘%’) paraList.add(number); } Query query = session.createQuery(sb.toString()); for (int i = 0; i < paraList.size(); i++) { query.setParameter(i, paraList.get(i)); } List<Goodsdetail> list=query.setFirstResult((currentPage-1)*pageSize).setMaxResults(pageSize).list();//選擇用hql語句進行分頁 return list; } }
3.jsp頁面
js函數:
function changesearch(currentPage){
$("#currentPage").val(currentPage);
$("#listform").action="goodsDetail_limit.action";//listform整個表單的名字,訪問action中分頁查詢的方法
$("#listform").submit();
}
<form method="post" action="" id="listform">
<input type="hidden" value="${page.currentPage}" name="page.currentPage" id="currentPage">//設置隱藏域,將當前頁的信息傳遞給action
</form>
<tr> <td colspan="8"><div class="pagelist"><a href="javascript:changesearch(${page.firstPage})">首頁</a> <a href="javascript:changesearch(${page.prePage})">上一頁</a> ${page.currentPage} <a href="javascript:changesearch(${page.nextPage})">下一頁</a> <a href="javascript:changesearch(${page.lastPage})">尾頁</a> </div></td> </tr>
4.action中
省略set get方法和一些其他的屬性,只寫方法
public String limit(){
int pageSize=1;
int totalCount=goodsDetailService.findGoods(number);//Spring動態代理
if(page==null){
int currentPage=1;
list=goodsDetailService.limit(number, currentPage, pageSize);
page=new PageResult1(list, currentPage, pageSize, totalCount);
}else{
list=goodsDetailService.limit(number, page.getCurrentPage(), pageSize);
page=new PageResult1(list, page.getCurrentPage(), pageSize, totalCount);
}
return "ddd";
}
配置文件省略