Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e4d40a0

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

File tree

11 files changed

+460
-0
lines changed

11 files changed

+460
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ APIJSON-Java-Server/APIJSONDemo/.settings
1010
APIJSON-Java-Server/APIJSONDemo/.idea
1111

1212
*.iml
13+
APIJSON-Java-Server/APIJSONDemo-HikariCP/.settings
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
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: 103 additions & 0 deletions
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-hikaricp</artifactId>
8+
<version>4.7.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>APIJSONDemo</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+
<!-- 需要用的 HikariCp 数据库连接池库,3.1.0 以上 -->
49+
<dependency>
50+
<groupId>com.zaxxer</groupId>
51+
<artifactId>HikariCP</artifactId>
52+
<version>3.1.0</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>
Lines changed: 117 additions & 0 deletions
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+
}
Lines changed: 68 additions & 0 deletions
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/**HikariCP 连接池配置,对应 application.yml 的 datasource 配置
26+
* @author Lemon
27+
*/
28+
@Configuration
29+
public class DemoDataSourceConfig {
30+
31+
@Bean
32+
@ConfigurationProperties(prefix = "spring.datasource.hikari")
33+
public DataSource dataSource(){
34+
return new HikariDataSource();
35+
}
36+
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 apijson.framework.APIJSONSQLConfig;
18+
19+
20+
/**SQL配置,这里不配置数据库账号密码等信息,改为使用 DemoDataSourceConfig 来配置
21+
* TiDB 用法和 MySQL 一致
22+
* @author Lemon
23+
*/
24+
public class DemoSQLConfig extends APIJSONSQLConfig {
25+
26+
static {
27+
DEFAULT_DATABASE = DATABASE_MYSQL; // TODO 默认数据库类型,改成你自己的
28+
DEFAULT_SCHEMA = "sys"; // TODO 默认模式名,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: public, SQL Server: dbo, Oracle:
29+
30+
// 表名和数据库不一致的,需要配置映射关系。只使用 APIJSONORM 时才需要;
31+
// 如果用了 apijson-framework 且调用了 APIJSONApplication.init
32+
// (间接调用 DemoVerifier.init 方法读取数据库 Access 表来替代手动输入配置),则不需要。
33+
// 但如果 Access 这张表的对外表名与数据库实际表名不一致,仍然需要这里注册。例如
34+
// TABLE_KEY_MAP.put(Access.class.getSimpleName(), "access");
35+
36+
//表名映射,隐藏真实表名,对安全要求很高的表可以这么做
37+
TABLE_KEY_MAP.put("User", "apijson_user");
38+
TABLE_KEY_MAP.put("Privacy", "apijson_privacy");
39+
}
40+
41+
@Override
42+
public String getDBVersion() {
43+
return "5.7.22"; // "8.0.11"; // TODO 改成你自己的 MySQL 或 PostgreSQL 数据库版本号 // MYSQL 8 和 7 使用的 JDBC 配置不一样
44+
}
45+
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /