Skip to content

Commit 1436f5b

Browse files
committed
初始化提交
1 parent 2ba8e6e commit 1436f5b

File tree

5 files changed

+324
-1
lines changed

5 files changed

+324
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/target/
2+
.classpath
3+
.project
4+
.settings
5+
.idea
6+
*.iml

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright ©2021 APIJSON(https://github.com/APIJSON)
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# apijson-mongodb [![](https://jitpack.io/v/APIJSON/apijson-mongodb.svg)](https://jitpack.io/#APIJSON/apijson-mongodb)
2+
腾讯 [APIJSON](https://github.com/Tencent/APIJSON) 的 MongoDB 数据库插件,可通过 Maven, Gradle 等远程依赖。<br />
3+
A MongoDB plugin for Tencent [APIJSON](https://github.com/Tencent/APIJSON)
4+
5+
![image](https://github-production-user-asset-6210df.s3.amazonaws.com/5738175/293696252-36406e20-b157-4121-bca7-280491462481.png)
6+
7+
## 添加依赖
8+
## Add Dependency
9+
10+
### Maven
11+
#### 1. 在 pom.xml 中添加 JitPack 仓库
12+
#### 1. Add the JitPack repository to pom.xml
13+
```xml
14+
<repositories>
15+
<repository>
16+
<id>jitpack.io</id>
17+
<url>https://jitpack.io</url>
18+
</repository>
19+
</repositories>
20+
```
21+
22+
![image](https://user-images.githubusercontent.com/5738175/167261814-d75d8fff-0e64-4534-a840-60ef628a8873.png)
23+
24+
<br />
25+
26+
#### 2. 在 pom.xml 中添加 apijson-mongodb 依赖
27+
#### 2. Add the apijson-mongodb dependency to pom.xml
28+
```xml
29+
<dependency>
30+
<groupId>com.github.APIJSON</groupId>
31+
<artifactId>apijson-mongodb</artifactId>
32+
<version>LATEST</version>
33+
</dependency>
34+
```
35+
36+
<br />
37+
38+
https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/pom.xml
39+
40+
<br />
41+
<br />
42+
43+
### Gradle
44+
#### 1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库
45+
#### 1. Add the JitPack repository in your root build.gradle at the end of repositories
46+
```gradle
47+
allprojects {
48+
repositories {
49+
maven { url 'https://jitpack.io' }
50+
}
51+
}
52+
```
53+
<br />
54+
55+
#### 2. 在项目某个 module 目录(例如 `app`) build.gradle 中添加 apijson-mongodb 依赖
56+
#### 2. Add the apijson-mongodb dependency in one of your modules(such as `app`)
57+
```gradle
58+
dependencies {
59+
implementation 'com.github.APIJSON:apijson-mongodb:latest'
60+
}
61+
```
62+
63+
<br />
64+
<br />
65+
<br />
66+
67+
## 使用
68+
## Usage
69+
70+
在你项目继承 AbstractSQLExecutor 的子类重写方法 getValue <br/>
71+
Override getValue in your SQLExecutor extends AbstractSQLExecutor
72+
```java
73+
@Override
74+
protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception {
75+
Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
76+
return MongoUtil.getValue(v);
77+
}
78+
```
79+
80+
#### [MongoUtil](/src/main/java/apijson/mongodb/MongoUtil.java) 的注释及 [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)[DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) <br />
81+
82+
#### See document in [MongoUtil](/src/main/java/apijson/mongodb/MongoUtil.java) and [DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) in [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)
83+
84+
<br />
85+
<br />
86+
<br />
87+
88+
有问题可以去 Tencent/APIJSON 提 issue <br />
89+
https://github.com/Tencent/APIJSON/issues/36
90+
91+
<br /><br />
92+
93+
#### 点右上角 ⭐Star 支持一下,谢谢 ^_^
94+
#### Please ⭐Star this project ^_^
95+
https://github.com/APIJSON/apijson-mongodb

pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apijson.mongodb</groupId>
7+
<artifactId>apijson-mongodb</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>apijson-mongodb</name>
12+
<description>A MongoDB JDBC plugin for APIJSON</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- APIJSON 需要用的依赖库,1.2.0 以上 -->
22+
<dependency>
23+
<groupId>com.alibaba</groupId>
24+
<artifactId>fastjson</artifactId>
25+
<version>1.2.83</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.mongodb</groupId>
29+
<artifactId>mongodb-jdbc</artifactId>
30+
<version>2.0.3</version>
31+
</dependency>
32+
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.8.1</version>
41+
<configuration>
42+
<source>1.8</source>
43+
<target>1.8</target>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<repositories>
50+
<!-- APIJSON 必须用到的托管平台 -->
51+
<repository>
52+
<id>jitpack.io</id>
53+
<url>https://jitpack.io</url>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
</project>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*Copyright ©2024 APIJSON(https://github.com/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.mongodb;
16+
17+
import com.mongodb.jdbc.MongoBsonValue;
18+
import org.bson.*;
19+
import org.bson.conversions.Bson;
20+
import org.bson.types.ObjectId;
21+
22+
import java.util.*;
23+
24+
25+
/**
26+
* @author Lemon
27+
* @see DemoSQLExecutor 重写 getValue 方法:
28+
* \@Override
29+
* protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition
30+
* , JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception {
31+
* Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
32+
* return MongoUtil.getValue(v);
33+
* }
34+
*/
35+
public class MongoUtil {
36+
public static final String TAG = "MongoUtil";
37+
38+
/**去除多余的包装
39+
* @param value
40+
* @return
41+
*/
42+
public static Object getValue(Object value) {
43+
if (value instanceof Bson) {
44+
try {
45+
value = ((Bson) value).toBsonDocument();
46+
} catch (Throwable e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
if (value instanceof MongoBsonValue) {
52+
value = ((MongoBsonValue) value).getBsonValue();
53+
}
54+
55+
56+
57+
if (value instanceof BsonValue) {
58+
BsonValue nv = (BsonValue) value;
59+
if (nv.isNull()) {
60+
return null;
61+
}
62+
63+
if (nv.isArray()) {
64+
List<BsonValue> vs = nv.asArray().getValues();
65+
if (vs == null) {
66+
return null;
67+
}
68+
69+
List<Object> l = new ArrayList<>();
70+
for (BsonValue v : vs) {
71+
Object cv = getValue(v);
72+
l.add(cv);
73+
}
74+
75+
return l;
76+
}
77+
78+
if (nv.isDocument()) {
79+
BsonDocument bd = nv.asDocument();
80+
Set<Map.Entry<String, BsonValue>> set = bd == null ? null : bd.entrySet();
81+
if (set == null) {
82+
return null;
83+
}
84+
85+
Map<Object, Object> m = new LinkedHashMap<>();
86+
for (Map.Entry<String, BsonValue> ety : set) {
87+
Object cv = getValue(ety.getValue());
88+
m.put(ety.getKey(), cv);
89+
}
90+
91+
return m;
92+
}
93+
94+
if (nv.isDBPointer()) {
95+
value = nv.asDBPointer().getId();
96+
}
97+
98+
if (nv.isBoolean()) {
99+
return nv.asBoolean().getValue();
100+
}
101+
if (nv.isBinary()) {
102+
return nv.asBinary().getData();
103+
}
104+
if (nv.isInt32()) {
105+
return nv.asInt32().getValue();
106+
}
107+
if (nv.isInt64()) {
108+
return nv.asInt64().getValue();
109+
}
110+
if (nv.isDouble()) {
111+
return nv.asDouble().getValue();
112+
}
113+
if (nv.isNumber()) {
114+
return nv.asNumber().doubleValue();
115+
}
116+
if (nv.isDecimal128()) {
117+
return nv.asDecimal128().doubleValue();
118+
}
119+
if (nv.isObjectId()) {
120+
ObjectId v = nv.asObjectId().getValue();
121+
return v == null ? null : v.toString();
122+
}
123+
if (nv.isDateTime()) {
124+
long v = nv.asDateTime().getValue();
125+
return v; // new Timestamp(v);
126+
}
127+
if (nv.isString()) {
128+
return nv.asString().getValue();
129+
}
130+
if (nv.isSymbol()) {
131+
return nv.asSymbol().getSymbol();
132+
}
133+
134+
if (nv.isJavaScript()) {
135+
BsonJavaScript v = nv.asJavaScript();
136+
if (v == null) {
137+
return null;
138+
}
139+
140+
Map<Object, Object> m = new LinkedHashMap<>();
141+
m.put("type", BsonType.JAVASCRIPT);
142+
m.put("code", v.getCode());
143+
return m;
144+
}
145+
146+
if (nv.isJavaScriptWithScope()) {
147+
BsonJavaScriptWithScope v = nv.asJavaScriptWithScope();
148+
if (v == null) {
149+
return null;
150+
}
151+
152+
Map<Object, Object> m = new LinkedHashMap<>();
153+
m.put("type", BsonType.JAVASCRIPT);
154+
m.put("scope", getValue(v.getScope()));
155+
m.put("code", v.getCode());
156+
return m;
157+
}
158+
}
159+
160+
return value;
161+
}
162+
}

0 commit comments

Comments
 (0)