Skip to content

Commit 2b6a2a9

Browse files
committed
新增支持分页页码 page 从 1 开始
1 parent 95d281d commit 2b6a2a9

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

APIJSONORM/src/main/java/apijson/JSONRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public JSONRequest setSubqueryRange(String range) {
152152
}
153153

154154
/**set from for Subquery
155-
* @param range
155+
* @param from
156156
* @return
157157
*/
158158
public JSONRequest setSubqueryFrom(String from) {

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,35 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
7070
public static boolean IS_PRINT_REQUEST_ENDTIME_LOG = false;
7171

7272

73-
public static int DEFAULT_QUERY_COUNT = 10;
73+
/**
74+
* 分页页码是否从 1 开始,默认为从 0 开始
75+
*/
76+
public static boolean IS_START_FROM_1 = false;
7477
public static int MAX_QUERY_PAGE = 100;
78+
public static int DEFAULT_QUERY_COUNT = 10;
7579
public static int MAX_QUERY_COUNT = 100;
7680
public static int MAX_UPDATE_COUNT = 10;
7781
public static int MAX_SQL_COUNT = 200;
7882
public static int MAX_OBJECT_COUNT = 5;
7983
public static int MAX_ARRAY_COUNT = 5;
8084
public static int MAX_QUERY_DEPTH = 5;
8185

86+
public boolean isStartFrom1() {
87+
return IS_START_FROM_1;
88+
}
8289
@Override
83-
public int getDefaultQueryCount() {
84-
return DEFAULT_QUERY_COUNT;
90+
public int getMinQueryPage() {
91+
return isStartFrom1() ? 1 : 0;
8592
}
8693
@Override
8794
public int getMaxQueryPage() {
8895
return MAX_QUERY_PAGE;
8996
}
9097
@Override
98+
public int getDefaultQueryCount() {
99+
return DEFAULT_QUERY_COUNT;
100+
}
101+
@Override
91102
public int getMaxQueryCount() {
92103
return MAX_QUERY_COUNT;
93104
}
@@ -1183,23 +1194,28 @@ public JSONObject onObjectParse(final JSONObject request
11831194
if (max < 0) {
11841195
max = 0;
11851196
}
1197+
int min = getMinQueryPage();
1198+
1199+
page += min;
1200+
max += min;
11861201

11871202
JSONObject pagination = new JSONObject(true);
11881203
Object explain = rp.get(JSONResponse.KEY_EXPLAIN);
11891204
if (explain instanceof JSONObject) {
11901205
pagination.put(JSONResponse.KEY_EXPLAIN, explain);
11911206
}
1207+
11921208
pagination.put(JSONResponse.KEY_TOTAL, total);
11931209
pagination.put(JSONRequest.KEY_COUNT, count);
11941210
pagination.put(JSONRequest.KEY_PAGE, page);
11951211
pagination.put(JSONResponse.KEY_MAX, max);
11961212
pagination.put(JSONResponse.KEY_MORE, page < max);
1197-
pagination.put(JSONResponse.KEY_FIRST, page == 0);
1213+
pagination.put(JSONResponse.KEY_FIRST, page == min);
11981214
pagination.put(JSONResponse.KEY_LAST, page == max);
11991215

12001216
putQueryResult(pathPrefix + JSONResponse.KEY_INFO, pagination);
12011217

1202-
if (total <= count*page) {
1218+
if (total <= count*(page - min)) {
12031219
query = JSONRequest.QUERY_TOTAL;//数量不够了,不再往后查询
12041220
}
12051221
}
@@ -1285,14 +1301,16 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
12851301
query2 = JSONRequest.QUERY_ALL;
12861302
break;
12871303
default:
1288-
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_QUERY + ":value 中 value 的值不合法!必须在 [0,1,2] 或 [TABLE, TOTAL, ALL] 内 !");
1304+
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_QUERY + ":value 中 value 的值不合法!必须在 [0, 1, 2] 或 [TABLE, TOTAL, ALL] 内 !");
12891305
}
12901306
}
12911307

1292-
int page2 = page == null ? 0 : page;
1308+
int minPage = getMinQueryPage(); // 兼容各种传 0 或 null/undefined 自动转 0 导致的问题
1309+
int page2 = page == null || page == 0 ? 0 : page - minPage;
1310+
12931311
int maxPage = getMaxQueryPage();
12941312
if (page2 < 0 || page2 > maxPage) {
1295-
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_PAGE + ":value 中 value 的值不合法!必须在 0-" + maxPage + " 内 !");
1313+
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_PAGE + ":value 中 value 的值不合法!必须在 " + minPage + "-" + maxPage + " 内 !");
12961314
}
12971315

12981316
//不用total限制数量了,只用中断机制,total只在query = 1,2的时候才获取

APIJSONORM/src/main/java/apijson/orm/Parser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, St
8383

8484
ObjectParser<T> createObjectParser(JSONObject request, String parentPath, SQLConfig<T> arrayConfig, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception;
8585

86-
int getDefaultQueryCount();
86+
int getMinQueryPage();
8787
int getMaxQueryPage();
88+
int getDefaultQueryCount();
8889
int getMaxQueryCount();
8990
int getMaxUpdateCount();
9091
int getMaxSQLCount();

0 commit comments

Comments
 (0)