mybatis结合分页的使用及解析.

首先说明: 这里分页是使用了SSM框架+ jsp 来做的, 当然分页还有其他的很多做法, 比如easyUI自带的分页效果. 但是这些原理都是很相似的, 再次只做为学习总结之用.


一, 效果图


这里的截图是来自百度, 当然我们的项目也是做成这种效果, 当点击第10页时, 相应页码数也要发生相应变化.


二, 代码实例
1, 首先我们在项目中加入了一个page 的jar包, 这个jar包有3个java文件组成, 这个jar包是事先写好的通用分页插件.

另外这里将源码也写上来:
Paginable.java:

 1 package cn.itcast.common.page;
 2 
 3 /**
 4  * 分页接口
 5  */
 6 public interface Paginable {
 7     /**
 8      * 总记录数
 9      * 
10      * @return
11      */
12     public int getTotalCount();
13 
14     /**
15      * 总页数
16      * 
17      * @return
18      */
19     public int getTotalPage();
20 
21     /**
22      * 每页记录数
23      * 
24      * @return
25      */
26     public int getPageSize();
27 
28     /**
29      * 当前页号
30      * 
31      * @return
32      */
33     public int getPageNo();
34 
35     /**
36      * 是否第一页
37      * 
38      * @return
39      */
40     public boolean isFirstPage();
41 
42     /**
43      * 是否最后一页
44      * 
45      * @return
46      */
47     public boolean isLastPage();
48 
49     /**
50      * 返回下页的页号
51      */
52     public int getNextPage();
53 
54     /**
55      * 返回上页的页号
56      */
57     public int getPrePage();
58 }
View Code

SimplePage.java:

  1 package cn.itcast.common.page;
  2 
  3 /**
  4  * 简单分页类
  5  */
  6 public class SimplePage implements java.io.Serializable,Paginable {
  7     
  8     private static final long serialVersionUID = 1L;
  9     public static final int DEF_COUNT = 20;
 10 
 11     /**
 12      * 检查页码 checkPageNo
 13      * 
 14      * @param pageNo
 15      * @return if pageNo==null or pageNo<1 then return 1 else return pageNo
 16      */
 17     public static int cpn(Integer pageNo) {
 18         return (pageNo == null || pageNo < 1) ? 1 : pageNo;
 19     }
 20 
 21     public SimplePage() {
 22     }
 23 
 24     /**
 25      * 构造器
 26      * 
 27      * @param pageNo
 28      *            页码
 29      * @param pageSize
 30      *            每页几条数据
 31      * @param totalCount
 32      *            总共几条数据
 33      */
 34     public SimplePage(int pageNo, int pageSize, int totalCount) {
 35         setTotalCount(totalCount);
 36         setPageSize(pageSize);
 37         setPageNo(pageNo);
 38         adjustPageNo();
 39         
 40     }
 41 
 42     /**
 43      * 调整页码,使不超过最大页数
 44      */
 45     public void adjustPageNo() {
 46         if (pageNo == 1) {
 47             return;
 48         }
 49         int tp = getTotalPage();
 50         if (pageNo > tp) {
 51             pageNo = tp;
 52         }
 53     }
 54 
 55     /**
 56      * 获得页码
 57      */
 58     public int getPageNo() {
 59         return pageNo;
 60     }
 61 
 62     /**
 63      * 每页几条数据
 64      */
 65     public int getPageSize() {
 66         return pageSize;
 67     }
 68 
 69     /**
 70      * 总共几条数据
 71      */
 72     public int getTotalCount() {
 73         return totalCount;
 74     }
 75 
 76     /**
 77      * 总共几页
 78      */
 79     public int getTotalPage() {
 80         int totalPage = totalCount / pageSize;
 81         if (totalPage == 0 || totalCount % pageSize != 0) {
 82             totalPage++;
 83         }
 84         return totalPage;
 85     }
 86 
 87     /**
 88      * 是否第一页
 89      */
 90     public boolean isFirstPage() {
 91         return pageNo <= 1;
 92     }
 93 
 94     /**
 95      * 是否最后一页
 96      */
 97     public boolean isLastPage() {
 98         return pageNo >= getTotalPage();
 99     }
100 
101     /**
102      * 下一页页码
103      */
104     public int getNextPage() {
105         if (isLastPage()) {
106             return pageNo;
107         } else {
108             return pageNo + 1;
109         }
110     }
111 
112     /**
113      * 上一页页码
114      */
115     public int getPrePage() {
116         if (isFirstPage()) {
117             return pageNo;
118         } else {
119             return pageNo - 1;
120         }
121     }
122 
123     protected int totalCount = 0;
124     protected int pageSize = 20;
125     protected int pageNo = 1;
126 
127     /**
128      * if totalCount<0 then totalCount=0
129      * 
130      * @param totalCount
131      */
132     public void setTotalCount(int totalCount) {
133         if (totalCount < 0) {
134             this.totalCount = 0;
135         } else {
136             this.totalCount = totalCount;
137         }
138     }
139 
140     /**
141      * if pageSize< 1 then pageSize=DEF_COUNT
142      * 
143      * @param pageSize
144      */
145     public void setPageSize(int pageSize) {
146         if (pageSize < 1) {
147             this.pageSize = DEF_COUNT;
148         } else {
149             this.pageSize = pageSize;
150         }
151     }
152 
153     /**
154      * if pageNo < 1 then pageNo=1
155      * 
156      * @param pageNo
157      */
158     public void setPageNo(int pageNo) {
159         if (pageNo < 1) {
160             this.pageNo = 1;
161         } else {
162             this.pageNo = pageNo;
163         }
164     }
165 }
View Code

 

