详情见: https://redkale.org * * @author zhangjx */ -public final class Flipper implements Serializable, Cloneable { +public final class Flipper extends RowBound { public static int DEFAULT_LIMIT = 20; - @ConvertColumn(index = 1) - @Comment("记录行的偏移量,从0开始") - private int offset = 0; - - @ConvertColumn(index = 2) - @Comment("每页多少行") - private int limit = DEFAULT_LIMIT; - @ConvertColumn(index = 3) @Comment("排序字段, 可多字段排序") private String sort = ""; @@ -36,7 +27,7 @@ public final class Flipper implements Serializable, Cloneable { public Flipper() {} public Flipper(int limit) { - this.limit = limit > 0 ? limit : 0; + super(limit); } public Flipper(String sortColumn) { @@ -44,27 +35,42 @@ public final class Flipper implements Serializable, Cloneable { } public Flipper(int limit, int offset) { - this.limit = limit > 0 ? limit : 0; - this.offset = offset < 0 ? 0 : offset; + super(limit, offset); } public Flipper(int limit, String sortColumn) { - this.limit = limit > 0 ? limit : 0; + super(limit); this.sort = sortColumn; } public Flipper(int limit, int offset, String sortColumn) { - this.limit = limit > 0 ? limit : 0; - this.offset = offset < 0 ? 0 : offset; + super(limit, offset); this.sort = sortColumn; } + @Override + public Flipper limit(int limit) { + super.limit(limit); + return this; + } + + @Override + public Flipper maxLimit(int maxlimit) { + super.maxLimit(maxlimit); + return this; + } + + @Override + public Flipper unlimit() { + super.unlimit(); + return this; + } + public Flipper copyTo(Flipper copy) { if (copy == null) { return copy; } - copy.offset = this.offset; - copy.limit = this.limit; + super.copyTo(copy); copy.sort = this.sort; return copy; } @@ -73,8 +79,7 @@ public final class Flipper implements Serializable, Cloneable { if (copy == null) { return this; } - this.offset = copy.offset; - this.limit = copy.limit; + super.copyFrom(copy); this.sort = copy.sort; return this; } @@ -82,10 +87,11 @@ public final class Flipper implements Serializable, Cloneable { /** * 翻下一页 * - * @return Flipper + * @return RowBound */ + @Override public Flipper next() { - this.offset = getOffset() + this.limit; + super.next(); return this; } @@ -95,8 +101,9 @@ public final class Flipper implements Serializable, Cloneable { * @param current 页号, 从1开始 * @return Flipper */ + @Override public Flipper current(int current) { - this.offset = (current - 1) * this.limit; + super.current(current); return this; } @@ -108,8 +115,9 @@ public final class Flipper implements Serializable, Cloneable { @Override public String toString() { - return "{offset:" + this.offset + ",limit:" + this.limit - + ((sort == null || sort.isEmpty()) ? "" : (",sort:\"" + this.sort.replace('"', '\'') + "\"")) + "}"; + return "{\"limit\":" + this.limit + ",\"offset\":" + this.offset + + ((sort == null || sort.isEmpty()) ? "" : (",\"sort\":\"" + this.sort.replace('"', '\'') + "\"")) + + "}"; } @Override @@ -142,42 +150,6 @@ public final class Flipper implements Serializable, Cloneable { return Objects.equals(this.sort, other.sort); } - public int getLimit() { - return limit; - } - - public void setLimit(int limit) { - this.limit = limit; - } - - public Flipper limit(int limit) { - setLimit(limit); - return this; - } - - public Flipper maxLimit(int maxlimit) { - setLimit(Math.max(1, Math.min(maxlimit, limit))); - return this; - } - - public Flipper unlimit() { - this.limit = 0; - return this; - } - - public int getOffset() { - return offset; - } - - public void setOffset(int offset) { - this.offset = offset < 0 ? 0 : offset; - } - - public Flipper offset(int offset) { - setOffset(offset); - return this; - } - public String getSort() { return sort; } diff --git a/src/main/java/org/redkale/source/RowBound.java b/src/main/java/org/redkale/source/RowBound.java new file mode 100644 index 000000000..b2a31c9b8 --- /dev/null +++ b/src/main/java/org/redkale/source/RowBound.java @@ -0,0 +1,154 @@ +/* + +*/ + +package org.redkale.source; + +import java.io.Serializable; +import org.redkale.annotation.Comment; +import org.redkale.convert.ConvertColumn; + +/** + * 翻页对象, offset从0开始, limit必须大于0 + * + *
详情见: https://redkale.org
+ *
+ * @author zhangjx
+ * @since 2.8.0
+ */
+public class RowBound implements Serializable, Cloneable {
+
+ public static int DEFAULT_LIMIT = 20;
+
+ @ConvertColumn(index = 1)
+ @Comment("记录行的偏移量,从0开始")
+ protected int offset = 0;
+
+ @ConvertColumn(index = 2)
+ @Comment("每页多少行")
+ protected int limit = DEFAULT_LIMIT;
+
+ public RowBound() {}
+
+ public RowBound(int limit) {
+ this.limit = limit > 0 ? limit : 0;
+ }
+
+ public RowBound(int limit, int offset) {
+ this.limit = limit > 0 ? limit : 0;
+ this.offset = offset < 0 ? 0 : offset;
+ }
+
+ public RowBound copyTo(RowBound copy) {
+ if (copy == null) {
+ return copy;
+ }
+ copy.offset = this.offset;
+ copy.limit = this.limit;
+ return copy;
+ }
+
+ public RowBound copyFrom(RowBound copy) {
+ if (copy == null) {
+ return this;
+ }
+ this.offset = copy.offset;
+ this.limit = copy.limit;
+ return this;
+ }
+
+ /**
+ * 翻下一页
+ *
+ * @return RowBound
+ */
+ public RowBound next() {
+ this.offset = getOffset() + this.limit;
+ return this;
+ }
+
+ /**
+ * 设置当前页号,页号从1开始
+ *
+ * @param current 页号, 从1开始
+ * @return Flipper
+ */
+ public RowBound current(int current) {
+ this.offset = (current - 1) * this.limit;
+ return this;
+ }
+
+ @Override
+ @SuppressWarnings("CloneDoesntCallSuperClone")
+ public RowBound clone() {
+ return this.copyTo(new RowBound());
+ }
+
+ @Override
+ public String toString() {
+ return "{\"limit\":" + this.limit + ",\"offset\":" + this.offset + "}";
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 5;
+ hash = 37 * hash + this.offset;
+ hash = 37 * hash + this.limit;
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final RowBound other = (RowBound) obj;
+ return this.offset != other.offset && this.limit != other.limit;
+ }
+
+ public RowBound limit(int limit) {
+ setLimit(limit);
+ return this;
+ }
+
+ public RowBound maxLimit(int maxlimit) {
+ setLimit(Math.max(1, Math.min(maxlimit, limit)));
+ return this;
+ }
+
+ public RowBound unlimit() {
+ this.limit = 0;
+ return this;
+ }
+
+ public int getLimit() {
+ return limit;
+ }
+
+ public void setLimit(int limit) {
+ this.limit = limit;
+ }
+
+ public int getOffset() {
+ return offset;
+ }
+
+ public void setOffset(int offset) {
+ this.offset = offset < 0 ? 0 : offset;
+ }
+
+ public RowBound offset(int offset) {
+ setOffset(offset);
+ return this;
+ }
+
+ public static boolean validLimit(RowBound flipper) {
+ return flipper != null && flipper.getLimit() > 0;
+ }
+}
diff --git a/src/main/java/org/redkale/source/spi/DataSqlMapperBuilder.java b/src/main/java/org/redkale/source/spi/DataSqlMapperBuilder.java
index c1060d060..6e08066d4 100644
--- a/src/main/java/org/redkale/source/spi/DataSqlMapperBuilder.java
+++ b/src/main/java/org/redkale/source/spi/DataSqlMapperBuilder.java
@@ -36,7 +36,7 @@ import org.redkale.source.DataNativeSqlParser;
import org.redkale.source.DataSqlMapper;
import org.redkale.source.DataSqlSource;
import org.redkale.source.EntityBuilder;
-import org.redkale.source.Flipper;
+import org.redkale.source.RowBound;
import org.redkale.source.SourceException;
import org.redkale.util.RedkaleClassLoader;
import org.redkale.util.Sheet;
@@ -122,21 +122,21 @@ public final class DataSqlMapperBuilder {
AsmMethodBean methodBean = selfMethodBeans.get(AsmMethodBoost.getMethodBeanKey(method));
List