Skip to content

Commit 78815e8

Browse files
author
stonexwang
committed
add jdbc demo
1 parent dee7606 commit 78815e8

File tree

19 files changed

+1538
-0
lines changed

19 files changed

+1538
-0
lines changed

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
.idea
26+
*.iml
27+
target/
28+
build.sh

apijson-extend/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>apijson-practice</artifactId>
7+
<groupId>com.stone</groupId>
8+
<version>1.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>apijson-extend</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
<java.version>1.8</java.version>
19+
<maven.compiler.source>8</maven.compiler.source>
20+
<maven.compiler.target>8</maven.compiler.target>
21+
</properties>
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.github.Tencent</groupId>
25+
<artifactId>APIJSON</artifactId>
26+
<version>4.7.2</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.stone.apijson.extend;
2+
3+
import apijson.MethodAccess;
4+
import apijson.orm.AbstractVerifier;
5+
import com.alibaba.fastjson.JSONObject;
6+
7+
import java.util.Comparator;
8+
import java.util.SortedMap;
9+
import java.util.TreeMap;
10+
11+
public class ApiJsonAnnotationUtil {
12+
/**
13+
* 动态扫描MethodAccess注解,添加到规则库,避免数据库操作
14+
*
15+
* @param clz
16+
*/
17+
public static void fillAccessMap(Class clz) {
18+
if (AbstractVerifier.ACCESS_MAP.containsKey(clz.getSimpleName())) {
19+
// 以数据库优先
20+
} else {
21+
// 通过注解设置访问控制
22+
AbstractVerifier.ACCESS_MAP.put(clz.getSimpleName(), AbstractVerifier.getAccessMap((MethodAccess) clz.getAnnotation(MethodAccess.class)));
23+
}
24+
}
25+
26+
/**
27+
* 动态扫描RequestStructure注解,添加到规则库,避免数据库操作
28+
*
29+
* @param clz
30+
*/
31+
public static void fillRequestMap(Class clz) {
32+
RequestStructures rss = (RequestStructures) clz.getAnnotation(RequestStructures.class);
33+
if (rss != null) {
34+
RequestStructure[] requestStructures = rss.value();
35+
for (RequestStructure rs : requestStructures) {
36+
fillRequestMap(rs);
37+
}
38+
} else {
39+
RequestStructure rs = (RequestStructure) clz.getAnnotation(RequestStructure.class);
40+
if (rs != null) {
41+
fillRequestMap(rs);
42+
}
43+
}
44+
}
45+
46+
public static void fillRequestMap(RequestStructure rs) {
47+
JSONObject item;
48+
item = new apijson.JSONObject();
49+
item.put("tag", rs.tag());
50+
item.put("detail", rs.detail());
51+
item.put("structure", rs.structure());
52+
item.put("method", rs.method().name());
53+
item.put("version", rs.version());
54+
String cacheKey = AbstractVerifier.getCacheKeyForRequest(rs.method().name(), rs.tag());
55+
if (!AbstractVerifier.REQUEST_MAP.containsKey(cacheKey)) {
56+
//数据库优先,数据库不存在了再考虑动态扫描
57+
SortedMap<Integer, JSONObject> versionedMap = AbstractVerifier.REQUEST_MAP.get(cacheKey);
58+
if (versionedMap == null) {
59+
versionedMap = new TreeMap<>(new Comparator<Integer>() {
60+
@Override
61+
public int compare(Integer o1, Integer o2) {
62+
return o2 == null ? -1 : o2.compareTo(o1); // 降序
63+
}
64+
});
65+
}
66+
versionedMap.put(rs.version(), item);
67+
AbstractVerifier.REQUEST_MAP.put(cacheKey, versionedMap);
68+
}
69+
}
70+
71+
72+
}
73+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
2+
3+
This source code is licensed under the Apache License Version 2.0.*/
4+
5+
6+
package com.stone.apijson.extend;
7+
8+
import apijson.RequestMethod;
9+
10+
import java.lang.annotation.*;
11+
12+
import static java.lang.annotation.ElementType.TYPE;
13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
14+
15+
/**通过注解实现对请求的设置,通过代码实现,避免了对request的操作
16+
* @author Lemon
17+
*/
18+
@Documented
19+
@Retention(RUNTIME)
20+
@Target(TYPE)
21+
@Inherited
22+
@Repeatable(RequestStructures.class)
23+
public @interface RequestStructure {
24+
RequestMethod method() default RequestMethod.GET;
25+
String tag() default "";
26+
String structure() default "{}";
27+
String detail() default "";
28+
int version() default 1;
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
2+
3+
This source code is licensed under the Apache License Version 2.0.*/
4+
5+
6+
package com.stone.apijson.extend;
7+
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.Inherited;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.Target;
12+
13+
import static java.lang.annotation.ElementType.TYPE;
14+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
15+
16+
/**实现多重注解
17+
* @author Lemon
18+
*/
19+
@Documented
20+
@Retention(RUNTIME)
21+
@Target(TYPE)
22+
@Inherited
23+
public @interface RequestStructures {
24+
RequestStructure[] value();
25+
}

apijson-jdbc-demo/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>apijson-practice</artifactId>
7+
<groupId>com.stone</groupId>
8+
<version>1.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>apijson-jdbc-demo</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
<java.version>1.8</java.version>
19+
<maven.compiler.source>8</maven.compiler.source>
20+
<maven.compiler.target>8</maven.compiler.target>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.stone</groupId>
26+
<artifactId>apijson-extend</artifactId>
27+
<version>1.0</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.github.APIJSON</groupId>
31+
<artifactId>apijson-framework</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
<version>2.4.2</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.github.TommyLemon</groupId>
41+
<artifactId>unitauto-java</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.github.TommyLemon</groupId>
45+
<artifactId>unitauto-jar</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.github.classgraph</groupId>
49+
<artifactId>classgraph</artifactId>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 com.stone.apijson.demo.jdbc;
16+
17+
import apijson.framework.APIJSONApplication;
18+
import apijson.framework.APIJSONCreator;
19+
import apijson.orm.FunctionParser;
20+
import apijson.orm.SQLConfig;
21+
import com.stone.apijson.demo.jdbc.config.DemoFunctionParser;
22+
import com.stone.apijson.demo.jdbc.config.DemoSQLConfig;
23+
import com.stone.apijson.demo.jdbc.model.MomentComment;
24+
import com.stone.apijson.extend.ApiJsonAnnotationUtil;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.autoconfigure.SpringBootApplication;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
30+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
31+
32+
33+
/**
34+
* SpringBootApplication
35+
* 右键这个类 > Run As > Java Application
36+
*
37+
* @author Lemon
38+
*/
39+
@Configuration
40+
@SpringBootApplication
41+
public class DemoApplication {
42+
43+
static {
44+
45+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() {
46+
@Override
47+
public SQLConfig createSQLConfig() {
48+
return new DemoSQLConfig();
49+
}
50+
51+
@Override
52+
public FunctionParser createFunctionParser() {
53+
return new DemoFunctionParser();
54+
}
55+
};
56+
57+
}
58+
59+
public static void main(String[] args) throws Exception {
60+
SpringApplication.run(DemoApplication.class, args);
61+
62+
// Log.DEBUG = false;
63+
// APIJSONParser.IS_PRINT_BIG_LOG = false;
64+
65+
APIJSONApplication.init(true); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
66+
ApiJsonAnnotationUtil.fillAccessMap(MomentComment.class);
67+
ApiJsonAnnotationUtil.fillRequestMap(MomentComment.class);
68+
}
69+
70+
71+
// 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
72+
73+
@Bean
74+
public WebMvcConfigurer corsConfigurer() {
75+
return new WebMvcConfigurer() {
76+
@Override
77+
public void addCorsMappings(CorsRegistry registry) {
78+
registry.addMapping("/**")
79+
.allowedOriginPatterns("*")
80+
.allowedMethods("*")
81+
.allowCredentials(true)
82+
.maxAge(3600);
83+
}
84+
};
85+
}
86+
87+
// 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
88+
89+
}

0 commit comments

Comments
 (0)