MyBatis_02_Page&PageInfo

转载自:
附加:PageHelper分页插件的:Page和PageInfo的区别

说明:

(1)为什么写本篇博客?:

          ● 在【Spring Boot电商项目29:商品分类模块八:后台的【分类列表(平铺)】接口;】中,实现分页功能时,使用到了PageHelper分页插件;

          ● 但是,在【Spring Boot电商项目29:商品分类模块八:后台的【分类列表(平铺)】接口;】中,我们使用的是PageHelper中的PageInfo对象;

          ● 而,在【MyBatis进阶五:PageHelper分页插件;】中,我们第一次遇到PageHelper时候,使用的是PageHelper中的Page对象;

          ● 所以,本篇博客,就分析一下:PageHelper分页插件中的:Page和PageInfo的区别;

(2)本篇博客参考的博客有:

          ● 【Page和PageInfo】,该文的作者是【程序员^晓洋】;

目录

一:Page和PageInfo;(这儿,归纳了两个对象的属性,总结的不错;)

1. Page和PageInfo简介;

2.Page和PageInfo,具体属性分析;

二:Page和PageInfo:源码;(这部分,仅供参考,爱看不看)

1.Page源码;

2.PageInfo源码;

三:Page和PageInfo:使用;(重要!)

(1)首先,就是开启分页,设置【pageNum:当前页】和【pageSize:每页记录数】;

 (2)然后,我们会利用Mybatis,根据上面设置的分页条件,去查询数据库,得到查询结果;

(3)然后,可以根据前端对分页数据格式的要求,可以把(3)中查到的分页数据,包装成Page对象;

(4)或者,包装成PageInfo对象;

(5)使用Page对象还是使用PageInfo对象?


一:Page和PageInfo;(这儿,归纳了两个对象的属性,总结的不错;)

这儿的内容,完全照抄自【Page和PageInfo】,该文的作者是【程序员^晓洋】;

1. Page和PageInfo简介;

两者都能获取到数据,

Page是一个ArrayListList。

MyBatis_02_Page&PageInfo_第1张图片

PageInfo是一个对象,能获取到的数据比Page多;

MyBatis_02_Page&PageInfo_第2张图片

2.Page和PageInfo,具体属性分析;

Page对象:


     
     
       
       
       
       
  1. private int pageNum; //当前页码
  2. private int pageSize; //每页数据的数量
  3. private int startRow; //始页首行行号
  4. private int endRow; //尾页尾行行号
  5. private long total; //总记录数
  6. private int pages; //总页数
  7. private Boolean reasonable; //分页合理化
  8. private Boolean pageSizeZero; //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果

PageInfo对象:


     
     
       
       
       
       
  1. private int pageNum; //当前页
  2. private int pageSize; //每页显示数据条数
  3. private int size; //当前页的数量
  4. private int startRow; //始页首行行号
  5. private int endRow; //尾页尾行行号
  6. private long total; //总记录数
  7. private int pages; //总页数
  8. private List list; //查询结果的数据
  9. private int firstPage; //首页
  10. private int prePage; //上一页
  11. private int nextPage; // 下一页
  12. private int lastPage; //最后一页
  13. private boolean isFirstPage; //是不是第一页
  14. private boolean isLastPage; //是不是最后一页
  15. private boolean hasPreviousPage; //有没有上一页
  16. private boolean hasNextPage; //有没有下一页
  17. private int navigatePages; //所有导航页号
  18. private int[] navigatepageNums; //导航页码数

二:Page和PageInfo:源码;(这部分,仅供参考,爱看不看)

1.Page源码;


     
     
       
       
       
       
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014-2017 [email protected]
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package com.github.pagehelper;
  25. import java.io.Closeable;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * Mybatis - 分页对象
  30. *
  31. * @author liuzh/abel533/isea533
  32. * @version 3.6.0
  33. * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
  34. */
  35. public class Page extends ArrayList implements Closeable {
  36. private static final long serialVersionUID = 1L;
  37. /**
  38. * 页码,从1开始
  39. */
  40. private int pageNum;
  41. /**
  42. * 页面大小
  43. */
  44. private int pageSize;
  45. /**
  46. * 起始行
  47. */
  48. private int startRow;
  49. /**
  50. * 末行
  51. */
  52. private int endRow;
  53. /**
  54. * 总数
  55. */
  56. private long total;
  57. /**
  58. * 总页数
  59. */
  60. private int pages;
  61. /**
  62. * 包含count查询
  63. */
  64. private boolean count = true;
  65. /**
  66. * 分页合理化
  67. */
  68. private Boolean reasonable;
  69. /**
  70. * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
  71. */
  72. private Boolean pageSizeZero;
  73. /**
  74. * 进行count查询的列名
  75. */
  76. private String countColumn;
  77. /**
  78. * 排序
  79. */
  80. private String orderBy;
  81. /**
  82. * 只增加排序
  83. */
  84. private boolean orderByOnly;
  85. public Page () {
  86. super();
  87. }
  88. public Page (int pageNum, int pageSize) {
  89. this(pageNum, pageSize, true, null);
  90. }
  91. public Page (int pageNum, int pageSize, boolean count) {
  92. this(pageNum, pageSize, count, null);
  93. }
  94. private Page (int pageNum, int pageSize, boolean count, Boolean reasonable) {
  95. super( 0);
  96. if (pageNum == 1 && pageSize == Integer.MAX_VALUE) {
  97. pageSizeZero = true;
  98. pageSize = 0;
  99. }
  100. this.pageNum = pageNum;
  101. this.pageSize = pageSize;
  102. this.count = count;
  103. calculateStartAndEndRow();
  104. setReasonable(reasonable);
  105. }
  106. /**
  107. * int[] rowBounds
  108. * 0 : offset
  109. * 1 : limit
  110. */
  111. public Page (int[] rowBounds, boolean count) {
  112. super( 0);
  113. if (rowBounds[ 0] == 0 && rowBounds[ 1] == Integer.MAX_VALUE) {
  114. pageSizeZero = true;
  115. this.pageSize = 0;
  116. } else {
  117. this.pageSize = rowBounds[ 1];
  118. this.pageNum = rowBounds[ 1] != 0 ? ( int) (Math.ceil((( double) rowBounds[ 0] + rowBounds[ 1]) / rowBounds[ 1])) : 0;
  119. }
  120. this.startRow = rowBounds[ 0];
  121. this.count = count;
  122. this.endRow = this.startRow + rowBounds[ 1];
  123. }
  124. public List getResult () {
  125. return this;
  126. }
  127. public int getPages () {
  128. return pages;
  129. }
  130. public Page setPages (int pages) {
  131. this.pages = pages;
  132. return this;
  133. }
  134. public int getEndRow () {
  135. return endRow;
  136. }
  137. public Page setEndRow (int endRow) {
  138. this.endRow = endRow;
  139. return this;
  140. }
  141. public int getPageNum () {
  142. return pageNum;
  143. }
  144. public Page setPageNum (int pageNum) {
  145. //分页合理化,针对不合理的页码自动处理
  146. this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
  147. return this;
  148. }
  149. public int getPageSize () {
  150. return pageSize;
  151. }
  152. public Page setPageSize (int pageSize) {
  153. this.pageSize = pageSize;
  154. return this;
  155. }
  156. public int getStartRow () {
  157. return startRow;
  158. }
  159. public Page setStartRow (int startRow) {
  160. this.startRow = startRow;
  161. return this;
  162. }
  163. public long getTotal () {
  164. return total;
  165. }
  166. public void setTotal (long total) {
  167. this.total = total;
  168. if (total == - 1) {
  169. pages = 1;
  170. return;
  171. }
  172. if (pageSize > 0) {
  173. pages = ( int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
  174. } else {
  175. pages = 0;
  176. }
  177. //分页合理化,针对不合理的页码自动处理
  178. if ((reasonable != null && reasonable) && pageNum > pages) {
  179. if(pages!= 0){
  180. pageNum = pages;
  181. }
  182. calculateStartAndEndRow();
  183. }
  184. }
  185. public Boolean getReasonable () {
  186. return reasonable;
  187. }
  188. public Page setReasonable (Boolean reasonable) {
  189. if (reasonable == null) {
  190. return this;
  191. }
  192. this.reasonable = reasonable;
  193. //分页合理化,针对不合理的页码自动处理
  194. if ( this.reasonable && this.pageNum <= 0) {
  195. this.pageNum = 1;
  196. calculateStartAndEndRow();
  197. }
  198. return this;
  199. }
  200. public Boolean getPageSizeZero () {
  201. return pageSizeZero;
  202. }
  203. public Page setPageSizeZero (Boolean pageSizeZero) {
  204. if (pageSizeZero != null) {
  205. this.pageSizeZero = pageSizeZero;
  206. }
  207. return this;
  208. }
  209. public String getOrderBy () {
  210. return orderBy;
  211. }
  212. public Page setOrderBy (String orderBy) {
  213. this.orderBy = orderBy;
  214. return (Page) this;
  215. }
  216. public boolean isOrderByOnly () {
  217. return orderByOnly;
  218. }
  219. public void setOrderByOnly (boolean orderByOnly) {
  220. this.orderByOnly = orderByOnly;
  221. }
  222. /**
  223. * 计算起止行号
  224. */
  225. private void calculateStartAndEndRow () {
  226. this.startRow = this.pageNum > 0 ? ( this.pageNum - 1) * this.pageSize : 0;
  227. this.endRow = this.startRow + this.pageSize * ( this.pageNum > 0 ? 1 : 0);
  228. }
  229. public boolean isCount () {
  230. return this.count;
  231. }
  232. public Page setCount (boolean count) {
  233. this.count = count;
  234. return this;
  235. }
  236. /**
  237. * 设置页码
  238. *
  239. * @param pageNum
  240. * @return
  241. */
  242. public Page pageNum (int pageNum) {
  243. //分页合理化,针对不合理的页码自动处理
  244. this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
  245. return this;
  246. }
  247. /**
  248. * 设置页面大小
  249. *
  250. * @param pageSize
  251. * @return
  252. */
  253. public Page pageSize (int pageSize) {
  254. this.pageSize = pageSize;
  255. calculateStartAndEndRow();
  256. return this;
  257. }
  258. /**
  259. * 是否执行count查询
  260. *
  261. * @param count
  262. * @return
  263. */
  264. public Page count (Boolean count) {
  265. this.count = count;
  266. return this;
  267. }
  268. /**
  269. * 设置合理化
  270. *
  271. * @param reasonable
  272. * @return
  273. */
  274. public Page reasonable (Boolean reasonable) {
  275. setReasonable(reasonable);
  276. return this;
  277. }
  278. /**
  279. * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
  280. *
  281. * @param pageSizeZero
  282. * @return
  283. */
  284. public Page pageSizeZero (Boolean pageSizeZero) {
  285. setPageSizeZero(pageSizeZero);
  286. return this;
  287. }
  288. /**
  289. * 指定 count 查询列
  290. *
  291. * @param columnName
  292. * @return
  293. */
  294. public Page countColumn (String columnName) {
  295. this.countColumn = columnName;
  296. return this;
  297. }
  298. public PageInfo toPageInfo () {
  299. PageInfo pageInfo = new PageInfo( this);
  300. return pageInfo;
  301. }
  302. public PageSerializable toPageSerializable () {
  303. PageSerializable serializable = new PageSerializable( this);
  304. return serializable;
  305. }
  306. public Page doSelectPage (ISelect select) {
  307. select.doSelect();
  308. return (Page) this;
  309. }
  310. public PageInfo doSelectPageInfo (ISelect select) {
  311. select.doSelect();
  312. return (PageInfo) this.toPageInfo();
  313. }
  314. public PageSerializable doSelectPageSerializable (ISelect select) {
  315. select.doSelect();
  316. return (PageSerializable) this.toPageSerializable();
  317. }
  318. public long doCount (ISelect select) {
  319. this.pageSizeZero = true;
  320. this.pageSize = 0;
  321. select.doSelect();
  322. return this.total;
  323. }
  324. public String getCountColumn () {
  325. return countColumn;
  326. }
  327. public void setCountColumn (String countColumn) {
  328. this.countColumn = countColumn;
  329. }
  330. @Override
  331. public String toString () {
  332. return "Page{" +
  333. "count=" + count +
  334. ", pageNum=" + pageNum +
  335. ", pageSize=" + pageSize +
  336. ", startRow=" + startRow +
  337. ", endRow=" + endRow +
  338. ", total=" + total +
  339. ", pages=" + pages +
  340. ", reasonable=" + reasonable +
  341. ", pageSizeZero=" + pageSizeZero +
  342. '}' + super.toString();
  343. }
  344. @Override
  345. public void close () {
  346. PageHelper.clearPage();
  347. }
  348. }

2.PageInfo源码;


     
     
       
       
       
       
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014-2017 [email protected]
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package com.github.pagehelper;
  25. import java.util.Collection;
  26. import java.util.List;
  27. /**
  28. * 对Page结果进行包装
  29. *

  30. * 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907
  31. *
  32. * @author liuzh/abel533/isea533
  33. * @version 3.3.0
  34. * @since 3.2.2
  35. * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
  36. */
  37. @SuppressWarnings({"rawtypes", "unchecked"})
  38. public class PageInfo extends PageSerializable {
  39. //当前页
  40. private int pageNum;
  41. //每页的数量
  42. private int pageSize;
  43. //当前页的数量
  44. private int size;
  45. //由于startRow和endRow不常用,这里说个具体的用法
  46. //可以在页面中"显示startRow到endRow 共size条数据"
  47. //当前页面第一个元素在数据库中的行号
  48. private int startRow;
  49. //当前页面最后一个元素在数据库中的行号
  50. private int endRow;
  51. //总页数
  52. private int pages;
  53. //前一页
  54. private int prePage;
  55. //下一页
  56. private int nextPage;
  57. //是否为第一页
  58. private boolean isFirstPage = false;
  59. //是否为最后一页
  60. private boolean isLastPage = false;
  61. //是否有前一页
  62. private boolean hasPreviousPage = false;
  63. //是否有下一页
  64. private boolean hasNextPage = false;
  65. //导航页码数
  66. private int navigatePages;
  67. //所有导航页号
  68. private int[] navigatepageNums;
  69. //导航条上的第一页
  70. private int navigateFirstPage;
  71. //导航条上的最后一页
  72. private int navigateLastPage;
  73. public PageInfo () {
  74. }
  75. /**
  76. * 包装Page对象
  77. *
  78. * @param list
  79. */
  80. public PageInfo (List list) {
  81. this(list, 8);
  82. }
  83. /**
  84. * 包装Page对象
  85. *
  86. * @param list page结果
  87. * @param navigatePages 页码数量
  88. */
  89. public PageInfo (List list, int navigatePages) {
  90. super(list);
  91. if (list instanceof Page) {
  92. Page page = (Page) list;
  93. this.pageNum = page.getPageNum();
  94. this.pageSize = page.getPageSize();
  95. this.pages = page.getPages();
  96. this.size = page.size();
  97. //由于结果是>startRow的,所以实际的需要+1
  98. if ( this.size == 0) {
  99. this.startRow = 0;
  100. this.endRow = 0;
  101. } else {
  102. this.startRow = page.getStartRow() + 1;
  103. //计算实际的endRow(最后一页的时候特殊)
  104. this.endRow = this.startRow - 1 + this.size;
  105. }
  106. } else if (list instanceof Collection) {
  107. this.pageNum = 1;
  108. this.pageSize = list.size();
  109. this.pages = this.pageSize > 0 ? 1 : 0;
  110. this.size = list.size();
  111. this.startRow = 0;
  112. this.endRow = list.size() > 0 ? list.size() - 1 : 0;
  113. }
  114. if (list instanceof Collection) {
  115. this.navigatePages = navigatePages;
  116. //计算导航页
  117. calcNavigatepageNums();
  118. //计算前后页,第一页,最后一页
  119. calcPage();
  120. //判断页面边界
  121. judgePageBoudary();
  122. }
  123. }
  124. public static PageInfo of (List list) {
  125. return new PageInfo(list);
  126. }
  127. public static PageInfo of (List list, int navigatePages) {
  128. return new PageInfo(list, navigatePages);
  129. }
  130. /**
  131. * 计算导航页
  132. */
  133. private void calcNavigatepageNums () {
  134. //当总页数小于或等于导航页码数时
  135. if (pages <= navigatePages) {
  136. navigatepageNums = new int[pages];
  137. for ( int i = 0; i < pages; i++) {
  138. navigatepageNums[i] = i + 1;
  139. }
  140. } else { //当总页数大于导航页码数时
  141. navigatepageNums = new int[navigatePages];
  142. int startNum = pageNum - navigatePages / 2;
  143. int endNum = pageNum + navigatePages / 2;
  144. if (startNum < 1) {
  145. startNum = 1;
  146. //(最前navigatePages页
  147. for ( int i = 0; i < navigatePages; i++) {
  148. navigatepageNums[i] = startNum++;
  149. }
  150. } else if (endNum > pages) {
  151. endNum = pages;
  152. //最后navigatePages页
  153. for ( int i = navigatePages - 1; i >= 0; i--) {
  154. navigatepageNums[i] = endNum--;
  155. }
  156. } else {
  157. //所有中间页
  158. for ( int i = 0; i < navigatePages; i++) {
  159. navigatepageNums[i] = startNum++;
  160. }
  161. }
  162. }
  163. }
  164. /**
  165. * 计算前后页,第一页,最后一页
  166. */
  167. private void calcPage () {
  168. if (navigatepageNums != null && navigatepageNums.length > 0) {
  169. navigateFirstPage = navigatepageNums[ 0];
  170. navigateLastPage = navigatepageNums[navigatepageNums.length - 1];
  171. if (pageNum > 1) {
  172. prePage = pageNum - 1;
  173. }
  174. if (pageNum < pages) {
  175. nextPage = pageNum + 1;
  176. }
  177. }
  178. }
  179. /**
  180. * 判定页面边界
  181. */
  182. private void judgePageBoudary () {
  183. isFirstPage = pageNum == 1;
  184. isLastPage = pageNum == pages || pages == 0;
  185. hasPreviousPage = pageNum > 1;
  186. hasNextPage = pageNum < pages;
  187. }
  188. public int getPageNum () {
  189. return pageNum;
  190. }
  191. public void setPageNum (int pageNum) {
  192. this.pageNum = pageNum;
  193. }
  194. public int getPageSize () {
  195. return pageSize;
  196. }
  197. public void setPageSize (int pageSize) {
  198. this.pageSize = pageSize;
  199. }
  200. public int getSize () {
  201. return size;
  202. }
  203. public void setSize (int size) {
  204. this.size = size;
  205. }
  206. public int getStartRow () {
  207. return startRow;
  208. }
  209. public void setStartRow (int startRow) {
  210. this.startRow = startRow;
  211. }
  212. public int getEndRow () {
  213. return endRow;
  214. }
  215. public void setEndRow (int endRow) {
  216. this.endRow = endRow;
  217. }
  218. public int getPages () {
  219. return pages;
  220. }
  221. public void setPages (int pages) {
  222. this.pages = pages;
  223. }
  224. public int getPrePage () {
  225. return prePage;
  226. }
  227. public void setPrePage (int prePage) {
  228. this.prePage = prePage;
  229. }
  230. public int getNextPage () {
  231. return nextPage;
  232. }
  233. public void setNextPage (int nextPage) {
  234. this.nextPage = nextPage;
  235. }
  236. public boolean isIsFirstPage () {
  237. return isFirstPage;
  238. }
  239. public void setIsFirstPage (boolean isFirstPage) {
  240. this.isFirstPage = isFirstPage;
  241. }
  242. public boolean isIsLastPage () {
  243. return isLastPage;
  244. }
  245. public void setIsLastPage (boolean isLastPage) {
  246. this.isLastPage = isLastPage;
  247. }
  248. public boolean isHasPreviousPage () {
  249. return hasPreviousPage;
  250. }
  251. public void setHasPreviousPage (boolean hasPreviousPage) {
  252. this.hasPreviousPage = hasPreviousPage;
  253. }
  254. public boolean isHasNextPage () {
  255. return hasNextPage;
  256. }
  257. public void setHasNextPage (boolean hasNextPage) {
  258. this.hasNextPage = hasNextPage;
  259. }
  260. public int getNavigatePages () {
  261. return navigatePages;
  262. }
  263. public void setNavigatePages (int navigatePages) {
  264. this.navigatePages = navigatePages;
  265. }
  266. public int[] getNavigatepageNums() {
  267. return navigatepageNums;
  268. }
  269. public void setNavigatepageNums (int[] navigatepageNums) {
  270. this.navigatepageNums = navigatepageNums;
  271. }
  272. public int getNavigateFirstPage () {
  273. return navigateFirstPage;
  274. }
  275. public int getNavigateLastPage () {
  276. return navigateLastPage;
  277. }
  278. public void setNavigateFirstPage (int navigateFirstPage) {
  279. this.navigateFirstPage = navigateFirstPage;
  280. }
  281. public void setNavigateLastPage (int navigateLastPage) {
  282. this.navigateLastPage = navigateLastPage;
  283. }
  284. @Override
  285. public String toString () {
  286. final StringBuilder sb = new StringBuilder( "PageInfo{");
  287. sb.append( "pageNum=").append(pageNum);
  288. sb.append( ", pageSize=").append(pageSize);
  289. sb.append( ", size=").append(size);
  290. sb.append( ", startRow=").append(startRow);
  291. sb.append( ", endRow=").append(endRow);
  292. sb.append( ", total=").append(total);
  293. sb.append( ", pages=").append(pages);
  294. sb.append( ", list=").append(list);
  295. sb.append( ", prePage=").append(prePage);
  296. sb.append( ", nextPage=").append(nextPage);
  297. sb.append( ", isFirstPage=").append(isFirstPage);
  298. sb.append( ", isLastPage=").append(isLastPage);
  299. sb.append( ", hasPreviousPage=").append(hasPreviousPage);
  300. sb.append( ", hasNextPage=").append(hasNextPage);
  301. sb.append( ", navigatePages=").append(navigatePages);
  302. sb.append( ", navigateFirstPage=").append(navigateFirstPage);
  303. sb.append( ", navigateLastPage=").append(navigateLastPage);
  304. sb.append( ", navigatepageNums=");
  305. if (navigatepageNums == null) {
  306. sb.append( "null");
  307. } else {
  308. sb.append( '[');
  309. for ( int i = 0; i < navigatepageNums.length; ++i) {
  310. sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]);
  311. }
  312. sb.append( ']');
  313. }
  314. sb.append( '}');
  315. return sb.toString();
  316. }
  317. }

