mybatis結合分頁的使用及解析.

首先說明: 這裏分頁是使用了SSM框架+ jsp 來做的, 當然分頁還有其他的很多做法, 比如easyUI自帶的分頁效果. 但是這些原理都是很相似的, 再次只做爲學習總結之用.

一, 效果圖
mybatis結合分頁的使用及解析.
這裏的截圖是來自百度, 當然我們的項目也是做成這種效果, 當點擊第10頁時, 相應頁碼數也要發生相應變化.

二, 代碼實例
1, 首先我們在項目中加入了一個page 的jar包, 這個jar包有3個java文件組成, 這個jar包是事先寫好的通用分頁插件.
mybatis結合分頁的使用及解析.

另外這裏將源碼也寫上來:
Paginable.java:

View Code
SimplePage.java:

View Code

Pagination.java:

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 封裝查詢條件, 因爲當我們根據查詢條件查詢到的數據也有分頁效果時, 這時候我們點擊頁碼的按鈕時跳轉到相應的頁數後, 查詢條件也應該回顯.
mybatis結合分頁的使用及解析.
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"&gt;
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"&gt;
4 <html xmlns="http://www.w3.org/1999/xhtml"&gt;
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和查詢參數.

到了這裏整個分頁的流程就搞完了, 下面來看下整體效果:
mybatis結合分頁的使用及解析.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章