Pagination.java:

  1 package cn.itcast.common.page;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 /**
  7  * 列表分页。包含list属性。
  8  */
  9 public class Pagination extends SimplePage{
 10 
 11     public Pagination() {
 12     }
 13 
 14     /**
 15      * 构造器
 16      * 
 17      * @param pageNo
 18      *            页码
 19      * @param pageSize
 20      *            每页几条数据
 21      * @param totalCount
 22      *            总共几条数据
 23      */
 24     public Pagination(int pageNo, int pageSize, int totalCount) {
 25         super(pageNo, pageSize, totalCount);
 26         
 27     }
 28 
 29     /**
 30      * 构造器
 31      * 
 32      * @param pageNo
 33      *            页码
 34      * @param pageSize
 35      *            每页几条数据
 36      * @param totalCount
 37      *            总共几条数据
 38      * @param list
 39      *            分页内容
 40      */
 41     public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) {
 42         super(pageNo, pageSize, totalCount);
 43         this.list = list;
 44     }
 45 
 46     /**
 47      * 第一条数据位置
 48      * 
 49      * @return
 50      */
 51     public int getFirstResult() {
 52         return (pageNo - 1) * pageSize;
 53     }
 54 
 55     /**
 56      * 当前页的数据
 57      */
 58     private List<?> list;
 59     
 60     /**
 61      * 当前页的分页样式
 62      */
 63     private List<String> pageView;
 64 
 65     /**
 66      * 获得分页内容
 67      * 
 68      * @return
 69      */
 70     public List<?> getList() {
 71         return list;
 72     }
 73 
 74     /**
 75      * 设置分页内容
 76      * 
 77      * @param list
 78      */
 79     @SuppressWarnings("unchecked")
 80     public void setList(List list) {
 81         this.list = list;
 82     }
 83     /**
 84      * 获得分页样式
 85      * 
 86      * @return
 87      */
 88     public List<String> getPageView() {
 89         return pageView;
 90     }
 91     /**
 92      * 设置分页样式
 93      * 
 94      * @param list
 95      */
 96     public void setPageView(List<String> pageView) {
 97         this.pageView = pageView;
 98     }
 99 
