Skip to content

Commit 792acb7

Browse files
committed
Java:完善 ShardingSphere Demo 的代码、配置、数据,并升级 JDK 17,APIJSON 7.1.0, apijson-framework 7.1.5
1 parent 43f412d commit 792acb7

22 files changed

+649
-421
lines changed

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_1_single/ds_1_Comment_0.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_1_single/ds_1_Comment_1.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_1_single/ds_1_Moment_0.sql

+57
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_1_single/ds_1_Moment_1.sql

+57
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_1_single/ds_1_apijson_user.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_2_single/ds_2_Comment_0.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_2_single/ds_2_Comment_1.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/MySQL/ds_2_single/ds_2_apijson_user.sql

+59
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/pom.xml

+72-13
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@
55

66
<groupId>apijson.demo</groupId>
77
<artifactId>apijson-demo</artifactId>
8-
<version>7.0.3</version>
8+
<version>7.1.5</version>
99

1010
<name>APIJSONDemo-SharingSphere</name>
1111
<description>Demo project for testing APIJSON server based on SpringBoot</description>
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16-
<java.version>1.8</java.version>
16+
<java.version>17</java.version>
17+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
18+
<maven.compiler.source>17</maven.compiler.source>
19+
<maven.compiler.target>17</maven.compiler.target>
1720
</properties>
1821

1922
<dependencies>
2023
<!-- 需要的 APIJSON 相关依赖 -->
2124
<dependency>
2225
<groupId>com.github.Tencent</groupId>
2326
<artifactId>APIJSON</artifactId>
24-
<version>7.0.3</version>
27+
<version>7.1.0</version>
2528
</dependency>
2629
<dependency>
2730
<groupId>com.github.APIJSON</groupId>
2831
<artifactId>apijson-framework</artifactId>
29-
<version>7.0.3</version>
32+
<version>7.1.5</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>jakarta.servlet</groupId>
37+
<artifactId>jakarta.servlet-api</artifactId>
38+
<version>6.0.0</version>
3039
</dependency>
3140

3241
<!-- 需要用的数据库 JDBC 驱动 -->
@@ -43,18 +52,68 @@
4352
<version>3.1.0</version>
4453
</dependency>
4554

46-
<dependency>
47-
<groupId>org.apache.shardingsphere</groupId>
48-
<artifactId>shardingsphere-jdbc-core</artifactId>
49-
<version>5.2.1</version>
50-
</dependency>
55+
<dependency>
56+
<groupId>org.apache.shardingsphere</groupId>
57+
<artifactId>shardingsphere-jdbc-core</artifactId>
58+
<version>5.4.1</version>
59+
</dependency>
5160
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
5261

62+
<dependency>
63+
<groupId>org.yaml</groupId>
64+
<artifactId>snakeyaml</artifactId>
65+
<version>1.33</version>
66+
</dependency>
67+
68+
<!-- <dependency>-->
69+
<!-- <groupId>org.slf4j</groupId>-->
70+
<!-- <artifactId>slf4j-api</artifactId>-->
71+
<!-- <version>2.0.13</version>-->
72+
<!-- </dependency>-->
73+
<!-- <dependency>-->
74+
<!-- <groupId>org.slf4j</groupId>-->
75+
<!-- <artifactId>slf4j-log4j12</artifactId>-->
76+
<!-- <version>2.0.13</version>-->
77+
<!-- </dependency>-->
78+
<!-- logback -->
79+
<!-- <dependency>-->
80+
<!-- <groupId>ch.qos.logback</groupId>-->
81+
<!-- <artifactId>logback-classic</artifactId>-->
82+
<!-- <version>1.5.6</version>-->
83+
<!-- </dependency>-->
84+
<!-- <dependency>-->
85+
<!-- <groupId>ch.qos.logback</groupId>-->
86+
<!-- <artifactId>logback-core</artifactId>-->
87+
<!-- <version>1.5.6</version>-->
88+
<!-- </dependency>-->
89+
5390
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
5491
<dependency>
5592
<groupId>org.springframework.boot</groupId>
5693
<artifactId>spring-boot-starter-web</artifactId>
57-
<version>2.5.13</version>
94+
<version>2.7.0</version>
95+
<exclusions>
96+
<!-- <exclusion>-->
97+
<!-- <groupId>org.slf4j</groupId>-->
98+
<!-- <artifactId>slf4j-api</artifactId>-->
99+
<!-- </exclusion>-->
100+
<!-- <exclusion>-->
101+
<!-- <groupId>org.slf4j</groupId>-->
102+
<!-- <artifactId>slf4j-log4j12</artifactId>-->
103+
<!-- </exclusion>-->
104+
<!-- <exclusion>-->
105+
<!-- <groupId>ch.qos.logback</groupId>-->
106+
<!-- <artifactId>logback-classic</artifactId>-->
107+
<!-- </exclusion>-->
108+
<!-- <exclusion>-->
109+
<!-- <groupId>ch.qos.logback</groupId>-->
110+
<!-- <artifactId>logback-core</artifactId>-->
111+
<!-- </exclusion>-->
112+
<!-- <exclusion>-->
113+
<!-- <groupId>org.slf4j</groupId>-->
114+
<!-- <artifactId>slf4j-simple</artifactId>-->
115+
<!-- </exclusion>-->
116+
</exclusions>
58117
</dependency>
59118

