zoukankan      html  css  js  c++  java
  • 分页查询的具体实现

    首先,我们应该定义一个PageBean类,在类里我们封装分页查询所需要的各种属性以及生成各属性的get和set方法:

     1       //当前页
     2     private Integer currentPage;
     3     //总记录数
     4     private Integer totalCount;
     5     //每页显示的记录数
     6     private Integer pageSize;
     7     //总页数
     8     private Integer totalPage;
     9     //开始位置
    10     private Integer begin;
    11     //每页记录的list集合
    12     private List<Customer> list;

    然后,在action中定义了一个方法listpage,在listpage中获取页面所传过来的当前页,然后调用sercvice方法完成封装:

    1 public String listpage() {
    2         //调用service方法完成封装
    3         PageBean pageBean = customerService.listpage(currentPage);
    4         ServletActionContext.getRequest().setAttribute("pageBean", pageBean);
    5         return "listpage";
    6     }

    随后我们要在service方法中完成具体的封装:

     1 public PageBean listpage(Integer currentPage) {
     2         PageBean pageBean = new PageBean();
     3         //当前页
     4         pageBean.setCurrentPage(currentPage);
     5         //总记录数
     6         int totalCount = customerDao.findcount();
     7         pageBean.setTotalCount(totalCount);
     8         //每页显示记录数
     9         int pageSize = 3;
    10         pageBean.setPageSize(pageSize);
    11         //总页数 总记录数除以每页显示的记录数
    12         //判断是否能够整除
    13         int totalPage=0;
    14         if(totalCount%pageSize==0){
    15             totalPage = totalCount/pageSize;
    16         }else {
    17             totalPage = totalCount/pageSize+1;
    18         }
    19         pageBean.setTotalPage(totalPage);
    20         //开始位置
    21         int begin = (currentPage-1)*pageSize;
    22         pageBean.setBegin(begin);
    23         
    24         //每页纪录的list集合
    25         List<Customer> list = customerDao.findPage(begin,pageSize);
    26         pageBean.setList(list);
    27         return pageBean;
    28     }

    然后,在dao中写具体的查询总记录数和分页查询的方法,这里一共有两种方式;

     1 //查询记录数
     2     public int findcount() {
     3         @SuppressWarnings("all")
     4         List<Object> list = (List<Object>) this.getHibernateTemplate().find("select count(*) from Customer");
     5         //从list中把值得到
     6         if(list!=null && list.size()!=0){
     7             Object obj = list.get(0);
     8             Long lobj = (Long) obj;
     9             int count = lobj.intValue();
    10             return count;
    11         }
    12         return 0;
    13     }
    14 
    15     //分页查询操作
    16     @SuppressWarnings("all")
    17     public List<Customer> findPage(int begin, int pageSize) {
    18         //1.使用hibernate底层代码实现
    19 //        SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
    20 //        Session session = sessionFactory.getCurrentSession();
    21 //        Query query = session.createQuery("from Customer");
    22 //        query.setFirstResult(begin);
    23 //        query.setMaxResults(pageSize);
    24 //        List<Customer> list = query.list();
    25         
    26         //2.使用离线对象和hibernate模版实现
    27         //创建离线对象,设置对哪个实体类进行操作
    28         DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
    29         //调用hibernateTemplete进行操作
    30         List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
    31         return list;
    32     }

    最后在页面显示是应该判断当前页是否是第一页或最后一页

    1 共[<B>${pageBean.totalCount}</B>]条记录,共[<B>${pageBean.totalPage}</B>]页
    2  ,当前第[<b>${pageBean.currentPage}</b>]页
    3  <c:if test="${pageBean.currentPage!=1}">
    4  [<A href="${pageContext.request.contextPath }/customer_listpage.action?currentPage=${pageBean.currentPage-1}">前一页</A>]
    5   </c:if>
    6  <c:if test="${pageBean.currentPage!=pageBean.totalPage}">
    7  [<A href="${pageContext.request.contextPath }/customer_listpage.action?currentPage=${pageBean.currentPage+1}">后一页</A>] 
    8                                                 </c:if>
  • 相关阅读:
    javaWeb接口开发
    .NET设计模式(4):建造者模式(Builder Pattern)(转载)
    .net数据库连接池(转载)
    系统操作日志设计(转载)
    系统操作日志设计-代码实现(转载)
    GOF对Builder模式的定义(转载)
    Javascript中的对象和原型(一)(转载)
    Javascript中的对象和原型(二)(转载)
    Javascript中的对象和原型(三)(转载)
    服务器安全设置(转载)
  • 原文地址:https://www.cnblogs.com/cuibin/p/6740526.html
Copyright © 2011-2022 走看看