100 
101     /**
102      * 分页显示样示部分
103      */
104     public void pageView(String url,String params){
105         
106          pageView = new ArrayList<String>();
107              
108         if(this.pageNo != 1){
109             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=1'\"><font size=2>首页</font></a>");
110             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"'\"><font size=2>上一页</font></a>");
111         }else{
112             pageView.add("<font size=2>首页</font>");
113             pageView.add("<font size=2>上一页</font>");
114         }
115     
116         if(this.getTotalPage() <= 10){
117             for (int i = 0; i < this.getTotalPage(); i++) {
118                 if((i+1)==this.pageNo){
119                     pageView.add("<strong>"+this.pageNo+"</strong>");
120                     i = i+1;
121                     if(this.pageNo==this.getTotalPage())break;
122                 }
123                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
124             }
125         }else if(this.getTotalPage() <= 20){
126             //没有把...加上
127             int l = 0;
128             int r = 0;
129             if(this.pageNo<5){
130                 l=this.pageNo-1;
131                 r=10-l-1;
132             }else if(this.getTotalPage()-this.pageNo<5){
133                 r=this.getTotalPage()-this.pageNo;
134                 l=10-1-r;
135             }else{
136                 l=4;
137                 r=5;
138             }
139             int tmp =  this.pageNo-l;
140             for (int i = tmp; i < tmp+10; i++) {
141                 if(i==this.pageNo){
142                     pageView.add("<strong>"+this.pageNo+"</strong>");
143                     i = i+1;
144                     if(this.pageNo==this.getTotalPage()) break;
145                 }
146                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i)+"'\">"+(i)+"</a>");
147             }
148                 
149         }else if(this.pageNo<7){
150             for (int i = 0; i < 8; i++) {
151                 if(i+1==this.pageNo){
152                     pageView.add("<strong>"+this.pageNo+"</strong>");
153                     i = i+1;
154                 }
155                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
156             }
157             pageView.add("...");
158             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"'\">"+(this.getTotalPage()-1)+"</a>");
159             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"'\">"+(this.getTotalPage())+"</a>");
160         }else if(this.pageNo>this.getTotalPage()-6){
161             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(1)+"'\">"+(1)+"</a>");
162             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(2)+"'\">"+(2)+"</a>");
163             pageView.add("...");
164             for (int i = this.getTotalPage()-8; i <this.getTotalPage() ; i++) {
165                 if(i+1==this.pageNo){
166                     pageView.add("<strong>"+this.pageNo+"</strong>");
167                     i = i+1;
168                     if(this.pageNo==this.getTotalPage()) break;
169                 }
170                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
171             }
172         }else{
173             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(1)+"'\">"+(1)+"</a>");
174             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(2)+"'\">"+(2)+"</a>");
175             pageView.add("...");
176             
177             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-2)+"'\">"+(this.pageNo-2)+"</a>");
178             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"'\">"+(this.pageNo-1)+"</a>");
179             pageView.add("<strong>"+this.pageNo+"</strong>");
180             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"'\">"+(this.pageNo+1)+"</a>");
181             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+2)+"'\">"+(this.pageNo+2)+"</a>");
182             
183             pageView.add("...");
184             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"'\">"+(this.getTotalPage()-1)+"</a>");
185             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"'\">"+(this.getTotalPage())+"</a>");
186         }    
187         if(this.pageNo != this.getTotalPage()){
188             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"'\"><font size=2>下一页</font></a>");
189             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+this.getTotalPage()+"'\"><font size=2>尾页</font></a>");
190         } else{
191             pageView.add("<font size=2>下一页</font>");
192             pageView.add("<font size=2>尾页</font>");
193         }
194         pageView.add("共<var>" + getTotalPage() + "</var>页 到第<input type='text' id='PAGENO'  size='3' />页 <input type='button' id='skip' class='hand btn60x20' value='确定' onclick=\"javascript:window.location.href = '" + url + "?" + params + "&pageNo=' + $('#PAGENO').val() \"/>");
195     }
196 }
View Code



