|
|
|
|
@ -0,0 +1,73 @@
|
|
|
|
|
package cn.iocoder.yudao.framework.mybatis.config;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
|
import org.springframework.boot.env.EnvironmentPostProcessor;
|
|
|
|
|
import org.springframework.core.env.ConfigurableEnvironment;
|
|
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当 IdType 为 {@link IdType#NONE} 时,根据 PRIMARY 数据源所使用的数据库,自动设置
|
|
|
|
|
*
|
|
|
|
|
* @author 芋道源码
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor {
|
|
|
|
|
|
|
|
|
|
private static final String ID_TYPE_KEY = "mybatis-plus.global-config.db-config.id-type";
|
|
|
|
|
|
|
|
|
|
private static final String DATASOURCE_DYNAMIC_KEY = "spring.datasource.dynamic";
|
|
|
|
|
|
|
|
|
|
private static final Set<DbType> INPUT_ID_TYPES = SetUtils.asSet(DbType.ORACLE, DbType.ORACLE_12C,
|
|
|
|
|
DbType.POSTGRE_SQL, DbType.KINGBASE_ES, DbType.DB2, DbType.H2);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
|
|
|
|
// 如果非 NONE,则不进行处理
|
|
|
|
|
IdType idType = getIdType(environment);
|
|
|
|
|
if (idType != IdType.NONE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 如果获取不到 DbType,则不进行处理
|
|
|
|
|
DbType dbType = getDbType(environment);
|
|
|
|
|
if (dbType == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 情况一,用户输入 ID,适合 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库
|
|
|
|
|
if (INPUT_ID_TYPES.contains(dbType)) {
|
|
|
|
|
setIdType(environment, IdType.INPUT);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 情况二,自增 ID,适合 MySQL 等直接自增的数据库
|
|
|
|
|
setIdType(environment, IdType.AUTO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IdType getIdType(ConfigurableEnvironment environment) {
|
|
|
|
|
return environment.getProperty(ID_TYPE_KEY, IdType.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setIdType(ConfigurableEnvironment environment, IdType idType) {
|
|
|
|
|
environment.getSystemProperties().put(ID_TYPE_KEY, idType);
|
|
|
|
|
log.info("[setIdType][修改 MyBatis Plus 的 idType 为({})]", idType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DbType getDbType(ConfigurableEnvironment environment) {
|
|
|
|
|
String primary = environment.getProperty(DATASOURCE_DYNAMIC_KEY + "." + "primary");
|
|
|
|
|
if (StrUtil.isEmpty(primary)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
String url = environment.getProperty(DATASOURCE_DYNAMIC_KEY + ".datasource." + primary + ".url");
|
|
|
|
|
if (StrUtil.isEmpty(url)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return JdbcUtils.getDbType(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|