Skip to content

Commit aee3d3f

Browse files
committed
Java:新增数据库连接池 Druid 的 Demo,名为 APIJSONDemo-Druid
1 parent e4d40a0 commit aee3d3f

13 files changed

+475
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding//src/test/java=UTF-8
5+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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-druid</artifactId>
8+
<version>4.7.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>APIJSONDemo-Druid</name>
12+
<description>Demo project for Testing APIJSON Server based on SpringBoot</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- 需要的 APIJSON 相关依赖 -->
22+
<dependency>
23+
<groupId>com.github.APIJSON</groupId>
24+
<artifactId>apijson-framework</artifactId>
25+
<version>LATEST</version>
26+
</dependency>
27+
28+
<!-- 需要用的数据库 JDBC 驱动 -->
29+
<dependency>
30+
<groupId>mysql</groupId>
31+
<artifactId>mysql-connector-java</artifactId>
32+
<version>8.0.22</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.postgresql</groupId>
36+
<artifactId>postgresql</artifactId>
37+
<version>42.2.18</version>
38+
</dependency>
39+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
40+
41+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
<version>2.4.2</version>
46+
</dependency>
47+
48+
<!-- 需要用的 Druid 数据库连接池库,1.0.29 以上 -->
49+
<dependency>
50+
<groupId>com.alibaba</groupId>
51+
<artifactId>druid</artifactId>
52+
<version>1.0.29</version>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
<configuration>
63+
<fork>true</fork>
64+
<mainClass>apijson.demo.DemoApplication</mainClass>
65+
</configuration>
66+
<executions>
67+
<execution>
68+
<goals>
69+
<goal>repackage</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<configuration>
78+
<source>1.8</source>
79+
<target>1.8</target>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
<repositories>
86+
<repository>
87+
<id>spring-snapshots</id>
88+
<url>https://repo.spring.io/snapshot</url>
89+
<snapshots>
90+
<enabled>true</enabled>
91+
</snapshots>
92+
</repository>
93+
<repository>
94+
<id>spring-milestones</id>
95+
<url>https://repo.spring.io/milestone</url>
96+
</repository>
97+
<repository>
98+
<id>jitpack.io</id>
99+
<url>https://jitpack.io</url>
100+
</repository>
101+
</repositories>
102+
103+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.beans.BeansException;
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.ApplicationContextAware;
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+
/**SpringBootApplication
37+
* 右键这个类 > Run As > Java Application
38+
* @author Lemon
39+
*/
40+
@Configuration
41+
@SpringBootApplication
42+
@EnableAutoConfiguration
43+
@EnableConfigurationProperties
44+
public class DemoApplication implements ApplicationContextAware {
45+
46+
static {
47+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() {
48+
@Override
49+
public SQLConfig createSQLConfig() {
50+
return new DemoSQLConfig();
51+
}
52+
@Override
53+
public SQLExecutor createSQLExecutor() {
54+
return new DemoSQLExecutor();
55+
}
56+
};
57+
58+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
59+
// try { //加载驱动程序
60+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
61+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
62+
// Log.d(TAG, "成功加载 SQLServer 驱动��>>>>>>>>>>>>>>>>>>>>> ");
63+
// }
64+
// catch (ClassNotFoundException e) {
65+
// e.printStackTrace();
66+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
67+
// }
68+
//
69+
// try { //加载驱动程序
70+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
71+
// Class.forName("oracle.jdbc.driver.OracleDriver");
72+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
73+
// }
74+
// catch (ClassNotFoundException e) {
75+
// e.printStackTrace();
76+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
77+
// }
78+
79+
}
80+
81+
public static void main(String[] args) throws Exception {
82+
SpringApplication.run(DemoApplication.class, args);
83+
84+
Log.DEBUG = true;
85+
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
86+
}
87+
88+
// 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
89+
private static ApplicationContext APPLICATION_CONTEXT;
90+
public static ApplicationContext getApplicationContext() {
91+
return APPLICATION_CONTEXT;
92+
}
93+
@Override
94+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
95+
APPLICATION_CONTEXT = applicationContext;
96+
}
97+
98+
99+
// 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100+
101+
@Bean
102+
public WebMvcConfigurer corsConfigurer() {
103+
return new WebMvcConfigurer() {
104+
@Override
105+
public void addCorsMappings(CorsRegistry registry) {
106+
registry.addMapping("/**")
107+
.allowedOriginPatterns("*")
108+
.allowedMethods("*")
109+
.allowCredentials(true)
110+
.maxAge(3600);
111+
}
112+
};
113+
}
114+
115+
// 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
116+
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
19+
import javax.servlet.http.HttpSession;
20+
21+
import org.springframework.web.bind.annotation.PathVariable;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RequestBody;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
import apijson.RequestMethod;
28+
import apijson.StringUtil;
29+
import apijson.framework.APIJSONController;
30+
import apijson.orm.Parser;
31+
32+
33+
/**提供入口,转交给 APIJSON 的 Parser 来处理
34+
* @author Lemon
35+
*/
36+
@RestController
37+
@RequestMapping("")
38+
public class DemoController extends APIJSONController {
39+
40+
@Override
41+
public Parser<Long> newParser(HttpSession session, RequestMethod method) {
42+
return super.newParser(session, method).setNeedVerify(false); //TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
43+
}
44+
45+
@PostMapping(value = "get")
46+
@Override
47+
public String get(@RequestBody String request, HttpSession session) {
48+
return super.get(request, session);
49+
}
50+
51+
/**获取
52+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
53+
* @param request 只用String,避免encode后未decode
54+
* @param session
55+
* @return
56+
* @see {@link RequestMethod#GET}
57+
*/
58+
@RequestMapping("get/{request}")
59+
public String openGet(@PathVariable String request, HttpSession session) {
60+
try {
61+
request = URLDecoder.decode(request, StringUtil.UTF_8);
62+
} catch (Exception e) {
63+
// Parser会报错
64+
}
65+
return get(request, session);
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.context.properties.ConfigurationProperties;
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
21+
import com.alibaba.druid.pool.DruidDataSource;
22+
23+
24+
/**Druid 连接池配置,对应 application.yml 的 datasource 配置
25+
* @author Lemon
26+
*/
27+
@Configuration
28+
public class DemoDruidConfig {
29+
30+
@Bean
31+
@ConfigurationProperties(prefix = "spring.datasource.druid")
32+
public DruidDataSource dataSource(){
33+
return new DruidDataSource();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)