Skip to content

Commit 83640a9

Browse files
committed
Java: MultiDataSource 新增支持 NoSQL 数据库 MongoDB,更新 APIAuto
1 parent e5e025b commit 83640a9

File tree

7 files changed

+235
-38
lines changed

7 files changed

+235
-38
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
<artifactId>databricks-jdbc</artifactId>
132132
<version>2.6.25-1</version>
133133
</dependency>
134+
<dependency>
135+
<groupId>org.mongodb</groupId>
136+
<artifactId>mongodb-jdbc</artifactId>
137+
<version>2.0.3</version>
138+
</dependency>
134139
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
135140
<!-- 数据库 JDBC 驱动 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
136141

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void customize(ConfigurableServletWebServerFactory server) {
107107

108108
// 支持 APIAuto 中 JavaScript 代码跨域请求
109109
@Bean
110-
public WebMvcConfigurer corsConfigurer() {
110+
public WebMvcConfigurer corsConfig() {
111111
return new WebMvcConfigurer() {
112112
@Override
113113
public void addCorsMappings(CorsRegistry registry) {

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import apijson.orm.AbstractSQLConfig;
2727
import com.alibaba.fastjson.annotation.JSONField;
2828

29-
import apijson.RequestMethod;
30-
import apijson.StringUtil;
3129
import apijson.column.ColumnUtil;
3230
import apijson.framework.APIJSONSQLConfig;
3331
import apijson.orm.Join;
@@ -49,9 +47,30 @@ public DemoSQLConfig(RequestMethod method, String table) {
4947
super(method, table);
5048
}
5149

50+
// 支持 NoSQL 数据库 MongoDB,APIJSON 6.4.0- 版本需要手动添加相关代码 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
51+
// public static final String DATABASE_MONGODB = "MONGODB";
52+
// @Override
53+
// public boolean isPrepared() {
54+
// return super.isPrepared() && ! isMongoDB(); // MongoDB JDBC 还不支持预编译
55+
// }
56+
//
57+
// public boolean isMongoDB() {
58+
// return DATABASE_MONGODB.equals(getDatabase());
59+
// }
60+
61+
// MongoDB 同时支持 `tbl` 反引号 和 "col" 双引号
62+
// @Override
63+
// public String getQuote() {
64+
// return "MONGODB".equals(getDatabase()) ? "`" : super.getQuote();
65+
// }
66+
5267
static {
68+
// DATABASE_LIST.add(DATABASE_MONGODB);
69+
70+
// 支持 NoSQL 数据库 MongoDB,APIJSON 6.4.0- 版本需要手动添加相关代码 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
71+
5372
DEFAULT_DATABASE = DATABASE_MYSQL; //TODO 默认数据库类型,改成你自己的。TiDB, MariaDB, OceanBase 这类兼容 MySQL 的可当做 MySQL 使用
54-
DEFAULT_SCHEMA = "sys"; //TODO 默认数据库名/模式,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: sys, SQL Server: dbo, Oracle:
73+
DEFAULT_SCHEMA = "sys"; // ""apijson"; //TODO 默认数据库名/模式,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: sys, SQL Server: dbo, Oracle:
5574

5675
// 表名和数据库不一致的,需要配置映射关系。只使用 APIJSONORM 时才需要;
5776
// 这个 Demo 用了 apijson-framework 且调用了 APIJSONApplication.init 则不需要
@@ -140,6 +159,10 @@ public String getDBVersion() {
140159
if (isTDengine()) {
141160
return "2.6.0.8"; //TODO 改成你自己的
142161
}
162+
if (isMongoDB()) {
163+
return "6.0.12"; //TODO 改成你自己的
164+
}
165+
143166
return null;
144167
}
145168

@@ -185,12 +208,14 @@ public String getDBUri() {
185208
if (isInfluxDB()) {
186209
return "http://localhost:8086";
187210
}
211+
if (isMongoDB()) {
212+
return "jdbc:mongodb://atlas-sql-6593c65c296c5865121e6ebe-xxskv.a.query.mongodb.net/myVirtualDatabase?ssl=true&authSource=admin";
213+
}
188214

189215
return "";
190216
}
191217

192218

193-
194219
// TODO 迁移到 APIJSON 主项目 <<<<<<<<<<<<<<<<<<<<
195220
@Override
196221
public String getSchema() {
@@ -221,7 +246,7 @@ public String getDBAccount() {
221246
}
222247

223248
if (isMySQL()) {
224-
return "root"; //TODO 改成你自己的
249+
return "root"; // ""apijson"; //TODO 改成你自己的
225250
}
226251
if (isPostgreSQL()) {
227252
return "postgres"; //TODO 改成你自己的
@@ -247,6 +272,9 @@ public String getDBAccount() {
247272
if (isInfluxDB()) {
248273
return "root";
249274
}
275+
if (isMongoDB()) {
276+
return "root"; //TODO 改成你自己的
277+
}
250278

251279
return null;
252280
}
@@ -290,6 +318,9 @@ public String getDBPassword() {
290318
if (isInfluxDB()) {
291319
return "apijson@123"; //TODO 改成你自己的
292320
}
321+
if (isMongoDB()) {
322+
return "apijson"; //TODO 改成你自己的
323+
}
293324

294325
return null;
295326
}

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/apijson/JSONRequest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ function parseJSON(s) {
261261
}
262262
// alertOfDebug("parseJSON s = \n" + s);
263263

264+
if (StringUtil.isEmpty(s, true)) {
265+
return null;
266+
}
267+
264268
try {
265269
return JSON.parse(s);
266270
} catch (e) {

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/apijson/JSONResponse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ var JSONResponse = {
855855
};
856856
}
857857

858-
if (notempty == true && StringUtil.isEmpty(real, true)) { // 空
858+
if (notempty == true && typeof real != 'boolean' && typeof real != 'number' && StringUtil.isEmpty(real, true)) { // 空
859859
log('compareWithStandard notempty == true && StringUtil.isEmpty(real, true) >> return COMPARE_VALUE_EMPTY');
860860
return {
861861
code: JSONResponse.COMPARE_VALUE_EMPTY,
@@ -1290,7 +1290,7 @@ var JSONResponse = {
12901290

12911291
var notempty = target.notempty;
12921292
log('updateStandard notempty = target.notempty = ' + notempty + ' >>');
1293-
if (real != null && typeof real != 'boolean') {
1293+
if (real != null && typeof real != 'boolean' && typeof real != 'number') {
12941294
notempty = target.notempty = StringUtil.isNotEmpty(real, true);
12951295
}
12961296

0 commit comments

Comments
 (0)