pageBean实现分页

废话不多说,直接上代码!

PageBean类

 1 package com.xujingyang.domain ;
 2 
 3 import java.util.List ;
 4 
 5 /**
 6  * @author oldmonk
 7  * @date   2017年4月1日
 8  * @param <E>
 9  */
10 public class PageBean<E> {
11     
12     private List<E>    bean ;            // 存放实体类集合
13                                     
14     private int        currentPage ;    // 当前页
15     private int        pageSize ;        // 每页显示的条数
16     private int        totalPage ;        // 总页数
17     private int        totalCount ;    // 总条数
18     
19     public List<E> getBean() {
20         return bean ;
21     }
22     
23     public void setBean(List<E> bean) {
24         this.bean = bean ;
25     }
26     
27     public int getCurrentPage() {
28         return currentPage ;
29     }
30     
31     public void setCurrentPage(int currentPage) {
32         this.currentPage = currentPage ;
33     }
34     
35     public int getPageSize() {
36         return pageSize ;
37     }
38     
39     public void setPageSize(int pageSize) {
40         this.pageSize = pageSize ;
41     }
42     
43     public int getTotalPage() {
44         return (totalCount + pageSize - 1) / pageSize ;
45     }
46     
47     public void setTotalCount(int totalCount) {
48         this.totalCount = totalCount ;
49     }
50     
51 }

 

dao层实现实例

 1 /* 根据分类查询信息
 2      * @see com.xujingyang.dao.IProductDao#getCurrentPageBean(com.xujingyang.domain.PageBean, java.lang.String)
 3      */
 4     @Override
 5     public List<Product> getCurrentPageBean(PageBean<Product> pBean, String cid)
 6             throws SQLException {
 7         QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
 8         String sql="SELECT * FROM product WHERE cid=? ORDER BY pdate DESC LIMIT ?,?";
 9         return runner.query(sql, new BeanListHandler<Product>(Product.class),cid,(pBean.getCurrentPage()-1)*pBean.getPageSize(),
          pBean.getPageSize());
10 }

 

Service层对PageBean进行封装

 1 /* 分页查询
 2      * @see com.xujingyang.service.IProductService#getByPage(com.xujingyang.domain.PageBean, java.lang.String)
 3      */
 4     @Override
 5     public PageBean<Product> getByPage(PageBean<Product> pBean, String cid) throws SQLException {
 6         IProductDao dao=(IProductDao) BeanFactory.getBeanClass("ProductDao");
 7         
 8         //查询总条数
 9         int totalCount=dao.getTotalCount(cid);
10         
11         //查询当前页的数据
12         List<Product> lProducts=dao.getCurrentPageBean(pBean,cid);
13         
14         PageBean< Product> pBean2=new PageBean<Product>();
15         pBean2.setTotalCount(totalCount);
16         pBean2.setBean(lProducts);
17         pBean2.setCurrentPage(pBean.getCurrentPage());
18         pBean2.setPageSize(pBean.getPageSize());
19         return pBean2;
20     }

 

前台EL表达式分析

 1 <!--分页 -->
 2         <div style="width:380px;margin:0 auto;margin-top:50px;">
 3             <ul class="pagination" style="text-align:center; margin-top:10px;">
 4             <c:if test="${pBean.currentPage<=1 }">
 5                 <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
 6             </c:if>
 7             <c:if test="${pBean.currentPage>1 }">
 8                 <li><a href="${pageContext.request.contextPath }/product?method=findByPage&currentPage=${pBean.currentPage-1}&cid=
              ${pBean.bean[0].cid }"
aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li> 9 </c:if> 10 11 <!-- <li class="active"><a href="#">1</a></li> --> 12 <c:forEach begin="${pBean.currentPage-5>0?pBean.currentPage-5:1}" end=
                "${pBean.currentPage+4>pBean.totalPage ?pBean.totalPage:pBean.currentPage+4}"
var="i"> 13 <li><a href="${pageContext.request.contextPath }/product?method=findByPage&currentPage=${i }
                &cid=${pBean.bean[0].cid }"
>${i }</a></li> 14 </c:forEach> 15 16 <c:if test="${pBean.currentPage>=pBean.totalPage }"> 17 <li class="disabled"> 18 <a href="#" aria-label="Next"> 19 <span aria-hidden="true">&raquo;</span> 20 </a> 21 </li> 22 </c:if> 23 <c:if test="${pBean.currentPage<pBean.totalPage }"> 24 <li> 25 <a href="${pageContext.request.contextPath }/product?method=findByPage&currentPage=
                  ${pBean.currentPage+1}&cid=${pBean.bean[0].cid }"
aria-label="Next"> 26 <span aria-hidden="true">&raquo;</span> 27 </a> 28 </li> 29 </c:if> 30 </ul> 31 </div> 32 <!-- 分页结束======================= -->

 

posted @ 2017-04-01 11:00  oldmonk  阅读(5881)  评论(2编辑  收藏  举报
Fork me on GitHub