三:Page和PageInfo:使用;(重要!)

(1)首先,就是开启分页,设置【pageNum:当前页】和【pageSize:每页记录数】;

MyBatis_02_Page&PageInfo_第3张图片

 (2)然后,我们会利用Mybatis,根据上面设置的分页条件,去查询数据库,得到查询结果;

MyBatis_02_Page&PageInfo_第4张图片

(3)然后,可以根据前端对分页数据格式的要求,可以把(3)中查到的分页数据,包装成Page对象;

MyBatis_02_Page&PageInfo_第5张图片

(4)或者,包装成PageInfo对象;

MyBatis_02_Page&PageInfo_第6张图片

(5)使用Page对象还是使用PageInfo对象?

          ● 目前的个人理解:使用Page对象还是PageInfo对象,需要根据前端的要求,或者说是根据接口对返回数据的格式的要求;

          ● 比如,在【Spring Boot电商项目29:商品分类模块八:后台的【分类列表(平铺)】接口;】中,【分类列表(平铺)接口】的接口文档要求,接口的返回是:

MyBatis_02_Page&PageInfo_第7张图片

所以,对于这样的接口要求,很自然就是使用PageInfo对象;

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览 125711 人正在系统学习中

你可能感兴趣的:(MyBatis,mybatis,PageHelper,Page-PageInfo)