2, 下面源码就是需要自己在项目中构建
下面开始从Controller(babasport-console)-->ServiceImpl(babasport-product)-->DaoMapper(babasport-dao)-->jsp展示

Controller层:
BrandController.java:

 1 package cn.itcast.core.controller;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import cn.itcast.common.page.Pagination;
11 import cn.itcast.core.bean.product.Brand;
12 import cn.itcast.core.service.product.BrandService;
13 
14 /*
15  * 品牌
16  */
17 @Controller
18 @RequestMapping(value="/brand")
19 public class BrandController {
20 
21     @Autowired
22     private BrandService brandService;
23     
24     @RequestMapping(value="/list.do")
25     public String list(Integer pageNo, String name, Integer isDisplay, Model model){
26         //List<Brand> brands = brandService.selectBrandListByQuery(name, isDisplay);
27         Pagination pagination = brandService.selectPaginationByQuery(pageNo, name, isDisplay);
28         model.addAttribute("pagination", pagination);
29         //回显查询条件
30         model.addAttribute("name", name);
31         if(isDisplay != null){
32             model.addAttribute("isDisplay", isDisplay);
33         }else{
34             model.addAttribute("isDisplay", 1);
35         }
36         return "brand/list";
37     }
38     
39     @RequestMapping(value="/toEdit.do")
40     public String selectBrandById(Long id, Model model){
41         
42         Brand brand = brandService.selectBrandById(id);
43         model.addAttribute("brand", brand);
44         
45         return "brand/edit";
46     }
47 }

解析: 这个controller 包括两个方法, 一个是查询分页数据, 另一个是根据ID 回显数据.
两个方法都很简单, 第一个就是根据页面传递过来的当前页数和查询条件通过service层去做进一步处理后再通过Dao层去查询结果, 将查询的结果返回后再封装到Model里面, 然后转发到视图层.
第二个方法更简单了, 通过Id查询到相应的商品然后回显数据即可.

Service层:
BrandServiceImpl.java:

 1 package cn.itcast.core.service.product;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 import org.springframework.transaction.annotation.Transactional;
 8 
 9 import cn.itcast.common.page.Pagination;
10 import cn.itcast.core.bean.product.Brand;
11 import cn.itcast.core.bean.product.BrandQuery;
12 import cn.itcast.core.dao.product.BrandDao;
13 @Service("brandService")
14 @Transactional
15 public class BrandServiceImpl implements BrandService {
16 
17     @Autowired
18     private BrandDao brandDao;
19     
20     @Override
21     public List<Brand> selectBrandListByQuery(String name, Integer isDisplay) {
22         BrandQuery brandQuery = new BrandQuery();
23         if (name != null) {
24             brandQuery.setName(name);
25         }
26         if (isDisplay != null) {
27             brandQuery.setIsDisplay(isDisplay);
28         }
29         else {
30             //是: 1, 否:0
31             brandQuery.setIsDisplay(1);
32         }
33         
34         return brandDao.selectBrandListByQuery(brandQuery);
35     }
36     
37     //分页
38     public Pagination selectPaginationByQuery(Integer pageNo, String name, Integer isDisplay) {
39         BrandQuery brandQuery = new BrandQuery();
40         //设置当前页  cpn方法: 如果pageNumber 是null或者 小于1, 那么就将pageNumber设置为1, 如果不是则使用传递进来的pageNumber
41         brandQuery.setPageNo(Pagination.cpn(pageNo));
42         //设置每页数
43         brandQuery.setPageSize(3);
44         
45         //拼接条件
46         StringBuilder sb = new StringBuilder();
47         
48         if (name != null) {
49             brandQuery.setName(name);
50             sb.append("name=").append(name);
51         }
52         
53         if (isDisplay != null) {
54             brandQuery.setIsDisplay(isDisplay);
55             sb.append("&isDisplay=").append(isDisplay);
56         }
57         else {
58             //是: 1, 否:0
59             brandQuery.setIsDisplay(1);
60             sb.append("&isDisplay=").append(1);
61         }
62         //构建分页对象
63         //三个参数: 当前页,每页显示行数,总记录数
64         Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(), brandDao.selectCount(brandQuery));
65         //查询结果集
66         //使用查询语句: select * from bbs_brand where ... limit (pageNumber - 1) * 3, 3
67         pagination.setList(brandDao.selectBrandListByQuery(brandQuery));
68         
69         //分页在页面显示 /brand/list.do?name=aaa&&isDisplay=0
70         String url = "/brand/list.do";
71         pagination.pageView(url, sb.toString());
72         
73         return pagination;
74     }
75     
76     //通过ID查询一个品牌
77     public Brand selectBrandById(Long id){
78          return brandDao.selectBrandById(id);
79     }
80 
81 }

解析: Service层中主要来说第二个分页的方法.
 1 brandQuery.setPageNo(Pagination.cpn(pageNo)); 
这个cpn方法是Pagination中封装好的静态方法, 我们来看下源码:

1 /**
2      * 检查页码 checkPageNo
3      * 
4      * @param pageNo
5      * @return if pageNo==null or pageNo<1 then return 1 else return pageNo
6      */
7     public static int cpn(Integer pageNo) {
8         return (pageNo == null || pageNo < 1) ? 1 : pageNo;
9     }

使用StringBuilder 封装查询条件, 因为当我们根据查询条件查询到的数据也有分页效果时, 这时候我们点击页码的按钮时跳转到相应的页数后, 查询条件也应该回显.


 1 //构建分页对象
 2 //三个参数: 当前页,每页显示行数,总记录数
 3 Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(), brandDao.selectCount(brandQuery));
 4 //查询结果集
 5 //使用查询语句: select * from bbs_brand where ... limit (pageNumber - 1) * 3, 3
 6 pagination.setList(brandDao.selectBrandListByQuery(brandQuery));
 7 
 8 //分页在页面显示 /brand/list.do?name=aaa&&isDisplay=0
 9 String url = "/brand/list.do";
10 pagination.pageView(url, sb.toString());

构建分页对象, 将查询的结果封装到pagination中, 且 将url和封装的条件封装到pageView中. 这里因为页码按钮的样式是固定的, 不固定的只是我们点击 每一个按钮跳转的url和查询的条件不同, 所以这里使用pageView属性来封装url和查询条件.

Dao层:
BrandDao.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>  
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="cn.itcast.core.dao.product.BrandDao">
 5 
 6     <resultMap type="Brand" id="brand">
 7          <result column="img_url" property="imgUrl"/>
 8          <result column="is_display" property="isDisplay"/>
 9     </resultMap>
10     <!-- 查询品牌结果集  List<Brand> -->
11     <select id="selectBrandListByQuery" parameterType="BrandQuery" resultMap="brand">
12         select id ,name,description,img_url,is_display,sort
13         from bbs_brand
14         <where>
15             <if test="name != null">
16                 name like "%"#{name}"%"
17             </if>
18             <if test="isDisplay != null">
19                 and is_display = #{isDisplay}
20             </if>
21         </where>
22         <if test="startRow != null">
23             limit #{startRow}, #{pageSize}
24         </if>
25     </select>
26     
27     <!-- 查询品牌结果集  List<Brand> -->
28     <select id="selectCount" parameterType="BrandQuery" resultType="Integer">
29         select count(1)
30         from bbs_brand
31         <where>
32             <if test="name != null">
33                 name like "%"#{name}"%"
34             </if>
35             <if test="isDisplay != null">
36                 and is_display = #{isDisplay}
37             </if>
38         </where>
39     </select>
40     
41     <!-- 根据ID查询 -->
42     <select id="selectBrandById" parameterType="Long" resultMap="brand">
43         select id ,name,description,img_url,is_display,sort
44         from bbs_brand
45         <where>
46             id = #{id}
47         </where>
48     </select>
49 </mapper>

