Pagination.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.plugins.pagination;
  17. import java.io.Serializable;
  18. import org.apache.ibatis.session.RowBounds;
  19. import com.baomidou.mybatisplus.toolkit.StringUtils;
  20. /**
  21. * <p>
  22. * 简单分页模型
  23. * </p>
  24. * 用户可以通过继承 org.apache.ibatis.session.RowBounds实现自己的分页模型<br>
  25. * 注意:插件仅支持RowBounds及其子类作为分页参数
  26. *
  27. * @author hubin
  28. * @Date 2016-01-23
  29. */
  30. public class Pagination extends RowBounds implements Serializable {
  31. private static final long serialVersionUID = 1L;
  32. /* 总数 */
  33. private int total;
  34. /* 每页显示条数,默认 10 */
  35. private int size = 10;
  36. /* 总页数 */
  37. private int pages;
  38. /* 当前页 */
  39. private int current = 1;
  40. /* 查询总记录数(默认 true) */
  41. private boolean searchCount = true;
  42. /**
  43. * 开启排序(默认 true) 只在代码逻辑判断 并不截取sql分析
  44. *
  45. * @see com.baomidou.mybatisplus.mapper.SqlHelper fillWrapper
  46. **/
  47. private boolean openSort = true;
  48. /**
  49. * <p>
  50. * SQL 排序 ORDER BY 字段,例如: id DESC(根据id倒序查询)
  51. * </p>
  52. * <p>
  53. * DESC 表示按倒序排序(即:从大到小排序)<br>
  54. * ASC 表示按正序排序(即:从小到大排序)
  55. * </p>
  56. */
  57. private String orderByField;
  58. /**
  59. * 是否为升序 ASC( 默认: true )
  60. */
  61. private boolean isAsc = true;
  62. public Pagination() {
  63. super();
  64. }
  65. /**
  66. * <p>
  67. * 分页构造函数
  68. * </p>
  69. *
  70. * @param current 当前页
  71. * @param size 每页显示条数
  72. */
  73. public Pagination(int current, int size) {
  74. this(current, size, true);
  75. }
  76. public Pagination(int current, int size, boolean searchCount) {
  77. this(current, size, searchCount, true);
  78. }
  79. public Pagination(int current, int size, boolean searchCount, boolean openSort) {
  80. super(offsetCurrent(current, size), size);
  81. if (current > 1) {
  82. this.current = current;
  83. }
  84. this.size = size;
  85. this.searchCount = searchCount;
  86. this.openSort = openSort;
  87. }
  88. protected static int offsetCurrent(int current, int size) {
  89. if (current > 0) {
  90. return (current - 1) * size;
  91. }
  92. return 0;
  93. }
  94. public int getOffsetCurrent() {
  95. return offsetCurrent(this.current, this.size);
  96. }
  97. public boolean hasPrevious() {
  98. return this.current > 1;
  99. }
  100. public boolean hasNext() {
  101. return this.current < this.pages;
  102. }
  103. public int getTotal() {
  104. return total;
  105. }
  106. public Pagination setTotal(int total) {
  107. this.total = total;
  108. return this;
  109. }
  110. public int getSize() {
  111. return size;
  112. }
  113. public Pagination setSize(int size) {
  114. this.size = size;
  115. return this;
  116. }
  117. public int getPages() {
  118. if (this.size == 0) {
  119. return 0;
  120. }
  121. this.pages = this.total / this.size;
  122. if (this.total % this.size != 0) {
  123. this.pages++;
  124. }
  125. return this.pages;
  126. }
  127. public int getCurrent() {
  128. return current;
  129. }
  130. public Pagination setCurrent(int current) {
  131. this.current = current;
  132. return this;
  133. }
  134. public boolean isSearchCount() {
  135. return searchCount;
  136. }
  137. public Pagination setSearchCount(boolean searchCount) {
  138. this.searchCount = searchCount;
  139. return this;
  140. }
  141. public String getOrderByField() {
  142. return orderByField;
  143. }
  144. public Pagination setOrderByField(String orderByField) {
  145. if (StringUtils.isNotEmpty(orderByField)) {
  146. this.orderByField = orderByField;
  147. }
  148. return this;
  149. }
  150. public boolean isOpenSort() {
  151. return openSort;
  152. }
  153. public Pagination setOpenSort(boolean openSort) {
  154. this.openSort = openSort;
  155. return this;
  156. }
  157. public boolean isAsc() {
  158. return isAsc;
  159. }
  160. public Pagination setAsc(boolean isAsc) {
  161. this.isAsc = isAsc;
  162. return this;
  163. }
  164. @Override
  165. public String toString() {
  166. return "Pagination { total=" + total + " ,size=" + size + " ,pages=" + pages + " ,current=" + current + " }";
  167. }
  168. }