mirror of https://gitee.com/stylefeng/roses
【7.0.2】【config】更新config获取系统配置判断数据库类型
parent
44dcfcdd50
commit
7177d92a17
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
||||
*
|
||||
* Guns采用APACHE 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* 获取所有配置list的sql
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/3/27 21:19
|
||||
*/
|
||||
String getConfigListSql();
|
||||
|
||||
}
|
|
@ -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 = ?";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 url获取api
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue