Skip to content

Commit 10d5e29

Browse files
committed
Java: 主键类型相关代码全面使用泛型;新增驼峰与蛇形命名互转配置示例
1 parent 23b5c20 commit 10d5e29

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* 具体见 https://github.com/Tencent/APIJSON/issues/101
3838
* @author Lemon
3939
*/
40-
public class DemoFunctionParser extends APIJSONFunctionParser {
40+
public class DemoFunctionParser extends APIJSONFunctionParser<Long> {
4141
public static final String TAG = "DemoFunctionParser";
4242

4343
static {
@@ -102,7 +102,7 @@ public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) thr
102102

103103
@Override
104104
public boolean isContain(JSONObject curObj, String array, String value) {
105-
List<String> list = apijson.JSON.parseArray(JSON.toJSONString(getArgVal(array)), String.class);
105+
List<String> list = apijson.JSON.parseArray(getArgStr(array), String.class);
106106
Object val = getArgVal(value);
107107
return list != null && list.contains(val == null ? null : String.valueOf(val));
108108
}
@@ -143,7 +143,7 @@ public void verifyURLList(@NotNull JSONObject curObj, @NotNull String urlList) t
143143
* @throws Exception
144144
*/
145145
public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String momentId) throws Exception {
146-
Long mid = getArgVal(momentId);
146+
Long mid = getArgLong(momentId);
147147
if (mid == null || mid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
148148
return 0;
149149
}
@@ -170,7 +170,7 @@ public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String mom
170170
* @return
171171
*/
172172
public int deleteChildComment(@NotNull JSONObject curObj, @NotNull String toId) throws Exception {
173-
Long tid = getArgVal(toId);
173+
Long tid = getArgLong(toId);
174174
if (tid == null || tid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
175175
return 0;
176176
}
@@ -194,7 +194,6 @@ public int deleteChildComment(@NotNull JSONObject curObj, @NotNull String toId)
194194

195195

196196
private JSONArray getChildCommentIdList(long tid) {
197-
198197
JSONArray arr = new JSONArray();
199198

200199
JSONRequest request = new JSONRequest();
@@ -248,8 +247,8 @@ public JSONArray getIdList(@NotNull JSONObject curObj) {
248247
* @throws Exception
249248
*/
250249
public Object verifyAccess(@NotNull JSONObject curObj) throws Exception {
251-
String role = getArgVal(JSONRequest.KEY_ROLE);
252-
Long userId = getArgVal(JSONRequest.KEY_USER_ID);
250+
String role = getArgStr(JSONRequest.KEY_ROLE);
251+
Long userId = getArgLong(JSONRequest.KEY_USER_ID);
253252
if (AbstractVerifier.OWNER.equals(role) && ! Objects.equals(userId, DemoVerifier.getVisitorId(getSession()))) {
254253
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
255254
}

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoObjectParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
/**对象解析器,用来简化 Parser
3131
* @author Lemon
3232
*/
33-
public class DemoObjectParser extends APIJSONObjectParser {
33+
public class DemoObjectParser extends APIJSONObjectParser<Long> {
3434

35-
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig arrayConfig
35+
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig<Long> arrayConfig
3636
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
3737
super(session, request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable);
3838
}
3939

4040
@Override
41-
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
41+
public SQLConfig<Long> newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
4242
return DemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
4343
}
4444

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import java.text.SimpleDateFormat;
2323
import java.util.*;
2424

25-
import apijson.Log;
26-
import apijson.NotNull;
25+
import apijson.*;
2726
import apijson.orm.AbstractSQLConfig;
2827
import com.alibaba.fastjson.annotation.JSONField;
2928

@@ -391,5 +390,10 @@ protected void onJoinComplexRelation(String sql, String quote, Join join, String
391390
}
392391

393392

393+
// 取消注释可将前端传参驼峰命名转为蛇形命名 aBCdEfg => upper ? A_B_CD_EFG : a_b_cd_efg
394+
// @Override
395+
// public String getSQLKey(String key) {
396+
// return super.getSQLKey(JSONRequest.recoverUnderline(key, false));
397+
// }
394398

395399
}

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLExecutor.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.alibaba.fastjson.JSONObject;
2222
import com.datastax.oss.driver.api.core.CqlSession;
2323
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
24-
import com.datastax.oss.driver.api.core.cql.ResultSet;
24+
import java.sql.ResultSet;
2525
import com.datastax.oss.driver.api.core.cql.Row;
2626
//import com.vesoft.nebula.jdbc.impl.NebulaDriver;
2727
import com.zaxxer.hikari.HikariDataSource;
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.nio.file.Paths;
3333
import java.sql.Connection;
34+
import java.sql.ResultSetMetaData;
3435
import java.sql.SQLException;
3536
import java.util.*;
3637
import java.util.concurrent.TimeUnit;
@@ -82,7 +83,7 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
8283

8384
// 可重写以下方法,支持 Redis 等单机全局缓存或分布式缓存
8485
@Override
85-
public List<JSONObject> getCache(String sql, SQLConfig config) {
86+
public List<JSONObject> getCache(String sql, SQLConfig<Long> config) {
8687
List<JSONObject> list = super.getCache(sql, config);
8788
if (list == null) {
8889
try {
@@ -95,7 +96,7 @@ public List<JSONObject> getCache(String sql, SQLConfig config) {
9596
}
9697

9798
@Override
98-
public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig config) {
99+
public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig<Long> config) {
99100
super.putCache(sql, list, config);
100101

101102
String table = config != null && config.isMain() ? config.getTable() : null;
@@ -113,7 +114,7 @@ public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig c
113114
}
114115

115116
@Override
116-
public synchronized void removeCache(String sql, SQLConfig config) {
117+
public synchronized void removeCache(String sql, SQLConfig<Long> config) {
117118
super.removeCache(sql, config);
118119
try {
119120
if (config.getMethod() == RequestMethod.DELETE) { // 避免缓存击穿
@@ -130,9 +131,9 @@ public synchronized void removeCache(String sql, SQLConfig config) {
130131

131132
public static final String DATABASE_NEBULA = "NEBULA";
132133

133-
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
134+
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig<Long> 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
134135
@Override
135-
public Connection getConnection(SQLConfig config) throws Exception {
136+
public Connection getConnection(SQLConfig<Long> config) throws Exception {
136137
// if (DATABASE_NEBULA.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接
137138
// String uri = config.getDBUri();
138139
//
@@ -200,7 +201,7 @@ public Connection getConnection(SQLConfig config) throws Exception {
200201

201202

202203
@Override
203-
public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception {
204+
public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
204205
boolean isCassandra = config.isCassandra();
205206
boolean isInfluxDB = config.isInfluxDB();
206207

@@ -245,7 +246,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
245246
// sql = stt.getQuery();
246247
// }
247248

248-
ResultSet rs = session.execute(sql);
249+
com.datastax.oss.driver.api.core.cql.ResultSet rs = session.execute(sql);
249250

250251
List<Row> list = rs.all();
251252
if (list == null || list.isEmpty()) {
@@ -366,9 +367,16 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
366367

367368
// 不需要隐藏字段这个功能时,取消注释来提升性能
368369
// @Override
369-
// protected boolean isHideColumn(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
370+
// protected boolean isHideColumn(SQLConfig<Long> config, java.sql.ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
370371
// JSONObject table, int columnIndex, Map<String, JSONObject> childMap) throws SQLException {
371372
// return false;
372373
// }
373374

375+
// 取消注释可将前端传参驼峰命名转为蛇形命名 aBCdEfg => upper ? A_B_CD_EFG : a_b_cd_efg
376+
// @Override
377+
// protected String getKey(SQLConfig<Long> config, java.sql.ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, int columnIndex, Map<String, JSONObject> childMap) throws Exception {
378+
// String key = super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap);
379+
// return JSONResponse.formatUnderline(key, true);
380+
// }
381+
374382
}

0 commit comments

Comments
 (0)