60119
</dependencies>
@@ -64,7 +123,7 @@
64123
<plugin>
65124
<groupId>org.springframework.boot</groupId>
66125
<artifactId>spring-boot-maven-plugin</artifactId>
67-
<version>2.5.13</version>
126+
<version>3.2.5</version>
68127
<executions>
69128
<execution>
70129
<goals>
@@ -78,8 +137,8 @@
78137
<artifactId>maven-compiler-plugin</artifactId>
79138
<version>3.8.1</version>
80139
<configuration>
81-
<source>1.8</source>
82-
<target>1.8</target>
140+
<source>17</source>
141+
<target>17</target>
83142
</configuration>
84143
</plugin>
85144
</plugins>

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/src/main/java/apijson/demo/DemoApplication.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2828

2929
import apijson.Log;
30-
import apijson.framework.APIJSONApplication;
31-
import apijson.framework.APIJSONCreator;
30+
import apijson.framework.javax.APIJSONApplication;
31+
import apijson.framework.javax.APIJSONCreator;
3232
import apijson.orm.SQLConfig;
3333
import apijson.orm.SQLExecutor;
3434

@@ -68,7 +68,7 @@ public void customize(ConfigurableServletWebServerFactory server) {
6868

6969
// 支持 APIAuto 中 JavaScript 代码跨域请求
7070
@Bean
71-
public WebMvcConfigurer corsConfigurer() {
71+
public WebMvcConfigurer corsConfig() {
7272
return new WebMvcConfigurer() {
7373
@Override
7474
public void addCorsMappings(CorsRegistry registry) {
@@ -85,12 +85,12 @@ public void addCorsMappings(CorsRegistry registry) {
8585
// 使用本项目的自定义处理类
8686
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
8787
@Override
88-
public SQLConfig createSQLConfig() {
88+
public SQLConfig<Long> createSQLConfig() {
8989
return new DemoSQLConfig();
9090
}
9191

9292
@Override
93-
public SQLExecutor createSQLExecutor() {
93+
public SQLExecutor<Long> createSQLExecutor() {
9494
return new DemoSQLExecutor();
9595
}
9696
};

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/src/main/java/apijson/demo/DemoController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import apijson.RequestMethod;
3131
import apijson.StringUtil;
32-
import apijson.framework.APIJSONController;
32+
import apijson.framework.javax.APIJSONController;
3333
import apijson.orm.Parser;
3434

3535

Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
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 com.zaxxer.hikari.HikariDataSource;
18-
import org.springframework.boot.context.properties.ConfigurationProperties;
19-
import org.springframework.context.annotation.Bean;
20-
import org.springframework.context.annotation.Configuration;
21-
22-
import javax.sql.DataSource;
23-
24-
25-
/**数据源配置,对应 application.yml 的数据库连接池 datasource 配置。
26-
* 也可以直接 new 再 set 属性,具体见 HikariCP 的文档
27-
* https://github.com/brettwooldridge/HikariCP#rocket-initialization
28-
* @author Lemon
29-
*/
30-
@Configuration
31-
public class DemoDataSourceConfig {
32-
33-
@Bean
34-
@ConfigurationProperties(prefix = "spring.datasource.hikari")
35-
public DataSource dataSource() {
36-
return new HikariDataSource();
37-
}
38-
39-
@Bean
40-
@ConfigurationProperties(prefix = "spring.datasource.ds-0")
41-
public DataSource dataSourceDS0() {
42-
return new HikariDataSource();
43-
}
44-
45-
@Bean
46-
@ConfigurationProperties(prefix = "spring.datasource.ds-1")
47-
public DataSource dataSourceDS1() {
48-
return new HikariDataSource();
49-
}
50-
51-
}
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 com.zaxxer.hikari.HikariDataSource;
18+
//import org.springframework.boot.context.properties.ConfigurationProperties;
19+
//import org.springframework.context.annotation.Bean;
20+
//import org.springframework.context.annotation.Configuration;
21+
//
22+
//import javax.sql.DataSource;
23+
//
24+
//
25+
///**数据源配置,对应 application.yml 的数据库连接池 datasource 配置。
26+
// * 也可以直接 new 再 set 属性,具体见 HikariCP 的文档
27+
// * https://github.com/brettwooldridge/HikariCP#rocket-initialization
28+
// * @author Lemon
29+
// */
30+
//@Configuration
31+
//public class DemoDataSourceConfig {
32+
//
33+
// @Bean
34+
// @ConfigurationProperties(prefix = "spring.datasource.hikari")
35+
// public DataSource dataSource() {
36+
// return new HikariDataSource();
37+
// }
38+
//
39+
// @Bean
40+
// @ConfigurationProperties(prefix = "spring.datasource.ds-0")
41+
// public DataSource dataSourceDS0() {
42+
// return new HikariDataSource();
43+
// }
44+
//
45+
// @Bean
46+
// @ConfigurationProperties(prefix = "spring.datasource.ds-1")
47+
// public DataSource dataSourceDS1() {
48+
// return new HikariDataSource();
49+
// }
50+
//
51+
//}

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/src/main/java/apijson/demo/DemoSQLConfig.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import com.alibaba.fastjson.annotation.JSONField;
1818

19-
import apijson.framework.APIJSONSQLConfig;
19+
import apijson.framework.javax.APIJSONSQLConfig;
2020

2121

2222
/**SQL 配置
@@ -50,7 +50,10 @@ public String getDBVersion() {
5050
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息
5151
@Override
5252
public String getDBUri() {
53-
return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
53+
if (isConfigTable()) {
54+
return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
55+
}
56+
return "jdbc:shardingsphere:classpath:config.yaml"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
5457
}
5558

5659
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息
@@ -65,4 +68,8 @@ public String getDBPassword() {
6568
return "apijson"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用, 默认密码为空字符串 ""
6669
}
6770

71+
@Override
72+
public String getSQLSchema() {
73+
return isConfigTable() ? super.getSQLSchema() : null;
74+
}
6875
}

APIJSON-Java-Server/APIJSONDemo-ShardingSphere/src/main/java/apijson/demo/DemoSQLExecutor.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
package apijson.demo;
1616

1717
import java.sql.Connection;
18+
import java.sql.ResultSet;
19+
import java.sql.Statement;
1820
import java.util.Map;
1921

2022
import javax.sql.DataSource;
2123

22-
import apijson.framework.APIJSONSQLExecutor;
24+
import apijson.framework.javax.APIJSONSQLExecutor;
2325
import apijson.orm.SQLConfig;
2426

2527

@@ -32,21 +34,30 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
3234

3335
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
3436
@Override
35-
public Connection getConnection(SQLConfig config) throws Exception {
37+
public Connection getConnection(SQLConfig<Long> config) throws Exception {
3638
// Log.d(TAG, "getConnection config.getDatasource() = " + config.getDatasource());
3739

38-
String key = config.getDatasource() + "-" + config.getDatabase();
39-
Connection c = connectionMap.get(key);
40-
if (c == null || c.isClosed()) {
41-
Map<String, DataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DataSource.class);
42-
DataSource ds = dsMap == null ? null : dsMap.get("dataSourceDS0");
43-
// 另一种方式是 DemoDataSourceConfig 初始化获取到 Datasource 后给静态变量 DATA_SOURCE 赋值: ds = DemoDataSourceConfig.DATA_SOURCE.getConnection();
44-
connectionMap.put(key, ds == null ? null : ds.getConnection());
45-
}
40+
//String key = config.getDatasource() + "-" + config.getDatabase();
41+
//Connection c = connectionMap.get(key);
42+
//if (c == null || c.isClosed()) {
43+
// Map<String, DataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DataSource.class);
44+
// DataSource ds = dsMap == null ? null : dsMap.get("dataSourceDS0");
45+
// // 另一种方式是 DemoDataSourceConfig 初始化获取到 Datasource 后给静态变量 DATA_SOURCE 赋值: ds = DemoDataSourceConfig.DATA_SOURCE.getConnection();
46+
// connectionMap.put(key, ds == null ? null : ds.getConnection());
47+
//}
4648

4749
// 必须最后执行 super 方法,因为里面还有事务相关处理。
4850
// 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表
4951
return super.getConnection(config);
5052
}
5153

54+
@Override
55+
public ResultSet executeQuery(SQLConfig<Long> config, String sql) throws Exception {
56+
return super.executeQuery(config, sql);
57+
}
58+
59+
@Override
60+
public ResultSet executeQuery(Statement statement, String sql) throws Exception {
61+
return super.executeQuery(statement, sql);
62+
}
5263
}

0 commit comments

Comments
 (0)