jpa 分页功能1

来源:互联网 发布:韩国人和日本人知乎 编辑:程序博客网 时间:2024/04/28 22:29
1.service 层
public Result findAllAdvertise(Page page,String categoryId){    StringBuffer sql1 = new StringBuffer();    StringBuffer sql2 = new StringBuffer();    sql1.append("select count(*) from Advertise a");    sql2.append("from Advertise a where 1=1");    if(categoryId != null && !categoryId.equals("")){    sql1.append(" where a.categoryId = ?");    sql2.append(" and a.categoryId = ?");    sql2.append(" order by a.createtime desc");        page = PageUtil.createPage(page, (Long) em.createQuery(sql1.toString()).setParameter(1,categoryId).getSingleResult());    return new Result(page,em.createQuery(sql2.toString()).setParameter(1,categoryId).setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()).getResultList());    }else{    sql2.append(" order by a.createtime desc");    page = PageUtil.createPage(page, (Long) em.createQuery(sql1.toString()).getSingleResult());        return new Result(page,em.createQuery(sql2.toString()).setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()).getResultList());    }    }

2.action 层

@RequestMapping("/webadmin/AderviseList.htm")
 public String AderviseList(HttpServletRequest request){
  String categoryId = request.getParameter("categoryId");
  if(categoryId ==null ||categoryId.equals("")){
   categoryId = (String)request.getAttribute("categoryId");
  }
  String currpage =request.getParameter("page");
  if(currpage == null || currpage.equals("")){
   currpage ="1";
  }
  Page page = new Page(20);
  page.setCurrentPage(new Integer(currpage));
  
  Result list = aderviseBiz.findAllAdvertise(page, categoryId);
  request.setAttribute("aderviselist", list);
  
  //查询所有类别
  List categoryList = aderviseBiz.findAllAdvertiseCategory();
  request.setAttribute("categoryList",categoryList);
     request.setAttribute("categoryId", categoryId);
  return "/webadmin/AderviseList.vm";
 }

0 0
原创粉丝点击