Skip to content

Commit ce94fc1

Browse files
apijson执行sql语句
Tencent/APIJSON#510
1 parent f5d9b5b commit ce94fc1

File tree

10 files changed

+709
-0
lines changed

10 files changed

+709
-0
lines changed
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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>apijson.demo</groupId>
6+
<artifactId>apijson-demo-druid-rawSQL</artifactId>
7+
<version>6.0.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>APIJSONDemo-Druid</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+
<!-- 需要的 APIJSON 相关依赖 -->
21+
<dependency>
22+
<groupId>com.github.Tencent</groupId>
23+
<artifactId>APIJSON</artifactId>
24+
<version>6.0.0</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.github.APIJSON</groupId>
28+
<artifactId>apijson-framework</artifactId>
29+
<version>6.0.0</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.github.APIJSON</groupId>
33+
<artifactId>apijson-router</artifactId>
34+
<version>1.5.1</version>
35+
</dependency>
36+
<!-- 需要用的数据库 JDBC 驱动 -->
37+
<dependency>
38+
<groupId>mysql</groupId>
39+
<artifactId>mysql-connector-java</artifactId>
40+
<version>8.0.29</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.postgresql</groupId>
44+
<artifactId>postgresql</artifactId>
45+
<version>42.3.4</version>
46+
</dependency>
47+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
48+
49+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-web</artifactId>
53+
<version>2.5.13</version>
54+
</dependency>
55+
56+
<!-- 需要用的 Druid 数据库连接池库,1.0.29 以上 -->
57+
<dependency>
58+
<groupId>com.alibaba</groupId>
59+
<artifactId>druid</artifactId>
60+
<version>1.2.9</version>
61+
</dependency>
62+
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
<configuration>
71+
<fork>true</fork>
72+
<mainClass>apijson.demo.DemoApplication</mainClass>
73+
</configuration>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>repackage</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<configuration>
86+
<source>1.8</source>
87+
<target>1.8</target>
88+
</configuration>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
<repositories>
94+
<!-- APIJSON 必须用到的托管平台 -->
95+
<repository>
96+
<id>jitpack.io</id>
97+
<url>https://jitpack.io</url>
98+
<snapshots>
99+
<enabled>true</enabled>
100+
</snapshots>
101+
</repository>
102+
103+
<repository>
104+
<id>spring-snapshots</id>
105+
<url>https://repo.spring.io/snapshot</url>
106+
<snapshots>
107+
<enabled>true</enabled>
108+
</snapshots>
109+
</repository>
110+
<repository>
111+
<id>spring-milestones</id>
112+
<url>https://repo.spring.io/milestone</url>
113+
</repository>
114+
</repositories>
115+
116+
</project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.SpringBootApplication;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
20+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
21+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.http.MediaType;
26+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
27+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
28+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
29+
30+
import apijson.Log;
31+
import apijson.framework.APIJSONApplication;
32+
import apijson.framework.APIJSONCreator;
33+
import apijson.orm.FunctionParser;
34+
import apijson.orm.SQLConfig;
35+
import apijson.orm.SQLExecutor;
36+
import apijson.router.APIJSONRouterApplication;
37+
38+
/**
39+
* Demo SpringBoot Application 主应用程序启动类 右键这个类 > Run As > Java Application 具体见
40+
* SpringBoot 文档
41+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
42+
*
43+
* @author Lemon
44+
*/
45+
@Configuration
46+
@SpringBootApplication
47+
@EnableConfigurationProperties
48+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
49+
50+
// 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
51+
private static ApplicationContext APPLICATION_CONTEXT;
52+
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(true); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
62+
APIJSONRouterApplication.init();
63+
}
64+
65+
// SpringBoot 2.x 自定义端口方式
66+
@Override
67+
public void customize(ConfigurableServletWebServerFactory server) {
68+
server.setPort(8080);
69+
}
70+
71+
// 支持 APIAuto 中 JavaScript 代码跨域请求
72+
@Bean
73+
public WebMvcConfigurer corsConfigurer() {
74+
return new WebMvcConfigurer() {
75+
@Override
76+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
77+
// 设定各个接口默认返回 application/json,以便 Postman 展示
78+
configurer.defaultContentType(MediaType.APPLICATION_JSON);
79+
}
80+
81+
@Override
82+
public void addCorsMappings(CorsRegistry registry) {
83+
registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("*").allowCredentials(true).maxAge(3600);
84+
}
85+
};
86+
}
87+
88+
static {
89+
// 使用本项目的自定义处理类
90+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
91+
@Override
92+
public SQLConfig createSQLConfig() {
93+
return new DemoSQLConfig();
94+
}
95+
96+
@Override
97+
public SQLExecutor createSQLExecutor() {
98+
return new DemoSQLExecutor();
99+
}
100+
101+
@Override
102+
public FunctionParser createFunctionParser() {
103+
return new DemoFunctionParser();
104+
}
105+
};
106+
107+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
108+
// try { //加载驱动程序
109+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
110+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
111+
// Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
112+
// }
113+
// catch (ClassNotFoundException e) {
114+
// e.printStackTrace();
115+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用
116+
// !!!");
117+
// }
118+
//
119+
// try { //加载驱动程序
120+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
121+
// Class.forName("oracle.jdbc.driver.OracleDriver");
122+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
123+
// }
124+
// catch (ClassNotFoundException e) {
125+
// e.printStackTrace();
126+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
127+
// }
128+
129+
}
130+
131+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 java.net.URLDecoder;
18+
import java.util.Map;
19+
20+
import javax.servlet.http.HttpSession;
21+
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.PathVariable;
24+
import org.springframework.web.bind.annotation.PostMapping;
25+
import org.springframework.web.bind.annotation.RequestBody;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RequestParam;
28+
import org.springframework.web.bind.annotation.RestController;
29+
30+
import apijson.RequestMethod;
31+
import apijson.StringUtil;
32+
import apijson.orm.Parser;
33+
import apijson.router.APIJSONRouterController;
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 APIJSONRouterController<String> {
50+
51+
@Override
52+
public Parser<String> 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+
@PostMapping("router/{method}/{tag}")
100+
@Override
101+
public String router(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
102+
return super.router(method, tag, params, request, session);
103+
}
104+
}

0 commit comments

Comments
 (0)