【7.0.2】【config】更新config获取系统配置判断数据库类型

pull/5/MERGE
fengshuonan 2021-03-27 21:30:35 +08:00
parent 44dcfcdd50
commit 7177d92a17
6 changed files with 216 additions and 8 deletions

View File

@ -0,0 +1,46 @@
package cn.stylefeng.roses.kernel.rule.enums;
import lombok.Getter;
/**
*
* <p>
* mapping.xml
*
* @author stylefeng
* @date 2020/6/20 21:08
*/
@Getter
public enum DbTypeEnum {
/**
* mysql
*/
MYSQL("mysql", "mysql"),
/**
* pgsql
*/
PG_SQL("pgsql", "postgresql"),
/**
* oracle
*/
ORACLE("oracle", "oracle"),
/**
* mssql
*/
MS_SQL("mssql", "sqlserver");
private final String code;
private final String name;
DbTypeEnum(String code, String name) {
this.code = code;
this.name = name;
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.util;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
/**
*
*
* @author fengshuonan
* @date 2021/3/27 21:24
*/
public class DatabaseTypeUtil {
/**
*
*
* @author fengshuonan
* @date 2021/3/27 21:25
*/
public static DbTypeEnum getDbType(String jdbcUrl) {
if (StrUtil.isEmpty(jdbcUrl)) {
return DbTypeEnum.MYSQL;
}
if (jdbcUrl.contains(DbTypeEnum.ORACLE.getName())) {
return DbTypeEnum.ORACLE;
} else if (jdbcUrl.contains(DbTypeEnum.PG_SQL.getName())) {
return DbTypeEnum.PG_SQL;
} else if (jdbcUrl.contains(DbTypeEnum.MS_SQL.getName())) {
return DbTypeEnum.MS_SQL;
} else {
return DbTypeEnum.MYSQL;
}
}
}

View File

@ -0,0 +1,35 @@
package cn.stylefeng.roses.kernel.config.api;
import cn.hutool.db.Entity;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* api
*
* @author fengshuonan
* @date 2021/3/27 21:15
*/
public interface SysConfigDataApi {
/**
*
*
* @param conn
* @return list
* @author fengshuonan
* @date 2021/3/27 21:15
*/
List<Entity> getConfigs(Connection conn) throws SQLException;
/**
* listsql
*
* @author fengshuonan
* @date 2021/3/27 21:19
*/
String getConfigListSql();
}

View File

@ -0,0 +1,34 @@
package cn.stylefeng.roses.kernel.config.modular.data;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.stylefeng.roses.kernel.config.api.SysConfigDataApi;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* Mysql
*
* @author fengshuonan
* @date 2021/3/27 21:18
*/
@Slf4j
public class MysqlSysConfigData implements SysConfigDataApi {
@Override
public List<Entity> getConfigs(Connection conn) throws SQLException {
return SqlExecutor.query(conn, getConfigListSql(), new EntityListHandler(), StatusEnum.ENABLE.getCode(), YesOrNotEnum.N.getCode());
}
@Override
public String getConfigListSql() {
return "select config_code, config_value from sys_config where status_flag = ? and del_flag = ?";
}
}

View File

@ -0,0 +1,36 @@
package cn.stylefeng.roses.kernel.config.modular.factory;
import cn.stylefeng.roses.kernel.config.api.SysConfigDataApi;
import cn.stylefeng.roses.kernel.config.modular.data.MysqlSysConfigData;
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
import cn.stylefeng.roses.kernel.rule.util.DatabaseTypeUtil;
/**
* SysConfigDataApi
*
* @author fengshuonan
* @date 2021/3/27 21:27
*/
public class SysConfigDataFactory {
/**
* jdbc urlapi
*
* @author fengshuonan
* @date 2021/3/27 21:27
*/
public static SysConfigDataApi getSysConfigDataApi(String jdbcUrl) {
DbTypeEnum dbType = DatabaseTypeUtil.getDbType(jdbcUrl);
if (DbTypeEnum.MYSQL.equals(dbType)) {
return new MysqlSysConfigData();
} else if (DbTypeEnum.PG_SQL.equals(dbType)) {
// todo
} else if (DbTypeEnum.MS_SQL.equals(dbType)) {
// todo
} else if (DbTypeEnum.ORACLE.equals(dbType)) {
// todo
}
return new MysqlSysConfigData();
}
}

View File

@ -27,14 +27,11 @@ package cn.stylefeng.roses.kernel.config.modular.listener;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.stylefeng.roses.kernel.config.ConfigContainer;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.config.modular.factory.SysConfigDataFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
@ -62,8 +59,6 @@ import static cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExcepti
@Slf4j
public class ConfigInitListener implements ApplicationListener<ApplicationContextInitializedEvent>, Ordered {
private static final String CONFIG_LIST_SQL = "select config_code, config_value from sys_config where status_flag = ? and del_flag = ?";
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 100;
@ -87,6 +82,7 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
String dataSourceUrl = environment.getProperty("spring.datasource.url");
String dataSourceUsername = environment.getProperty("spring.datasource.username");
String dataSourcePassword = environment.getProperty("spring.datasource.password");
String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
// 如果有为空的配置,终止执行
if (ObjectUtil.hasEmpty(dataSourceUrl, dataSourceUsername, dataSourcePassword)) {
@ -95,12 +91,12 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName(driverClassName);
assert dataSourceUrl != null;
conn = DriverManager.getConnection(dataSourceUrl, dataSourceUsername, dataSourcePassword);
// 获取sys_config表的数据
List<Entity> entityList = SqlExecutor.query(conn, CONFIG_LIST_SQL, new EntityListHandler(), StatusEnum.ENABLE.getCode(), YesOrNotEnum.N.getCode());
List<Entity> entityList = SysConfigDataFactory.getSysConfigDataApi(dataSourceUrl).getConfigs(conn);
// 将查询到的参数配置添加到缓存
if (ObjectUtil.isNotEmpty(entityList)) {
@ -117,4 +113,5 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
}
}
}