feat(config): 支持多数据库类型配置及动态分页方言

This commit is contained in:
Ray.Hao
2025-12-03 09:49:54 +08:00
parent 289f79cdb4
commit 5fa2e08aad
3 changed files with 56 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionIntercepto
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.youlai.boot.plugin.mybatis.MyDataPermissionHandler; import com.youlai.boot.plugin.mybatis.MyDataPermissionHandler;
import com.youlai.boot.plugin.mybatis.MyMetaObjectHandler; import com.youlai.boot.plugin.mybatis.MyMetaObjectHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -21,16 +22,27 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement @EnableTransactionManagement
public class MybatisConfig { public class MybatisConfig {
@Value("${app.db-type:mysql}")
private String dbType;
/** /**
* 分页插件和数据权限插件 * 分页插件和数据权限插件
*/ */
@Bean @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() { public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//数据权限 // 数据权限
interceptor.addInnerInterceptor(new DataPermissionInterceptor(new MyDataPermissionHandler())); interceptor.addInnerInterceptor(new DataPermissionInterceptor(new MyDataPermissionHandler()));
//分页插件 // 分页插件,根据配置动态选择数据库类型
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); DbType mpDbType = DbType.MYSQL;
String type = dbType == null ? "mysql" : dbType.toLowerCase();
if ("postgres".equals(type) || "postgresql".equals(type)) {
mpDbType = DbType.POSTGRE_SQL;
} else if ("dm".equals(type) || "dameng".equals(type)) {
// 达梦更接近 Oracle 语法,这里选择 ORACLE 方言以获得较好兼容性
mpDbType = DbType.ORACLE;
}
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(mpDbType));
return interceptor; return interceptor;
} }

View File

@@ -1,13 +1,30 @@
server: server:
port: 8989 port: 8989
# 数据库类型:用于 MyBatis-Plus 分页方言等(仅方言,不负责连接信息)
app:
db-type: mysql # 可选mysql | postgres | dm
spring: spring:
datasource: datasource:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
# === MySQL 数据源(默认启用) ===
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://www.youlai.tech:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true url: jdbc:mysql://www.youlai.tech:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: youlai username: youlai
password: 123456 password: 123456
# === PostgreSQL 数据源示例(按需启用) ===
# driver-class-name: org.postgresql.Driver
# url: jdbc:postgresql://127.0.0.1:5432/youlai_boot
# username: postgres
# password: 123456
# === 达梦 DM 数据源示例(按需启用,注意按实际驱动与 URL 调整) ===
# driver-class-name: dm.jdbc.driver.DmDriver
# url: jdbc:dm://127.0.0.1:5236?schema=YOULAI_BOOT
# username: SYSDBA
# password: 123456
data: data:
redis: redis:
database: 0 database: 0
@@ -74,6 +91,8 @@ spring:
response-format: response-format:
type: json_object type: json_object
mybatis-plus: mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml mapper-locations: classpath*:/mapper/**/*.xml
global-config: global-config:

View File

@@ -4,10 +4,23 @@ server:
spring: spring:
datasource: datasource:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
# === MySQL 数据源(默认启用) ===
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://www.youlai.tech:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true url: jdbc:mysql://www.youlai.tech:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: youlai username: youlai
password: 123456 password: 123456
# === PostgreSQL 数据源示例(按需启用) ===
# driver-class-name: org.postgresql.Driver
# url: jdbc:postgresql://127.0.0.1:5432/youlai_boot
# username: postgres
# password: 123456
# === 达梦 DM 数据源示例(按需启用,注意按实际驱动与 URL 调整) ===
# driver-class-name: dm.jdbc.driver.DmDriver
# url: jdbc:dm://127.0.0.1:5236?schema=YOULAI_BOOT
# username: SYSDBA
# password: 123456
data: data:
redis: redis:
database: 11 database: 11
@@ -95,19 +108,19 @@ mybatis-plus:
security: security:
session: session:
type: jwt # 会话方式 [jwt|redis-token] type: jwt # 会话方式 [jwt|redis-token]
access-token-time-to-live: 7200 # 访问令牌 有效期(单位:秒),默认 2 小时,-1 表示永不过期 access-token-time-to-live: 7200 # 访问令牌 有效期(单位:秒),默认 2 小时,-1 表示永不过期
refresh-token-time-to-live: 604800 # 刷新令牌有效期(单位:秒),默认 7 天,-1 表示永不过期 refresh-token-time-to-live: 604800 # 刷新令牌有效期(单位:秒),默认 7 天,-1 表示永不过期
jwt: jwt:
secret-key: SecretKey012345678901234567890123456789012345678901234567890123456789 # JWT密钥(HS256算法至少32字符) secret-key: SecretKey012345678901234567890123456789012345678901234567890123456789 # JWT密钥(HS256算法至少32字符)
redis-token: redis-token:
allow-multi-login: true # 是否允许多设备登录 allow-multi-login: true # 是否允许多设备登录
# 安全白名单路径,仅跳过 AuthorizationFilter 过滤器,还是会走 Spring Security 的其他过滤器(CSRF、CORS等) # 安全白名单路径,仅跳过 AuthorizationFilter 过滤器,还是会走 Spring Security 的其他过滤器(CSRF、CORS等)
ignore-urls: ignore-urls:
- /api/v1/auth/login/** # 登录接口(账号密码登录、手机验证码登录和微信登录) - /api/v1/auth/login/** # 登录接口(账号密码登录、手机验证码登录和微信登录)
- /api/v1/auth/captcha # 验证码获取接口 - /api/v1/auth/captcha # 验证码获取接口
- /api/v1/auth/refresh-token # 刷新令牌接口 - /api/v1/auth/refresh-token # 刷新令牌接口
- //api/v1/auth/wx/miniapp/code-login # 微信小程序code登陆 - //api/v1/auth/wx/miniapp/code-login # 微信小程序code登陆
- /ws/** # WebSocket接口 - /ws/** # WebSocket接口
# 非安全端点路径,完全绕过 Spring Security 的过滤器 # 非安全端点路径,完全绕过 Spring Security 的过滤器
unsecured-urls: unsecured-urls:
- ${springdoc.swagger-ui.path} - ${springdoc.swagger-ui.path}
@@ -173,7 +186,7 @@ springdoc:
api-docs: api-docs:
path: /v3/api-docs path: /v3/api-docs
group-configs: group-configs:
- group: '系统管理' - group: "系统管理"
paths-to-match: "/**" paths-to-match: "/**"
packages-to-scan: packages-to-scan:
- com.youlai.boot.auth.controller - com.youlai.boot.auth.controller
@@ -185,9 +198,9 @@ springdoc:
# knife4j 接口文档配置 # knife4j 接口文档配置
knife4j: knife4j:
# 是否开启 Knife4j 增强功能 # 是否开启 Knife4j 增强功能
enable: true # 设置为 true 表示开启增强功能 enable: true # 设置为 true 表示开启增强功能
# 生产环境配置 # 生产环境配置
production: false # 设置为 true 表示在生产环境中不显示文档,为 false 表示显示文档(通常在开发环境中使用) production: false # 设置为 true 表示在生产环境中不显示文档,为 false 表示显示文档(通常在开发环境中使用)
setting: setting:
language: zh_cn language: zh_cn
@@ -243,7 +256,6 @@ wx:
app-id: xxxxxx app-id: xxxxxx
app-secret: xxxxxx app-secret: xxxxxx
# ==================== AI 命令系统配置 ==================== # ==================== AI 命令系统配置 ====================
ai: ai:
# 是否启用 AI 功能 # 是否启用 AI 功能