一个搜索框,然后会获取大量信息,将信息进行分页,每一页显示固定条数。
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";
}
配置文件省略