提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.config; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.common.util.collection.SetUtils; |
|
5 |
import com.iailab.framework.mybatis.core.enums.SqlConstants; |
|
6 |
import com.iailab.framework.mybatis.core.util.JdbcUtils; |
|
7 |
import com.baomidou.mybatisplus.annotation.DbType; |
|
8 |
import com.baomidou.mybatisplus.annotation.IdType; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
import org.springframework.boot.SpringApplication; |
|
11 |
import org.springframework.boot.env.EnvironmentPostProcessor; |
|
12 |
import org.springframework.core.env.ConfigurableEnvironment; |
|
13 |
|
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
/** |
|
17 |
* 当 IdType 为 {@link IdType#NONE} 时,根据 PRIMARY 数据源所使用的数据库,自动设置 |
|
18 |
* |
|
19 |
* @author iailab |
|
20 |
*/ |
|
21 |
@Slf4j |
|
22 |
public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor { |
|
23 |
|
|
24 |
private static final String ID_TYPE_KEY = "mybatis-plus.global-config.db-config.id-type"; |
|
25 |
|
|
26 |
private static final String DATASOURCE_DYNAMIC_KEY = "spring.datasource.dynamic"; |
|
27 |
|
|
28 |
private static final String QUARTZ_JOB_STORE_DRIVER_KEY = "spring.quartz.properties.org.quartz.jobStore.driverDelegateClass"; |
|
29 |
|
|
30 |
private static final Set<DbType> INPUT_ID_TYPES = SetUtils.asSet(DbType.ORACLE, DbType.ORACLE_12C, |
|
31 |
DbType.POSTGRE_SQL, DbType.KINGBASE_ES, DbType.DB2, DbType.H2); |
|
32 |
|
|
33 |
@Override |
|
34 |
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
|
35 |
// 如果获取不到 DbType,则不进行处理 |
|
36 |
DbType dbType = getDbType(environment); |
|
37 |
if (dbType == null) { |
|
38 |
return; |
|
39 |
} |
|
40 |
|
|
41 |
// 设置 Quartz JobStore 对应的 Driver |
|
42 |
// TODO iailab:暂时没有找到特别合适的地方,先放在这里 |
|
43 |
setJobStoreDriverIfPresent(environment, dbType); |
|
44 |
// 初始化 SQL 静态变量 |
|
45 |
SqlConstants.init(dbType); |
|
46 |
// 如果非 NONE,则不进行处理 |
|
47 |
IdType idType = getIdType(environment); |
|
48 |
if (idType != IdType.NONE) { |
|
49 |
return; |
|
50 |
} |
|
51 |
// 情况一,用户输入 ID,适合 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库 |
|
52 |
if (INPUT_ID_TYPES.contains(dbType)) { |
|
53 |
setIdType(environment, IdType.INPUT); |
|
54 |
return; |
|
55 |
} |
|
56 |
// 情况二,自增 ID,适合 MySQL 等直接自增的数据库 |
|
57 |
setIdType(environment, IdType.AUTO); |
|
58 |
} |
|
59 |
|
|
60 |
public IdType getIdType(ConfigurableEnvironment environment) { |
|
61 |
return environment.getProperty(ID_TYPE_KEY, IdType.class); |
|
62 |
} |
|
63 |
|
|
64 |
public void setIdType(ConfigurableEnvironment environment, IdType idType) { |
|
65 |
environment.getSystemProperties().put(ID_TYPE_KEY, idType); |
|
66 |
log.info("[setIdType][修改 MyBatis Plus 的 idType 为({})]", idType); |
|
67 |
} |
|
68 |
|
|
69 |
public void setJobStoreDriverIfPresent(ConfigurableEnvironment environment, DbType dbType) { |
|
70 |
String driverClass = environment.getProperty(QUARTZ_JOB_STORE_DRIVER_KEY); |
|
71 |
if (StrUtil.isNotEmpty(driverClass)) { |
|
72 |
return; |
|
73 |
} |
|
74 |
// 根据 dbType 类型,获取对应的 driverClass |
|
75 |
switch (dbType) { |
|
76 |
case POSTGRE_SQL: |
|
77 |
driverClass = "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate"; |
|
78 |
break; |
|
79 |
case ORACLE: |
|
80 |
case ORACLE_12C: |
|
81 |
driverClass = "org.quartz.impl.jdbcjobstore.oracle.OracleDelegate"; |
|
82 |
break; |
|
83 |
case SQL_SERVER: |
|
84 |
case SQL_SERVER2005: |
|
85 |
driverClass = "org.quartz.impl.jdbcjobstore.MSSQLDelegate"; |
|
86 |
break; |
|
87 |
} |
|
88 |
// 设置 driverClass 变量 |
|
89 |
if (StrUtil.isNotEmpty(driverClass)) { |
|
90 |
environment.getSystemProperties().put(QUARTZ_JOB_STORE_DRIVER_KEY, driverClass); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
public static DbType getDbType(ConfigurableEnvironment environment) { |
|
95 |
String primary = environment.getProperty(DATASOURCE_DYNAMIC_KEY + "." + "primary"); |
|
96 |
if (StrUtil.isEmpty(primary)) { |
|
97 |
return null; |
|
98 |
} |
|
99 |
String url = environment.getProperty(DATASOURCE_DYNAMIC_KEY + ".datasource." + primary + ".url"); |
|
100 |
if (StrUtil.isEmpty(url)) { |
|
101 |
return null; |
|
102 |
} |
|
103 |
return JdbcUtils.getDbType(url); |
|
104 |
} |
|
105 |
|
|
106 |
} |