-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
stonexwang
committed
Sep 16, 2021
1 parent
368ad37
commit 8a8b195
Showing
18 changed files
with
1,551 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 目的 | ||
本demo主要演示多数据源的案例,apijson本身依赖的数据源也是单独配置的 | ||
另外,可以看到,完全用注解实现完整的能力,也就是说,适当改造apijson自己依赖的数据库是可以干掉的。 | ||
|
||
更基础的信息,见jdbc-demo下的文件 | ||
|
||
# 快速跑起来 | ||
1. 建立数据库,创建apijson必须以来的几张表 | ||
* 执行apijson.sql文中的sql语句(apijson依赖表) | ||
* 执行biz_tables.sql(业务依赖表) | ||
|
||
2. 使用postman导入文件multi-datasource.postman_collection.json | ||
|
||
3. 运行程序,测试postman中的各个用例 | ||
|
||
# 代码解读 | ||
1. AbstractSQLConfig定义了从request中获取@datasource,根据该datasource,在MultiDemoSQLConfig中切换不同的getSQLSchema | ||
2. MultiDemoSQLExecutor定义了如何根据request中获取@datasource,找到后端对应的数据源,从而获得数据库链接 | ||
3. MultiDemoDataSourceConfig利用spring的特性,从application.xml中诸如DataSource供使用 | ||
4. 通过在MultiDemoSQLConfig中设置别名,完成表的隐私化处理 TABLE_KEY_MAP.put(VipUser.class.getSimpleName(), "vip_user"); | ||
|
||
更多信息见jdbc-demo下的文件 | ||
|
||
|
||
TODO:增加函数校验的实例,今天不玩了 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>apijson-practice</artifactId> | ||
<groupId>com.stone</groupId> | ||
<version>1.0</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>apijson-multi-datasource-demo</artifactId> | ||
|
||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.stone</groupId> | ||
<artifactId>apijson-extend</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.APIJSON</groupId> | ||
<artifactId>apijson-framework</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>2.4.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.TommyLemon</groupId> | ||
<artifactId>unitauto-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.TommyLemon</groupId> | ||
<artifactId>unitauto-jar</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.github.classgraph</groupId> | ||
<artifactId>classgraph</artifactId> | ||
</dependency> | ||
<!-- 需要用的 Druid 数据库连接池库,1.0.29 以上 --> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.0.29</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
230 changes: 230 additions & 0 deletions
230
...src/main/java/com/stone/apijson/demo/multidatasource/apijson/MultiDemoFunctionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License.*/ | ||
|
||
package com.stone.apijson.demo.multidatasource.apijson; | ||
|
||
import apijson.*; | ||
import apijson.framework.APIJSONFunctionParser; | ||
import apijson.orm.JSONRequest; | ||
import com.alibaba.fastjson.JSONArray; | ||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
|
||
/** | ||
* 可远程调用的函数类 | ||
* | ||
* @author Lemon | ||
*/ | ||
public class MultiDemoFunctionParser extends APIJSONFunctionParser { | ||
public static final String TAG = "MultiDemoFunctionParser"; | ||
|
||
public MultiDemoFunctionParser() { | ||
this(null, null, 0, null, null); | ||
} | ||
|
||
public MultiDemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) { | ||
super(method, tag, version, request, session); | ||
} | ||
|
||
public Object echo(@NotNull JSONObject current, String name) { | ||
return name + " from echo"; | ||
} | ||
|
||
/** | ||
* @param current | ||
* @param idList | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception { | ||
Object obj = current.get(idList); | ||
if (obj == null) { | ||
return null; | ||
} | ||
|
||
if (obj instanceof Collection == false) { | ||
throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !"); | ||
} | ||
JSONArray array = (JSONArray) obj; | ||
if (array != null) { | ||
for (int i = 0; i < array.size(); i++) { | ||
if (array.get(i) instanceof Long == false && array.get(i) instanceof Integer == false) { | ||
throw new IllegalArgumentException(idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!"); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* @param current | ||
* @param urlList | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public Object verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception { | ||
Object obj = current.get(urlList); | ||
if (obj == null) { | ||
return null; | ||
} | ||
|
||
if (obj instanceof Collection == false) { | ||
throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !"); | ||
} | ||
JSONArray array = (JSONArray) obj; | ||
if (array != null) { | ||
for (int i = 0; i < array.size(); i++) { | ||
if (StringUtil.isUrl(array.getString(i)) == false) { | ||
throw new IllegalArgumentException(urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!"); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* @param current | ||
* @param momentId | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String momentId) throws Exception { | ||
long mid = current.getLongValue(momentId); | ||
if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
return 0; | ||
} | ||
|
||
JSONRequest request = new JSONRequest(); | ||
|
||
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
JSONRequest comment = new JSONRequest(); | ||
comment.put("momentId", mid); | ||
|
||
request.put("Comment", comment); | ||
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
|
||
JSONObject rp = new MultiDemoParser(RequestMethod.DELETE).setNeedVerify(false).parseResponse(request); | ||
|
||
JSONObject c = rp.getJSONObject("Comment"); | ||
return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT); | ||
} | ||
|
||
|
||
/** | ||
* 删除评论的子评论 | ||
* | ||
* @param current | ||
* @param toId | ||
* @return | ||
*/ | ||
public int deleteChildComment(@NotNull JSONObject current, @NotNull String toId) throws Exception { | ||
long tid = current.getLongValue(toId); | ||
if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
return 0; | ||
} | ||
|
||
//递归获取到全部子评论id | ||
|
||
JSONRequest request = new JSONRequest(); | ||
|
||
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
JSONRequest comment = new JSONRequest(); | ||
comment.put("id{}", getChildCommentIdList(tid)); | ||
|
||
request.put("Comment", comment); | ||
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
|
||
JSONObject rp = new MultiDemoParser(RequestMethod.DELETE).setNeedVerify(false).parseResponse(request); | ||
|
||
JSONObject c = rp.getJSONObject("Comment"); | ||
return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT); | ||
} | ||
|
||
|
||
private JSONArray getChildCommentIdList(long tid) { | ||
|
||
JSONArray arr = new JSONArray(); | ||
|
||
JSONRequest request = new JSONRequest(); | ||
|
||
//Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
JSONRequest idItem = new JSONRequest(); | ||
|
||
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
JSONRequest comment = new JSONRequest(); | ||
comment.put("toId", tid); | ||
comment.setColumn("id"); | ||
idItem.put("Comment", comment); | ||
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
|
||
request.putAll(idItem.toArray(0, 0, "Comment-id")); | ||
//Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
|
||
JSONObject rp = new MultiDemoParser().setNeedVerify(false).parseResponse(request); | ||
|
||
JSONArray a = rp.getJSONArray("Comment-id[]"); | ||
if (a != null) { | ||
arr.addAll(a); | ||
|
||
JSONArray a2; | ||
for (int i = 0; i < a.size(); i++) { | ||
|
||
a2 = getChildCommentIdList(a.getLongValue(i)); | ||
if (a2 != null) { | ||
arr.addAll(a2); | ||
} | ||
} | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
|
||
/** | ||
* TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()" | ||
* | ||
* @param current | ||
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃 | ||
* @throws Exception | ||
*/ | ||
public JSONArray getIdList(@NotNull JSONObject current) { | ||
return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710))); | ||
} | ||
|
||
|
||
/** | ||
* TODO 仅用来测试 "key-()":"verifyAccess()" | ||
* | ||
* @param current | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public Object verifyAccess(@NotNull JSONObject current) throws Exception { | ||
long userId = current.getLongValue(JSONRequest.KEY_USER_ID); | ||
RequestRole role = RequestRole.get(current.getString(JSONRequest.KEY_ROLE)); | ||
if (role == RequestRole.OWNER && userId != MultiDemoVerifier.getVisitorId(getSession())) { | ||
throw new IllegalAccessException("登录用户与角色OWNER不匹配!"); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...o/src/main/java/com/stone/apijson/demo/multidatasource/apijson/MultiDemoObjectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License.*/ | ||
|
||
package com.stone.apijson.demo.multidatasource.apijson; | ||
|
||
import apijson.NotNull; | ||
import apijson.RequestMethod; | ||
import apijson.framework.APIJSONObjectParser; | ||
import apijson.orm.Join; | ||
import apijson.orm.SQLConfig; | ||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import java.util.List; | ||
|
||
|
||
/**简化Parser,getObject和getArray(getArrayConfig)都能用 | ||
* @author Lemon | ||
*/ | ||
public class MultiDemoObjectParser extends APIJSONObjectParser { | ||
|
||
public MultiDemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig arrayConfig | ||
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception { | ||
super(session, request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable); | ||
} | ||
|
||
@Override | ||
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception { | ||
return MultiDemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.