Model
BrandQuery.java:

  1 package cn.itcast.core.bean.product;
  2 
  3 import java.io.Serializable;
  4 
  5 public class BrandQuery implements Serializable{
  6     /**
  7      * 默认的ID
  8      */
  9     private static final long serialVersionUID = 1L;
 10     
 11     //品牌ID  bigint
 12     private Long id;
 13     //品牌名称
 14     private String name;
 15     //描述
 16     private String description;
 17     //图片URL
 18     private String imgUrl;
 19     //排序  越大越靠前   
 20     private Integer sort;
 21     //是否可用   0 不可用 1 可用
 22     private Integer isDisplay;//is_display
 23     public Long getId() {
 24         return id;
 25     }
 26     public void setId(Long id) {
 27         this.id = id;
 28     }
 29     public String getName() {
 30         return name;
 31     }
 32     public void setName(String name) {
 33         this.name = name;
 34     }
 35     public String getDescription() {
 36         return description;
 37     }
 38     public void setDescription(String description) {
 39         this.description = description;
 40     }
 41     public String getImgUrl() {
 42         return imgUrl;
 43     }
 44     public void setImgUrl(String imgUrl) {
 45         this.imgUrl = imgUrl;
 46     }
 47     public Integer getSort() {
 48         return sort;
 49     }
 50     public void setSort(Integer sort) {
 51         this.sort = sort;
 52     }
 53     public Integer getIsDisplay() {
 54         return isDisplay;
 55     }
 56     public void setIsDisplay(Integer isDisplay) {
 57         this.isDisplay = isDisplay;
 58     }
 59     public static long getSerialversionuid() {
 60         return serialVersionUID;
 61     }
 62     @Override
 63     public String toString() {
 64         return "Brand [id=" + id + ", name=" + name + ", description=" + description + ", imgUrl=" + imgUrl + ", sort="
 65                 + sort + ", isDisplay=" + isDisplay + "]";
 66     }
 67     
 68     //附加字段
 69     //当前页
 70     private Integer pageNo = 1;
 71     
 72     //每页数
 73     private Integer pageSize = 10;
 74     
 75     //开始行
 76     private Integer startRow;
 77     public Integer getPageNo() {
 78         return pageNo;
 79     }
 80     public void setPageNo(Integer pageNo) {
 81         //计算开始行
 82         this.startRow = (pageNo - 1)*pageSize;
 83         this.pageNo = pageNo;
 84     }
 85     public Integer getPageSize() {
 86         return pageSize;
 87     }
 88     public void setPageSize(Integer pageSize) {
 89         //计算开始行
 90         this.startRow = (pageNo - 1)*pageSize;
 91         this.pageSize = pageSize;
 92     }
 93     public Integer getStartRow() {
 94         return startRow;
 95     }
 96     public void setStartRow(Integer startRow) {
 97         this.startRow = startRow;
 98     }
 99     
100     
101 }

解析: mapper和model
这里model这接计算好了startRow, 所以在mapper就可以直接查询了.

 1 //附加字段
 2 //当前页
 3 private Integer pageNo = 1;
 4 
 5 //每页数
 6 private Integer pageSize = 10;
 7 
 8 //开始行
 9 private Integer startRow;
10 public Integer getPageNo() {
11     return pageNo;
12 }
13 public void setPageNo(Integer pageNo) {
14     //计算开始行
15     this.startRow = (pageNo - 1)*pageSize;
16     this.pageNo = pageNo;
17 }
18 public Integer getPageSize() {
19     return pageSize;
20 }
21 public void setPageSize(Integer pageSize) {
22     //计算开始行
23     this.startRow = (pageNo - 1)*pageSize;
24     this.pageSize = pageSize;
25 }
26 public Integer getStartRow() {
27     return startRow;
28 }
29 public void setStartRow(Integer startRow) {
30     this.startRow = startRow;
31 }


View层:
list.jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ include file="../head.jsp" %>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 <html xmlns="http://www.w3.org/1999/xhtml">
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 7 <title>babasport-list</title>
 8 </head>
 9 <body>
10 <div class="box-positon">
11     <div class="rpos">当前位置: 品牌管理 - 列表</div>
12     <form class="ropt">
13         <input class="add" type="button" value="添加" onclick="javascript:window.location.href='add.jsp'"/>
14     </form>
15     <div class="clear"></div>
16 </div>
17 <div class="body-box">
18 <form action="/brand/list.do" method="post" style="padding-top:5px;">
19 品牌名称: <input type="text" name="name" value="${name}"/>
20     <select name="isDisplay"> 
21         <option value="1" <c:if test="${isDisplay==1 }">selected="selected"</c:if>>是</option>
22         <option value="0" <c:if test="${isDisplay==0 }">selected="selected"</c:if>>否</option>
23     </select>
24     <input type="submit" class="query" value="查询"/>
25 </form>
26 <table cellspacing="1" cellpadding="0" border="0" width="100%" class="pn-ltable">
27     <thead class="pn-lthead">
28         <tr>
29             <th width="20"><input type="checkbox" onclick="checkBox('ids',this.checked)"/></th>
30             <th>品牌ID</th>
31             <th>品牌名称</th>
32             <th>品牌图片</th>
33             <th>品牌描述</th>
34             <th>排序</th>
35             <th>是否可用</th>
36             <th>操作选项</th>
37         </tr>
38     </thead>
39     <tbody class="pn-ltbody">
40         <c:forEach items="${pagination.list }" var="brand">
41         <tr bgcolor="#ffffff" onmouseout="this.bgColor='#ffffff'" onmouseover="this.bgColor='#eeeeee'">
42             <td><input type="checkbox" value="${brand.id }" name="ids"/></td>
43             <td align="center">${brand.id }</td>
44             <td align="center">${brand.name }</td>
45             <td align="center"><img width="40" height="40" src="/images/pic/ppp.jpg"/></td>
46             <td align="center"></td>
47             <td align="center">${brand.sort}</td>
48             <td align="center">
49                 <c:if test="${brand.isDisplay == 1 }"></c:if>
50                 <c:if test="${brand.isDisplay == 0 }"></c:if>
51             </td>
52             <td align="center">
53             <a class="pn-opt" href="/brand/toEdit.do?id=${brand.id}">修改</a> | <a class="pn-opt" onclick="if(!confirm('您确定删除吗?')) {return false;}" href="#">删除</a>
54             </td>
55         </tr>
56     
57     </c:forEach>
58     
59     </tbody>
60 </table>
61 <div class="page pb15">
62     <span class="r inb_a page_b">
63         <c:forEach items="${pagination.pageView }" var="page">
64             ${page }
65         </c:forEach>
66     </span>
67 </div>
68 <div style="margin-top:15px;"><input class="del-button" type="button" value="删除" onclick="optDelete();"/></div>
69 </div>
70 </body>
71 </html>

