Skip to content

Commit 87c983f

Browse files
committed
Java:新增 APIJSONDemo-ShardingSphere
1 parent d283f1c commit 87c983f

16 files changed

+872
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ APIJSON-Java-Server/APIJSONDemo-BigData/target
2323
APIJSON-Java-Server/APIJSONDemo-Presto/.idea
2424
APIJSON-Java-Server/APIJSONDemo-Presto/target
2525
APIJSON-Java-Server/APIJSONBoot-BigData/.idea
26+
APIJSON-Java-Server/APIJSONDemo-ShardingSphere/.idea
27+
APIJSON-Java-Server/APIJSONDemo-ShardingSphere/target

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public String getDBPassword() {
211211
return "apijson"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用, 默认密码为空字符串 ""
212212
}
213213
if (isElasticsearch()) {
214-
return "ObqNhN8vZEM9qRi3AXErLIC5"; // TODO 改成你自己的
214+
return null; // TODO 改成你自己的
215215
}
216216
if (isPresto()) {
217217
return null; // TODO 改成你自己的
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# APIJSONDemo
2+
3+
APIJSON + SpringBoot 初级使用的最简单 Demo
4+
5+
### 运行
6+
7+
右键 DemoApplication > Run As > Java Application
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apijson.demo</groupId>
7+
<artifactId>apijson-demo</artifactId>
8+
<version>5.4.0</version>
9+
10+
<name>APIJSONDemo-SharingSphere</name>
11+
<description>Demo project for testing APIJSON server based on SpringBoot</description>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<java.version>1.8</java.version>
17+
</properties>
18+
19+
<dependencies>
20+
<!-- JDK 11+ 需要,否则启动报错 NoClassDefFoundError: javax/activation/UnsupportedDataTypeException -->
21+
<dependency>
22+
<groupId>javax.activation</groupId>
23+
<artifactId>activation</artifactId>
24+
<version>1.1.1</version>
25+
</dependency>
26+
27+
<!-- 需要的 APIJSON 相关依赖 -->
28+
<dependency>
29+
<groupId>com.github.Tencent</groupId>
30+
<artifactId>APIJSON</artifactId>
31+
<version>5.3.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.github.APIJSON</groupId>
35+
<artifactId>apijson-framework</artifactId>
36+
<version>5.3.0</version>
37+
<exclusions>
38+
<exclusion>
39+
<groupId>com.github.Tecent</groupId>
40+
<artifactId>APIJSON</artifactId>
41+
</exclusion>
42+
</exclusions>
43+
</dependency>
44+
45+
<!-- 需要用的数据库 JDBC 驱动 -->
46+
<dependency>
47+
<groupId>mysql</groupId>
48+
<artifactId>mysql-connector-java</artifactId>
49+
<version>8.0.29</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.xerial</groupId>
54+
<artifactId>sqlite-jdbc</artifactId>
55+
<version>3.39.3.0</version>
56+
</dependency>
57+
58+
<!-- 需要用的 HikariCp 数据库连接池库,3.1.0 以上 -->
59+
<dependency>
60+
<groupId>com.zaxxer</groupId>
61+
<artifactId>HikariCP</artifactId>
62+
<version>3.1.0</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.apache.shardingsphere</groupId>
67+
<artifactId>shardingsphere-jdbc-core</artifactId>
68+
<version>5.2.1</version>
69+
</dependency>
70+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
71+
72+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-web</artifactId>
76+
<version>2.5.13</version>
77+
</dependency>
78+
79+
</dependencies>
80+
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-maven-plugin</artifactId>
86+
<configuration>
87+
<fork>true</fork>
88+
<mainClass>apijson.demo.DemoApplication</mainClass>
89+
</configuration>
90+
<executions>
91+
<execution>
92+
<goals>
93+
<goal>repackage</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-compiler-plugin</artifactId>
101+
<configuration>
102+
<source>1.8</source>
103+
<target>1.8</target>
104+
</configuration>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
109+
<repositories>
110+
<!-- APIJSON 必须用到的托管平台 -->
111+
<repository>
112+
<id>jitpack.io</id>
113+
<url>https://jitpack.io</url>
114+
<snapshots>
115+
<enabled>true</enabled>
116+
</snapshots>
117+
</repository>
118+
119+
<repository>
120+
<id>spring-snapshots</id>
121+
<url>https://repo.spring.io/snapshot</url>
122+
<snapshots>
123+
<enabled>true</enabled>
124+
</snapshots>
125+
</repository>
126+
<repository>
127+
<id>spring-milestones</id>
128+
<url>https://repo.spring.io/milestone</url>
129+
</repository>
130+
</repositories>
131+
132+
</project>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/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.demo;
16+
17+
import org.springframework.boot.SpringApplication;
18+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
22+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
27+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
28+
29+
import apijson.Log;
30+
import apijson.framework.APIJSONApplication;
31+
import apijson.framework.APIJSONCreator;
32+
import apijson.orm.SQLConfig;
33+
import apijson.orm.SQLExecutor;
34+
35+
36+
/**
37+
* Demo SpringBoot Application 主应用程序启动类
38+
* 右键这个类 > Run As > Java Application
39+
* 具体见 SpringBoot 文档
40+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
41+
*
42+
* @author Lemon
43+
*/
44+
@Configuration
45+
@SpringBootApplication
46+
@EnableAutoConfiguration
47+
@EnableConfigurationProperties
48+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
49+
public static final String TAG = "DemoApplication";
50+
51+
// 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
52+
private static ApplicationContext APPLICATION_CONTEXT;
53+
public static ApplicationContext getApplicationContext() {
54+
return APPLICATION_CONTEXT;
55+
}
56+
57+
public static void main(String[] args) throws Exception {
58+
APPLICATION_CONTEXT = SpringApplication.run(DemoApplication.class, args);
59+
60+
Log.DEBUG = true;
61+
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
62+
}
63+
64+
// SpringBoot 2.x 自定义端口方式
65+
@Override
66+
public void customize(ConfigurableServletWebServerFactory server) {
67+
server.setPort(8080);
68+
}
69+
70+
// 支持 APIAuto 中 JavaScript 代码跨域请求
71+
@Bean
72+
public WebMvcConfigurer corsConfigurer() {
73+
return new WebMvcConfigurer() {
74+
@Override
75+
public void addCorsMappings(CorsRegistry registry) {
76+
registry.addMapping("/**")
77+
.allowedOriginPatterns("*")
78+
.allowedMethods("*")
79+
.allowCredentials(true)
80+
.maxAge(3600);
81+
}
82+
};
83+
}
84+
85+
static {
86+
// 使用本项目的自定义处理类
87+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
88+
@Override
89+
public SQLConfig createSQLConfig() {
90+
return new DemoSQLConfig();
91+
}
92+
93+
@Override
94+
public SQLExecutor createSQLExecutor() {
95+
return new DemoSQLExecutor();
96+
}
97+
};
98+
99+
try { //加载驱动程序
100+
Log.d(TAG, "尝试加载 SQLite 驱动 <<<<<<<<<<<<<<<<<<<<< ");
101+
Class.forName("org.sqlite.SQLiteJDBCLoader");
102+
Log.d(TAG, "成功加载 SQLite 驱动!>>>>>>>>>>>>>>>>>>>>> ");
103+
}
104+
catch (ClassNotFoundException e) {
105+
e.printStackTrace();
106+
Log.e(TAG, "加载 SQLite 驱动失败,请检查 pom.xml 中 sqlite-jdbc 版本是否存在以及可用 !!!");
107+
}
108+
109+
try { //加载驱动程序
110+
Log.d(TAG, "尝试加载 ShardingSphere 驱动 <<<<<<<<<<<<<<<<<<<<< ");
111+
Class.forName("org.apache.shardingsphere.driver.ShardingSphereDriver");
112+
Log.d(TAG, "成功加载 ShardingSphere 驱动!>>>>>>>>>>>>>>>>>>>>> ");
113+
}
114+
catch (ClassNotFoundException e) {
115+
e.printStackTrace();
116+
Log.e(TAG, "加载 ShardingSphere 驱动失败,请检查 pom.xml 中 ShardingSphereDriver 版本是否存在以及可用 !!!");
117+
}
118+
119+
}
120+
121+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/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.demo;
16+
17+
import org.springframework.web.bind.annotation.GetMapping;
18+
import org.springframework.web.bind.annotation.PathVariable;
19+
import org.springframework.web.bind.annotation.PostMapping;
20+
import org.springframework.web.bind.annotation.RequestBody;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
import java.net.URLDecoder;
26+
import java.util.Map;
27+
28+
import javax.servlet.http.HttpSession;
29+
30+
import apijson.RequestMethod;
31+
import apijson.StringUtil;
32+
import apijson.framework.APIJSONController;
33+
import apijson.orm.Parser;
34+
35+
36+
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
37+
* 具体见 SpringBoot 文档
38+
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
39+
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
40+
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
41+
* <br > 建议全通过HTTP POST来请求:
42+
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
43+
* <br > 2.提高性能 - 无需URL encode和decode
44+
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
45+
* @author Lemon
46+
*/
47+
@RestController
48+
@RequestMapping("")
49+
public class DemoController extends APIJSONController<Long> {
50+
51+
@Override
52+
public Parser<Long> newParser(HttpSession session, RequestMethod method) {
53+
return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
54+
}
55+
56+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
57+
* @param method
58+
* @param request
59+
* @param session
60+
* @return
61+
*/
62+
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
63+
@Override
64+
public String crud(@PathVariable String method, @RequestBody String request, HttpSession session) {
65+
return super.crud(method, request, session);
66+
}
67+
68+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
69+
* @param method
70+
* @param tag
71+
* @param params
72+
* @param request
73+
* @param session
74+
* @return
75+
*/
76+
@PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
77+
@Override
78+
public String crudByTag(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
79+
return super.crudByTag(method, tag, params, request, session);
80+
}
81+
82+
/**获取
83+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
84+
* @param request 只用String,避免encode后未decode
85+
* @param session
86+
* @return
87+
* @see {@link RequestMethod#GET}
88+
*/
89+
@GetMapping("get/{request}")
90+
public String openGet(@PathVariable String request, HttpSession session) {
91+
try {
92+
request = URLDecoder.decode(request, StringUtil.UTF_8);
93+
} catch (Exception e) {
94+
// Parser 会报错
95+
}
96+
return get(request, session);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)