Skip to content

Commit e3898ae

Browse files
committed
Java:JFinal Demo 升级 APIJSON 8, apijson-framework 7.2 和 MySQL-JDBC 9.2
1 parent eb051b0 commit e3898ae

File tree

11 files changed

+333
-215
lines changed

11 files changed

+333
-215
lines changed

APIJSON-Java-Server/APIJSONFinal/pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
<dependency>
3737
<groupId>com.github.Tencent</groupId>
3838
<artifactId>APIJSON</artifactId>
39-
<version>7.1.0</version>
39+
<version>8.0.0.0.0</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>com.github.APIJSON</groupId>
4343
<artifactId>apijson-framework</artifactId>
44-
<version>7.1.5</version>
44+
<version>7.2.0.0</version>
4545
</dependency>
4646
<!-- 可使用 libs 目录的 apijson-orm.jar 和 apijson-framework.jar 来替代,两种方式二选一 >>>>>>>>>> -->
4747

4848
<!-- 需要用的数据库 JDBC 驱动 -->
4949
<dependency>
50-
<groupId>mysql</groupId>
51-
<artifactId>mysql-connector-java</artifactId>
52-
<version>8.0.29</version>
50+
<groupId>com.mysql</groupId>
51+
<artifactId>mysql-connector-j</artifactId>
52+
<version>9.2.0</version>
5353
</dependency>
5454
<dependency>
5555
<groupId>org.postgresql</groupId>
5656
<artifactId>postgresql</artifactId>
57-
<version>42.3.4</version>
57+
<version>42.7.2</version>
5858
</dependency>
5959
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
6060

APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/DemoFunctionParser.java

+42-39
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,26 @@
2121

2222
import javax.servlet.http.HttpSession;
2323

24-
import com.alibaba.fastjson.JSONArray;
25-
import com.alibaba.fastjson.JSONObject;
26-
2724
import apijson.JSONResponse;
2825
import apijson.NotNull;
2926
import apijson.RequestMethod;
3027
import apijson.StringUtil;
28+
import com.alibaba.fastjson.JSONArray;
29+
import com.alibaba.fastjson.JSONObject;
30+
3131
import apijson.framework.javax.APIJSONFunctionParser;
3232
import apijson.orm.AbstractVerifier;
33-
import apijson.orm.JSONRequest;
3433
import apijson.orm.Visitor;
3534

35+
import static apijson.JSONObject.KEY_COLUMN;
36+
import static apijson.JSONRequest.KEY_COUNT;
37+
3638

