Skip to content

Commit 00adec8

Browse files
committed
Java:Redis 当缓存用的 Demo 完善配置说明、升级 JDK 17,APIJSON 7.1.0, apijson-framework 及自身 7.1.5
1 parent a99e6db commit 00adec8

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55

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

1010
<name>APIJSONDemo-Redis</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>
3033
</dependency>
3134

3235
<!-- 需要用的数据库 JDBC 驱动 -->
@@ -88,8 +91,8 @@
8891
<artifactId>maven-compiler-plugin</artifactId>
8992
<version>3.8.1</version>
9093
<configuration>
91-
<source>1.8</source>
92-
<target>1.8</target>
94+
<source>17</source>
95+
<target>17</target>
9396
</configuration>
9497
</plugin>
9598
</plugins>

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

+9-9
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

3434

@@ -60,15 +60,15 @@ public void customize(ConfigurableServletWebServerFactory server) {
6060

6161
// 支持 APIAuto 中 JavaScript 代码跨域请求
6262
@Bean
63-
public WebMvcConfigurer corsConfigurer() {
63+
public WebMvcConfigurer corsConfig() {
6464
return new WebMvcConfigurer() {
6565
@Override
6666
public void addCorsMappings(CorsRegistry registry) {
6767
registry.addMapping("/**")
68-
.allowedOriginPatterns("*")
69-
.allowedMethods("*")
70-
.allowCredentials(true)
71-
.maxAge(3600);
68+
.allowedOriginPatterns("*")
69+
.allowedMethods("*")
70+
.allowCredentials(true)
71+
.maxAge(3600);
7272
}
7373
};
7474
}
@@ -77,12 +77,12 @@ public void addCorsMappings(CorsRegistry registry) {
7777
// 使用本项目的自定义处理类
7878
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
7979
@Override
80-
public SQLConfig createSQLConfig() {
80+
public SQLConfig<Long> createSQLConfig() {
8181
return new DemoSQLConfig();
8282
}
8383

8484
@Override
85-
public SQLExecutor createSQLExecutor() {
85+
public SQLExecutor<Long> createSQLExecutor() {
8686
return new DemoSQLExecutor();
8787
}
8888
};

APIJSON-Java-Server/APIJSONDemo-Redis/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

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

+1-1
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 配置

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import java.util.List;
3131
import java.util.concurrent.TimeUnit;
3232

33-
import static apijson.framework.APIJSONConstant.PRIVACY_;
34-
import static apijson.framework.APIJSONConstant.USER_;
33+
import static apijson.framework.javax.APIJSONConstant.PRIVACY_;
34+
import static apijson.framework.javax.APIJSONConstant.USER_;
3535

3636

3737
/**
@@ -46,8 +46,13 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
4646
// Redis 缓存 <<<<<<<<<<<<<<<<<<<<<<<
4747
public static final RedisTemplate<String, String> REDIS_TEMPLATE;
4848
static {
49+
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("127.0.0.1", 6379); // TODO 改成你自己的
50+
// 如果需要账号及密码则按以下方式设置
51+
// config.setUsername("root"); // TODO 改成你自己的
52+
// config.setPassword("apijson"); // TODO 改成你自己的
53+
4954
REDIS_TEMPLATE = new RedisTemplate<>();
50-
REDIS_TEMPLATE.setConnectionFactory(new JedisConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379)));
55+
REDIS_TEMPLATE.setConnectionFactory(new JedisConnectionFactory(config));
5156
REDIS_TEMPLATE.setKeySerializer(new StringRedisSerializer());
5257
REDIS_TEMPLATE.setHashValueSerializer(new GenericToStringSerializer<>(Serializable.class));
5358
REDIS_TEMPLATE.setValueSerializer(new GenericToStringSerializer<>(Serializable.class));
@@ -57,7 +62,7 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
5762

5863
// 可重写以下方法,支持 Redis 等单机全局缓存或分布式缓存
5964
@Override
60-
public List<JSONObject> getCache(String sql, SQLConfig config) {
65+
public List<JSONObject> getCache(String sql, SQLConfig<Long> config) {
6166
List<JSONObject> list = super.getCache(sql, config);
6267
if (list == null) {
6368
list = JSON.parseArray(REDIS_TEMPLATE.opsForValue().get(sql), JSONObject.class);
@@ -66,7 +71,7 @@ public List<JSONObject> getCache(String sql, SQLConfig config) {
6671
}
6772

6873
@Override
69-
public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig config) {
74+
public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig<Long> config) {
7075
super.putCache(sql, list, config);
7176

7277
String table = config != null && config.isMain() ? config.getTable() : null;
@@ -80,7 +85,7 @@ public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig c
8085
}
8186

8287
@Override
83-
public synchronized void removeCache(String sql, SQLConfig config) {
88+
public synchronized void removeCache(String sql, SQLConfig<Long> config) {
8489
super.removeCache(sql, config);
8590
if (config.getMethod() == RequestMethod.DELETE) { // 避免缓存击穿
8691
REDIS_TEMPLATE.expire(sql, 60, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)