Page.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.edu.base;
  2. /**
  3. * @author wu youyang
  4. */
  5. import com.alibaba.fastjson.JSON;
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. import java.io.Serializable;
  9. @ApiModel(description = "分页对象")
  10. public class Page implements Serializable {
  11. private static final long serialVersionUID = -4312323165564562319L;
  12. @ApiModelProperty("页码")
  13. private int page = 1;
  14. @ApiModelProperty("每页条数")
  15. private int pageSize = 10;
  16. /**
  17. * 总记录数, -1: 未知
  18. */
  19. private int total = -1;
  20. public int getPage() {
  21. return page;
  22. }
  23. /**
  24. * 当前页码, 1-based
  25. */
  26. public void setPage(int page) {
  27. this.page = page;
  28. }
  29. /**
  30. * 每页记录数
  31. */
  32. public int getPageSize() {
  33. return pageSize;
  34. }
  35. public void setPageSize(int pageSize) {
  36. this.pageSize = pageSize;
  37. }
  38. public int getTotal() {
  39. return total;
  40. }
  41. public void setTotal(int total) {
  42. this.total = total;
  43. }
  44. public int getPrevPage() {
  45. return this.isFirstPage() ? this.getPage() : this.getPage() - 1;
  46. }
  47. public int getNextPage() {
  48. return this.isLastPage() ? this.getPage() : this.getPage() + 1;
  49. }
  50. public boolean isFirstPage() {
  51. return (1 == this.getPage());
  52. }
  53. public boolean isLastPage() {
  54. if(-1 == this.getPageCount()){
  55. return false;
  56. }
  57. return (this.getPageCount() < 1 || this.getPageCount() <= this.getPage());
  58. }
  59. /**
  60. * 页数, 根据total和pageSize计算
  61. * -1: 未知
  62. * @return
  63. */
  64. public int getPageCount() {
  65. if(-1 == total){
  66. return -1;
  67. }
  68. if (total < 1) {
  69. return 0;
  70. }
  71. if (pageSize < 1) {
  72. return 1;
  73. }
  74. return (0 == total % pageSize) ? total / pageSize : total / pageSize
  75. + 1;
  76. }
  77. /**
  78. *
  79. * mysql offset, 0-based, 根据page和pageSize计算
  80. *
  81. * @return
  82. */
  83. public int getOffset() {
  84. if (page < 1) {
  85. return 0;
  86. }
  87. return (page - 1) * pageSize;
  88. }
  89. /**
  90. * mysql limit, 0-based, 根据page和pageSize计算
  91. *
  92. * @return
  93. */
  94. public int getLimit() {
  95. return pageSize;
  96. }
  97. @Override
  98. public int hashCode() {
  99. final int prime = 31;
  100. int result = 1;
  101. result = prime * result + page;
  102. result = prime * result + pageSize;
  103. result = prime * result + total;
  104. return result;
  105. }
  106. @Override
  107. public boolean equals(Object obj) {
  108. if (this == obj){
  109. return true;
  110. }
  111. if (obj == null){
  112. return false;
  113. }
  114. if (getClass() != obj.getClass()){
  115. return false;
  116. }
  117. Page other = (Page) obj;
  118. if (page != other.page){
  119. return false;
  120. }
  121. if (pageSize != other.pageSize){
  122. return false;
  123. }
  124. if (total != other.total){
  125. return false;
  126. }
  127. return true;
  128. }
  129. public String toString() {
  130. return JSON.toJSONString(this);
  131. }
  132. }