265 lines
6.7 KiB
Markdown
265 lines
6.7 KiB
Markdown
|
|
# 通用基础设施开发文档 - Part 3(Redis + MyBatis-Plus + IdGenerator + pom.xml)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 一、RedisConfig.java
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package com.openclaw.config;
|
|||
|
|
|
|||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|||
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|||
|
|
import org.springframework.context.annotation.Bean;
|
|||
|
|
import org.springframework.context.annotation.Configuration;
|
|||
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|||
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|||
|
|
import org.springframework.data.redis.serializer.*;
|
|||
|
|
|
|||
|
|
@Configuration
|
|||
|
|
public class RedisConfig {
|
|||
|
|
|
|||
|
|
@Bean
|
|||
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
|||
|
|
RedisTemplate<String, Object> tpl = new RedisTemplate<>();
|
|||
|
|
tpl.setConnectionFactory(factory);
|
|||
|
|
|
|||
|
|
ObjectMapper om = new ObjectMapper();
|
|||
|
|
om.registerModule(new JavaTimeModule());
|
|||
|
|
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|||
|
|
om.activateDefaultTyping(
|
|||
|
|
om.getPolymorphicTypeValidator(),
|
|||
|
|
ObjectMapper.DefaultTyping.NON_FINAL);
|
|||
|
|
|
|||
|
|
Jackson2JsonRedisSerializer<Object> json =
|
|||
|
|
new Jackson2JsonRedisSerializer<>(om, Object.class);
|
|||
|
|
|
|||
|
|
StringRedisSerializer str = new StringRedisSerializer();
|
|||
|
|
tpl.setKeySerializer(str);
|
|||
|
|
tpl.setHashKeySerializer(str);
|
|||
|
|
tpl.setValueSerializer(json);
|
|||
|
|
tpl.setHashValueSerializer(json);
|
|||
|
|
tpl.afterPropertiesSet();
|
|||
|
|
return tpl;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
# application.yml
|
|||
|
|
spring:
|
|||
|
|
data:
|
|||
|
|
redis:
|
|||
|
|
host: localhost
|
|||
|
|
port: 6379
|
|||
|
|
database: 0
|
|||
|
|
timeout: 3000ms
|
|||
|
|
lettuce:
|
|||
|
|
pool:
|
|||
|
|
max-active: 20
|
|||
|
|
max-idle: 10
|
|||
|
|
min-idle: 2
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 二、MybatisPlusConfig.java
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package com.openclaw.config;
|
|||
|
|
|
|||
|
|
import com.baomidou.mybatisplus.annotation.DbType;
|
|||
|
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|||
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
|||
|
|
import org.springframework.context.annotation.Bean;
|
|||
|
|
import org.springframework.context.annotation.Configuration;
|
|||
|
|
|
|||
|
|
@Configuration
|
|||
|
|
public class MybatisPlusConfig {
|
|||
|
|
|
|||
|
|
@Bean
|
|||
|
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
|||
|
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
|||
|
|
// 分页插件
|
|||
|
|
interceptor.addInnerInterceptor(
|
|||
|
|
new PaginationInnerInterceptor(DbType.MYSQL));
|
|||
|
|
return interceptor;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 三、IdGenerator.java
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package com.openclaw.util;
|
|||
|
|
|
|||
|
|
import org.springframework.stereotype.Component;
|
|||
|
|
import java.time.LocalDateTime;
|
|||
|
|
import java.time.format.DateTimeFormatter;
|
|||
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 业务单号生成器(无分布式要求,单机递增序列即可)。
|
|||
|
|
* 格式示例:
|
|||
|
|
* 订单号 ORD20260316143022000001
|
|||
|
|
* 退款号 REF20260316143022000001
|
|||
|
|
* 充值号 RCH20260316143022000001
|
|||
|
|
*/
|
|||
|
|
@Component
|
|||
|
|
public class IdGenerator {
|
|||
|
|
|
|||
|
|
private static final DateTimeFormatter FMT =
|
|||
|
|
DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|||
|
|
|
|||
|
|
private final AtomicInteger seq = new AtomicInteger(0);
|
|||
|
|
|
|||
|
|
private String next(String prefix) {
|
|||
|
|
int s = seq.incrementAndGet() % 1_000_000;
|
|||
|
|
return prefix + LocalDateTime.now().format(FMT) + String.format("%06d", s);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String generateOrderNo() { return next("ORD"); }
|
|||
|
|
public String generateRefundNo() { return next("REF"); }
|
|||
|
|
public String generateRechargeNo(){ return next("RCH"); }
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 四、核心 pom.xml 依赖
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<dependencies>
|
|||
|
|
<!-- Web -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.springframework.boot</groupId>
|
|||
|
|
<artifactId>spring-boot-starter-web</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- Security -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.springframework.boot</groupId>
|
|||
|
|
<artifactId>spring-boot-starter-security</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- MyBatis Plus -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>com.baomidou</groupId>
|
|||
|
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
|||
|
|
<version>3.5.7</version>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- MySQL -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>com.mysql</groupId>
|
|||
|
|
<artifactId>mysql-connector-j</artifactId>
|
|||
|
|
<scope>runtime</scope>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- Redis -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.springframework.boot</groupId>
|
|||
|
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.apache.commons</groupId>
|
|||
|
|
<artifactId>commons-pool2</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- JWT -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>io.jsonwebtoken</groupId>
|
|||
|
|
<artifactId>jjwt-api</artifactId>
|
|||
|
|
<version>0.11.5</version>
|
|||
|
|
</dependency>
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>io.jsonwebtoken</groupId>
|
|||
|
|
<artifactId>jjwt-impl</artifactId>
|
|||
|
|
<version>0.11.5</version>
|
|||
|
|
<scope>runtime</scope>
|
|||
|
|
</dependency>
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>io.jsonwebtoken</groupId>
|
|||
|
|
<artifactId>jjwt-jackson</artifactId>
|
|||
|
|
<version>0.11.5</version>
|
|||
|
|
<scope>runtime</scope>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- Validation -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.springframework.boot</groupId>
|
|||
|
|
<artifactId>spring-boot-starter-validation</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- Lombok -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>org.projectlombok</groupId>
|
|||
|
|
<artifactId>lombok</artifactId>
|
|||
|
|
<scope>provided</scope>
|
|||
|
|
</dependency>
|
|||
|
|
|
|||
|
|
<!-- Jackson Java Time -->
|
|||
|
|
<dependency>
|
|||
|
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
|||
|
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
|||
|
|
</dependency>
|
|||
|
|
</dependencies>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 五、application.yml 完整示例
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
server:
|
|||
|
|
port: 8080
|
|||
|
|
|
|||
|
|
spring:
|
|||
|
|
datasource:
|
|||
|
|
url: jdbc:mysql://localhost:3306/openclaw?useSSL=false&serverTimezone=Asia/Shanghai
|
|||
|
|
username: root
|
|||
|
|
password: your_password
|
|||
|
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
|||
|
|
data:
|
|||
|
|
redis:
|
|||
|
|
host: localhost
|
|||
|
|
port: 6379
|
|||
|
|
|
|||
|
|
mybatis-plus:
|
|||
|
|
configuration:
|
|||
|
|
map-underscore-to-camel-case: true
|
|||
|
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
|||
|
|
global-config:
|
|||
|
|
db-config:
|
|||
|
|
logic-delete-field: deleted
|
|||
|
|
logic-delete-value: 1
|
|||
|
|
logic-not-delete-value: 0
|
|||
|
|
|
|||
|
|
jwt:
|
|||
|
|
secret: change-this-to-a-256-bit-random-secret
|
|||
|
|
expire-ms: 86400000
|
|||
|
|
|
|||
|
|
invite:
|
|||
|
|
inviter-points: 50
|
|||
|
|
invitee-points: 30
|
|||
|
|
url-prefix: https://app.openclaw.com/invite/
|
|||
|
|
|
|||
|
|
recharge:
|
|||
|
|
tiers:
|
|||
|
|
- amount: 10
|
|||
|
|
bonusPoints: 10
|
|||
|
|
- amount: 50
|
|||
|
|
bonusPoints: 60
|
|||
|
|
- amount: 100
|
|||
|
|
bonusPoints: 150
|
|||
|
|
- amount: 500
|
|||
|
|
bonusPoints: 800
|
|||
|
|
- amount: 1000
|
|||
|
|
bonusPoints: 2000
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
**文档版本**:v1.0 | **创建日期**:2026-03-16
|