3739
/**可远程调用的函数类,用于自定义业务逻辑处理
3840
* 具体见 https://github.com/Tencent/APIJSON/issues/101
3941
* @author Lemon
4042
*/
41-
public class DemoFunctionParser extends APIJSONFunctionParser {
43+
public class DemoFunctionParser extends APIJSONFunctionParser<Long, JSONObject, JSONArray> {
4244
public static final String TAG = "DemoFunctionParser";
4345

4446
public DemoFunctionParser() {
@@ -48,34 +50,34 @@ public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObj
4850
super(method, tag, version, request, session);
4951
}
5052

51-
public Visitor<Long> getCurrentUser(@NotNull JSONObject current) {
53+
public Visitor<Long> getCurrentUser(@NotNull JSONObject curObj) {
5254
return DemoVerifier.getVisitor(getSession());
5355
}
5456

55-
public Long getCurrentUserId(@NotNull JSONObject current) {
57+
public Long getCurrentUserId(@NotNull JSONObject curObj) {
5658
return DemoVerifier.getVisitorId(getSession());
5759
}
5860

59-
public List<Long> getCurrentUserIdAsList(@NotNull JSONObject current) {
61+
public List<Long> getCurrentUserIdAsList(@NotNull JSONObject curObj) {
6062
List<Long> list = new ArrayList<>(1);
6163
list.add(DemoVerifier.getVisitorId(getSession()));
6264
return list;
6365
}
6466

65-
public List<Long> getCurrentContactIdList(@NotNull JSONObject current) {
66-
Visitor<Long> user = getCurrentUser(current);
67+
public List<Long> getCurrentContactIdList(@NotNull JSONObject curObj) {
68+
Visitor<Long> user = getCurrentUser(curObj);
6769
return user == null ? null : user.getContactIdList();
6870
}
6971

7072

7173
/**
72-
* @param current
74+
* @param curObj
7375
* @param idList
7476
* @return
7577
* @throws Exception
7678
*/
77-
public void verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception {
78-
Object obj = current.get(idList);
79+
public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) throws Exception {
80+
Object obj = curObj.get(idList);
7981
if (obj == null) {
8082
return;
8183
}
@@ -98,13 +100,13 @@ public void verifyIdList(@NotNull JSONObject current, @NotNull String idList) th
98100

99101

100102
/**
101-
* @param current
103+
* @param curObj
102104
* @param urlList
103105
* @return
104106
* @throws Exception
105107
*/
106-
public void verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception {
107-
Object obj = current.get(urlList);
108+
public void verifyURLList(@NotNull JSONObject curObj, @NotNull String urlList) throws Exception {
109+
Object obj = curObj.get(urlList);
108110
if (obj == null) {
109111
return;
110112
}
@@ -127,21 +129,21 @@ public void verifyURLList(@NotNull JSONObject current, @NotNull String urlList)
127129

128130

129131
/**
130-
* @param current
132+
* @param curObj
131133
* @param momentId
132134
* @return
133135
* @throws Exception
134136
*/
135-
public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String momentId) throws Exception {
136-
long mid = current.getLongValue(momentId);
137-
if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
137+
public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String momentId) throws Exception {
138+
long mid = curObj.getLongValue(momentId);
139+
if (mid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
138140
return 0;
139141
}
140142

141-
JSONRequest request = new JSONRequest();
143+
JSONObject request = JSON.createJSONObject();
142144

143145
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
144-
JSONRequest comment = new JSONRequest();
146+
JSONObject comment = JSON.createJSONObject();
145147
comment.put("momentId", mid);
146148

147149
request.put("Comment", comment);
@@ -155,22 +157,22 @@ public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String mo
155157

156158

157159
/**删除评论的子评论
158-
* @param current
160+
* @param curObj
159161
* @param toId
160162
* @return
161163
*/
162-
public int deleteChildComment(@NotNull JSONObject current, @NotNull String toId) throws Exception {
163-
long tid = current.getLongValue(toId);
164-
if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
164+
public int deleteChildComment(@NotNull JSONObject curObj, @NotNull String toId) throws Exception {
165+
long tid = curObj.getLongValue(toId);
166+
if (tid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
165167
return 0;
166168
}
167169

168170
//递归获取到全部子评论id
169171

170-
JSONRequest request = new JSONRequest();
172+
JSONObject request = JSON.createJSONObject();
171173

172174
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
173-
JSONRequest comment = new JSONRequest();
175+
JSONObject comment = JSON.createJSONObject();
174176
comment.put("id{}", getChildCommentIdList(tid));
175177

176178
request.put("Comment", comment);
@@ -187,19 +189,20 @@ private JSONArray getChildCommentIdList(long tid) {
187189

188190
JSONArray arr = new JSONArray();
189191

190-
JSONRequest request = new JSONRequest();
192+
JSONObject request = JSON.createJSONObject();
191193

192194
//Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193-
JSONRequest idItem = new JSONRequest();
195+
JSONObject idItem = JSON.createJSONObject();
194196

195197
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196-
JSONRequest comment = new JSONRequest();
198+
JSONObject comment = JSON.createJSONObject();
197199
comment.put("toId", tid);
198-
comment.setColumn("id");
200+
comment.put(KEY_COLUMN, "id");
199201
idItem.put("Comment", comment);
202+
idItem.put(KEY_COUNT, 0);
200203
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
201204

202-
request.putAll(idItem.toArray(0, 0, "Comment-id"));
205+
request.put("Comment-id[]", idItem);
203206
//Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
204207

205208
JSONObject rp = new DemoParser().setNeedVerify(false).parseResponse(request);
@@ -223,23 +226,23 @@ private JSONArray getChildCommentIdList(long tid) {
223226

224227

225228
/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
226-
* @param current
229+
* @param curObj
227230
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
228231
* @throws Exception
229232
*/
230-
public JSONArray getIdList(@NotNull JSONObject current) {
233+
public JSONArray getIdList(@NotNull JSONObject curObj) {
231234
return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710)));
232235
}
233236

234237

235238
/**TODO 仅用来测试 "key-()":"verifyAccess()"
236-
* @param current
239+
* @param curObj
237240
* @return
238241
* @throws Exception
239242
*/
240-
public Object verifyAccess(@NotNull JSONObject current) throws Exception {
241-
long userId = current.getLongValue(JSONRequest.KEY_USER_ID);
242-
String role = current.getString(JSONRequest.KEY_ROLE);
243+
public Object verifyAccess(@NotNull JSONObject curObj) throws Exception {
244+
long userId = curObj.getLongValue(apijson.JSONObject.KEY_USER_ID);
245+
String role = curObj.getString(apijson.JSONObject.KEY_ROLE);
243246
if (AbstractVerifier.OWNER.equals(role) && userId != (Long) DemoVerifier.getVisitorId(getSession())) {
244247
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
245248
}

APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/DemoParser.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414

1515
package apijson.demo;
1616

17+
import apijson.JSONResponse;
18+
import com.alibaba.fastjson.JSONArray;
19+
import com.alibaba.fastjson.JSONObject;
1720
import apijson.RequestMethod;
1821
import apijson.framework.javax.APIJSONParser;
1922

23+
import static apijson.JSONResponse.*;
24+
2025

2126
/**请求解析器
2227
* 具体见 https://github.com/Tencent/APIJSON/issues/38
2328
* @author Lemon
2429
*/
25-
public class DemoParser extends APIJSONParser<Long> {
30+
public class DemoParser extends APIJSONParser<Long, JSONObject, JSONArray> {
2631

2732
public DemoParser() {
2833
super();
@@ -34,6 +39,18 @@ public DemoParser(RequestMethod method, boolean needVerify) {
3439
super(method, needVerify);
3540
}
3641

42+
public static JSONObject parseRequest(String request) {
43+
try {
44+
return JSON.parseObject(request);
45+
} catch (Throwable e) {
46+
JSONObject obj = JSON.createJSONObject();
47+
obj.put(KEY_OK, false);
48+
obj.put(KEY_CODE, JSONResponse.CODE_ILLEGAL_ARGUMENT);
49+
obj.put(KEY_MSG, "JSON 格式不合法!" + request);
50+
return obj;
51+
}
52+
}
53+
3754
// 可重写来设置最大查询数量
3855
// @Override
3956
// public int getMaxQueryCount() {

APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/DemoSQLConfig.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@
1919
import static apijson.framework.javax.APIJSONConstant.USER_;
2020
import static apijson.framework.javax.APIJSONConstant.USER_ID;
2121

22-
import java.text.SimpleDateFormat;
23-
import java.util.Arrays;
24-
import java.util.HashMap;
2522
import java.util.List;
26-
import java.util.Map;
2723

28-
import apijson.Log;
24+
import apijson.RequestMethod;
25+
import com.alibaba.fastjson.JSONArray;
26+
import com.alibaba.fastjson.JSONObject;
2927
import com.alibaba.fastjson.annotation.JSONField;
3028

31-
import apijson.RequestMethod;
32-
import apijson.StringUtil;
3329
import apijson.framework.javax.APIJSONSQLConfig;
3430
import apijson.orm.AbstractSQLConfig;
3531
import apijson.orm.Join;
@@ -42,7 +38,7 @@
4238
* https://github.com/Tencent/APIJSON/blob/master/%E8%AF%A6%E7%BB%86%E7%9A%84%E8%AF%B4%E6%98%8E%E6%96%87%E6%A1%A3.md#c-1-1%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E9%93%BE%E6%8E%A5
4339
* @author Lemon
4440
*/
45-
public class DemoSQLConfig extends APIJSONSQLConfig<Long> {
41+
public class DemoSQLConfig extends APIJSONSQLConfig<Long, JSONObject, JSONArray> {
4642

4743
public DemoSQLConfig() {
4844
super();
@@ -66,10 +62,10 @@ public DemoSQLConfig(RequestMethod method, String table) {
6662
// TABLE_KEY_MAP.put(Privacy.class.getSimpleName(), "apijson_privacy");
6763

6864
// 主键名映射
69-
SIMPLE_CALLBACK = new SimpleCallback<Long>() {
65+
SIMPLE_CALLBACK = new SimpleCallback<Long, JSONObject, JSONArray>() {
7066

7167
@Override
72-
public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table) {
68+
public AbstractSQLConfig<Long, JSONObject, JSONArray> getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table) {
7369
return new DemoSQLConfig(method, table);
7470
}
7571

@@ -117,7 +113,7 @@ public String getUserIdKey(String database, String schema, String datasource, St
117113
// 如果 DemoSQLExecutor.getConnection 能拿到连接池的有效 Connection,则这里不需要配置 dbVersion, dbUri, dbAccount, dbPassword
118114

119115
@Override
120-
public String getDBVersion() {
116+
public String gainDBVersion() {
121117
if (isMySQL()) {
122118
return "5.7.22"; //"8.0.11"; //TODO 改成你自己的 MySQL 或 PostgreSQL 数据库版本号 //MYSQL 8 和 7 使用的 JDBC 配置不一样
123119
}
@@ -141,7 +137,7 @@ public String getDBVersion() {
141137

142138
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
143139
@Override
144-
public String getDBUri() {
140+
public String gainDBUri() {
145141
if (isMySQL()) {
146142
// 这个是 MySQL 8.0 及以上,要加 userSSL=false return "jdbc:mysql://localhost:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
147143
// 以下是 MySQL 5.7 及以下
@@ -168,7 +164,7 @@ public String getDBUri() {
168164

169165
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
170166
@Override
171-
public String getDBAccount() {
167+
public String gainDBAccount() {
172168
if (isMySQL()) {
173169
return "root"; //TODO 改成你自己的
174170
}
@@ -192,7 +188,7 @@ public String getDBAccount() {
192188

193189
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
194190
@Override
195-
public String getDBPassword() {
191+
public String gainDBPassword() {
196192
if (isMySQL()) {
197193
return "apijson"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用, 默认密码为空字符串 ""
198194
}
@@ -287,16 +283,16 @@ public String getDBPassword() {
287283

288284

289285
@Override
290-
protected void onGetCrossJoinString(Join j) throws UnsupportedOperationException {
291-
// 开启 CROSS JOIN 笛卡尔积联表 super.onGetCrossJoinString(j);
286+
protected void onGainCrossJoinString(Join<Long, JSONObject, JSONArray> join) throws UnsupportedOperationException {
287+
// 开启 CROSS JOIN 笛卡尔积联表 super.onGetCrossJoinString(join);
292288
}
293289
@Override
294-
protected void onJoinNotRelation(String sql, String quote, Join j, String jt, List<On> onList, On on) {
295-
// 开启 JOIN ON t1.c1 != t2.c2 等不等式关联 super.onJoinNotRelation(sql, quote, j, jt, onList, on);
290+
protected void onJoinNotRelation(String sql, String quote, Join<Long, JSONObject, JSONArray> join, String jt, List<On> onList, On on) {
291+
// 开启 JOIN ON t1.c1 != t2.c2 等不等式关联 super.onJoinNotRelation(sql, quote, join, jt, onList, on);
296292
}
297293
@Override
298-
protected void onJoinComplexRelation(String sql, String quote, Join j, String jt, List<On> onList, On on) {
299-
// 开启 JOIN ON t1.c1 LIKE concat('%', t2.c2, '%') 等复杂关联 super.onJoinComplextRelation(sql, quote, j, jt, onList, on);
294+
protected void onJoinComplexRelation(String sql, String quote, Join<Long, JSONObject, JSONArray> join, String jt, List<On> onList, On on) {
295+
// 开启 JOIN ON t1.c1 LIKE concat('%', t2.c2, '%') 等复杂关联 super.onJoinComplexRelation(sql, quote, join, jt, onList, on);
300296
}
301297

302298
}

APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/DemoVerifier.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
package apijson.demo;
1616

1717
import apijson.framework.javax.APIJSONVerifier;
18+
import com.alibaba.fastjson.JSONArray;
19+
import com.alibaba.fastjson.JSONObject;
1820

1921
/**安全校验器,校验请求参数、角色与权限等
2022
* 具体见 https://github.com/Tencent/APIJSON/issues/12
2123
* @author Lemon
2224
*/
23-
public class DemoVerifier extends APIJSONVerifier<Long> {
25+
public class DemoVerifier extends APIJSONVerifier<Long, JSONObject, JSONArray> {
2426
public static final String TAG = "DemoVerifier";
2527

2628
// 重写方法来自定义字段名等

0 commit comments

Comments
 (0)