解析: 这里需要添加一些必要的说明: 
这里 在显示分页页码的时候直接使用了 ${page}, 到底这个是怎么实现的呢? 下面来看下源码中的pageView.

 1 /**
 2      * 分页显示样示部分
 3      */
 4     public void pageView(String url,String params){
 5         
 6          pageView = new ArrayList<String>();
 7              
 8         if(this.pageNo != 1){
 9             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=1'\"><font size=2>首页</font></a>");
10             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"'\"><font size=2>上一页</font></a>");
11         }else{
12             pageView.add("<font size=2>首页</font>");
13             pageView.add("<font size=2>上一页</font>");
14         }
15     
16         if(this.getTotalPage() <= 10){
17             for (int i = 0; i < this.getTotalPage(); i++) {
18                 if((i+1)==this.pageNo){
19                     pageView.add("<strong>"+this.pageNo+"</strong>");
20                     i = i+1;
21                     if(this.pageNo==this.getTotalPage())break;
22                 }
23                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
24             }
25         }else if(this.getTotalPage() <= 20){
26             //没有把...加上
27             int l = 0;
28             int r = 0;
29             if(this.pageNo<5){
30                 l=this.pageNo-1;
31                 r=10-l-1;
32             }else if(this.getTotalPage()-this.pageNo<5){
33                 r=this.getTotalPage()-this.pageNo;
34                 l=10-1-r;
35             }else{
36                 l=4;
37                 r=5;
38             }
39             int tmp =  this.pageNo-l;
40             for (int i = tmp; i < tmp+10; i++) {
41                 if(i==this.pageNo){
42                     pageView.add("<strong>"+this.pageNo+"</strong>");
43                     i = i+1;
44                     if(this.pageNo==this.getTotalPage()) break;
45                 }
46                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i)+"'\">"+(i)+"</a>");
47             }
48                 
49         }else if(this.pageNo<7){
50             for (int i = 0; i < 8; i++) {
51                 if(i+1==this.pageNo){
52                     pageView.add("<strong>"+this.pageNo+"</strong>");
53                     i = i+1;
54                 }
55                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
56             }
57             pageView.add("...");
58             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"'\">"+(this.getTotalPage()-1)+"</a>");
59             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"'\">"+(this.getTotalPage())+"</a>");
60         }else if(this.pageNo>this.getTotalPage()-6){
61             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(1)+"'\">"+(1)+"</a>");
62             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(2)+"'\">"+(2)+"</a>");
63             pageView.add("...");
64             for (int i = this.getTotalPage()-8; i <this.getTotalPage() ; i++) {
65                 if(i+1==this.pageNo){
66                     pageView.add("<strong>"+this.pageNo+"</strong>");
67                     i = i+1;
68                     if(this.pageNo==this.getTotalPage()) break;
69                 }
70                 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(i+1)+"'\">"+(i+1)+"</a>");
71             }
72         }else{
73             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(1)+"'\">"+(1)+"</a>");
74             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(2)+"'\">"+(2)+"</a>");
75             pageView.add("...");
76             
77             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-2)+"'\">"+(this.pageNo-2)+"</a>");
78             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"'\">"+(this.pageNo-1)+"</a>");
79             pageView.add("<strong>"+this.pageNo+"</strong>");
80             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"'\">"+(this.pageNo+1)+"</a>");
81             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+2)+"'\">"+(this.pageNo+2)+"</a>");
82             
83             pageView.add("...");
84             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"'\">"+(this.getTotalPage()-1)+"</a>");
85             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"'\">"+(this.getTotalPage())+"</a>");
86         }    
87         if(this.pageNo != this.getTotalPage()){
88             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"'\"><font size=2>下一页</font></a>");
89             pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo="+this.getTotalPage()+"'\"><font size=2>尾页</font></a>");
90         } else{
91             pageView.add("<font size=2>下一页</font>");
92             pageView.add("<font size=2>尾页</font>");
93         }
94         pageView.add("共<var>" + getTotalPage() + "</var>页 到第<input type='text' id='PAGENO'  size='3' />页 <input type='button' id='skip' class='hand btn60x20' value='确定' onclick=\"javascript:window.location.href = '" + url + "?" + params + "&pageNo=' + $('#PAGENO').val() \"/>");
95     }

这里是直接将展示页拼接了出来, 而且加了url和查询参数.


到了这里整个分页的流程就搞完了, 下面来看下整体效果: 

posted @ 2016-08-21 16:12  一枝花算不算浪漫  阅读(6343)  评论(3编辑  收藏  举报