mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add config
This commit is contained in:
parent
d1e8b89bda
commit
8ce0ce7474
@ -46,7 +46,7 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
|
||||
/**
|
||||
* 是否大小写不敏感(默认大小写不敏感)
|
||||
*/
|
||||
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
|
||||
protected boolean caseInsensitive = true;
|
||||
protected DialectRunner runner;
|
||||
|
||||
// ------------------------------------------------------- Constructor start
|
||||
|
@ -13,83 +13,14 @@
|
||||
package org.dromara.hutool.db;
|
||||
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.db.ds.DSKeys;
|
||||
import org.dromara.hutool.db.config.DSKeys;
|
||||
import org.dromara.hutool.log.level.Level;
|
||||
import org.dromara.hutool.setting.Setting;
|
||||
|
||||
/**
|
||||
* 数据库操作工具类
|
||||
*
|
||||
* @author Luxiaolei
|
||||
* @author Looly
|
||||
*/
|
||||
public final class DbUtil {
|
||||
|
||||
/**
|
||||
* 从配置文件中读取SQL打印选项,读取后会去除相应属性
|
||||
*
|
||||
* @param setting 配置文件
|
||||
* @since 4.1.7
|
||||
*/
|
||||
public static void setShowSqlGlobal(final Setting setting) {
|
||||
// 初始化SQL显示
|
||||
final Boolean isShowSql = Convert.toBoolean(setting.remove(DSKeys.KEY_SHOW_SQL));
|
||||
final Boolean isFormatSql = Convert.toBoolean(setting.remove(DSKeys.KEY_FORMAT_SQL));
|
||||
final Boolean isShowParams = Convert.toBoolean(setting.remove(DSKeys.KEY_SHOW_PARAMS));
|
||||
String sqlLevelStr = setting.remove(DSKeys.KEY_SQL_LEVEL);
|
||||
if (null != sqlLevelStr) {
|
||||
sqlLevelStr = sqlLevelStr.toUpperCase();
|
||||
}
|
||||
final Level level = Convert.toEnum(Level.class, sqlLevelStr);
|
||||
setShowSqlGlobal(isShowSql, isFormatSql, isShowParams, level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局配置:是否通过debug日志显示SQL
|
||||
*
|
||||
* @param isShowSql 是否显示SQL,{@code null}表示保持默认
|
||||
* @param isFormatSql 是否格式化显示的SQL,{@code null}表示保持默认
|
||||
* @param isShowParams 是否打印参数,{@code null}表示保持默认
|
||||
* @param level 日志级别,{@code null}表示保持默认
|
||||
* @see GlobalDbConfig#setShowSql(Boolean, Boolean, Boolean, Level)
|
||||
* @since 4.1.7
|
||||
*/
|
||||
public static void setShowSqlGlobal(final Boolean isShowSql, final Boolean isFormatSql, final Boolean isShowParams, final Level level) {
|
||||
GlobalDbConfig.setShowSql(isShowSql, isFormatSql, isShowParams, level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局是否在结果中忽略大小写<br>
|
||||
* 如果忽略,则在Entity中调用getXXX时,字段值忽略大小写,默认忽略
|
||||
*
|
||||
* @param caseInsensitive 否在结果中忽略大小写
|
||||
* @see GlobalDbConfig#setCaseInsensitive(boolean)
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static void setCaseInsensitiveGlobal(final boolean caseInsensitive) {
|
||||
GlobalDbConfig.setCaseInsensitive(caseInsensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局是否INSERT语句中默认返回主键(默认返回主键)<br>
|
||||
* 如果false,则在Insert操作后,返回影响行数
|
||||
* 主要用于某些数据库不支持返回主键的情况
|
||||
*
|
||||
* @param returnGeneratedKey 是否INSERT语句中默认返回主键
|
||||
* @see GlobalDbConfig#setReturnGeneratedKey(boolean)
|
||||
* @since 5.3.10
|
||||
*/
|
||||
public static void setReturnGeneratedKeyGlobal(final boolean returnGeneratedKey) {
|
||||
GlobalDbConfig.setReturnGeneratedKey(returnGeneratedKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @param dbSettingPath 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
* @see GlobalDbConfig#setDbSettingPath(String)
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static void setDbSettingPathGlobal(final String dbSettingPath) {
|
||||
GlobalDbConfig.setDbSettingPath(dbSettingPath);
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public class DialectRunner implements Serializable {
|
||||
/**
|
||||
* 是否大小写不敏感(默认大小写不敏感)
|
||||
*/
|
||||
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
|
||||
protected boolean caseInsensitive = true;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
|
@ -1,123 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db;
|
||||
|
||||
import org.dromara.hutool.core.io.resource.NoResourceException;
|
||||
import org.dromara.hutool.db.sql.SqlLog;
|
||||
import org.dromara.hutool.log.level.Level;
|
||||
import org.dromara.hutool.setting.Setting;
|
||||
|
||||
/**
|
||||
* DB全局配置配置项
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.3.10
|
||||
*/
|
||||
public class GlobalDbConfig {
|
||||
/**
|
||||
* 数据库配置文件可选路径1
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH = "config/db.setting";
|
||||
/**
|
||||
* 数据库配置文件可选路径2
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH2 = "db.setting";
|
||||
|
||||
/**
|
||||
* 是否大小写不敏感(默认大小写不敏感)
|
||||
*/
|
||||
protected static boolean caseInsensitive = true;
|
||||
/**
|
||||
* 是否INSERT语句中默认返回主键(默认返回主键)
|
||||
*/
|
||||
protected static boolean returnGeneratedKey = true;
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private static String dbSettingPath = null;
|
||||
|
||||
/**
|
||||
* 设置全局是否在结果中忽略大小写<br>
|
||||
* 如果忽略,则在Entity中调用getXXX时,字段值忽略大小写,默认忽略
|
||||
*
|
||||
* @param isCaseInsensitive 否在结果中忽略大小写
|
||||
*/
|
||||
public static void setCaseInsensitive(final boolean isCaseInsensitive) {
|
||||
caseInsensitive = isCaseInsensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局是否INSERT语句中默认返回主键(默认返回主键)<br>
|
||||
* 如果false,则在Insert操作后,返回影响行数
|
||||
* 主要用于某些数据库不支持返回主键的情况
|
||||
*
|
||||
* @param isReturnGeneratedKey 是否INSERT语句中默认返回主键
|
||||
*/
|
||||
public static void setReturnGeneratedKey(final boolean isReturnGeneratedKey) {
|
||||
returnGeneratedKey = isReturnGeneratedKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @param customDbSettingPath 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static void setDbSettingPath(final String customDbSettingPath) {
|
||||
dbSettingPath = customDbSettingPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义或默认位置数据库配置{@link Setting}
|
||||
*
|
||||
* @return 数据库配置
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static Setting createDbSetting() {
|
||||
Setting setting;
|
||||
if (null != dbSettingPath) {
|
||||
// 自定义数据库配置文件位置
|
||||
try {
|
||||
setting = new Setting(dbSettingPath, false);
|
||||
} catch (final NoResourceException e3) {
|
||||
throw new NoResourceException("Customize db setting file [{}] not found !", dbSettingPath);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH, true);
|
||||
} catch (final NoResourceException e) {
|
||||
// 尝试ClassPath下直接读取配置文件
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH2, true);
|
||||
} catch (final NoResourceException e2) {
|
||||
throw new NoResourceException("Default db setting [{}] or [{}] in classpath not found !", DEFAULT_DB_SETTING_PATH, DEFAULT_DB_SETTING_PATH2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局配置:是否通过debug日志显示SQL
|
||||
*
|
||||
* @param isShowSql 是否显示SQL,{@code null}表示保持默认
|
||||
* @param isFormatSql 是否格式化显示的SQL,{@code null}表示保持默认
|
||||
* @param isShowParams 是否打印参数,{@code null}表示保持默认
|
||||
* @param level 日志级别,{@code null}表示保持默认
|
||||
*/
|
||||
public static void setShowSql(final Boolean isShowSql, final Boolean isFormatSql, final Boolean isShowParams, final Level level) {
|
||||
SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level);
|
||||
}
|
||||
}
|
@ -70,7 +70,7 @@ public class StatementUtil {
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Object... params) {
|
||||
return prepareStatement(GlobalDbConfig.returnGeneratedKey, conn, sql, params);
|
||||
return prepareStatement(true, conn, sql, params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db.config;
|
||||
|
||||
/**
|
||||
* 数据库配置解析接口,通过实现此接口,可完成不同类型的数据源解析为数据库配置
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public interface ConfigParser {
|
||||
/**
|
||||
* 解析,包括数据库基本连接信息、连接属性、连接池参数和其他配置项等
|
||||
*
|
||||
* @param group 分组,当配置文件中有多个数据源时,通过分组区分
|
||||
* @return {@link DbConfig}
|
||||
*/
|
||||
DbConfig parse(String group);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
@ -10,7 +10,7 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db.ds;
|
||||
package org.dromara.hutool.db.config;
|
||||
|
||||
/**
|
||||
* 数据源配置的字段名
|
||||
@ -36,6 +36,14 @@ public interface DSKeys {
|
||||
* 配置文件中配置属性名:显示的日志级别
|
||||
*/
|
||||
String KEY_SQL_LEVEL = "sqlLevel";
|
||||
/**
|
||||
* 配置文件中配置属性名:是否忽略大小写
|
||||
*/
|
||||
String KEY_CASE_INSENSITIVE = "caseInsensitive";
|
||||
/**
|
||||
* 配置文件中配置属性名:INSERT是否返回主键
|
||||
*/
|
||||
String KEY_RETURN_GENERATED_KEY = "returnGeneratedKey";
|
||||
|
||||
/**
|
||||
* 某些数据库需要的特殊配置项需要的配置项
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2023. looly(loolly@aliyun.com)
|
||||
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
@ -10,7 +10,7 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db.ds;
|
||||
package org.dromara.hutool.db.config;
|
||||
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.driver.DriverUtil;
|
||||
@ -30,9 +30,9 @@ import java.util.Properties;
|
||||
public class DbConfig {
|
||||
|
||||
/**
|
||||
* 创建DbConfig
|
||||
* 创建DsConfig
|
||||
*
|
||||
* @return DbConfig
|
||||
* @return DsConfig
|
||||
*/
|
||||
public static DbConfig of() {
|
||||
return new DbConfig();
|
||||
@ -48,6 +48,16 @@ public class DbConfig {
|
||||
// 连接池配置
|
||||
private Properties poolProps;
|
||||
|
||||
// 其它配置
|
||||
/**
|
||||
* 是否大小写不敏感(默认大小写不敏感)
|
||||
*/
|
||||
private boolean caseInsensitive = true;
|
||||
/**
|
||||
* 是否INSERT语句中默认返回主键(默认返回主键)
|
||||
*/
|
||||
private boolean returnGeneratedKey = true;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
@ -235,4 +245,47 @@ public class DbConfig {
|
||||
this.poolProps.setProperty(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取是否在结果中忽略大小写
|
||||
*
|
||||
* @return 是否在结果中忽略大小写
|
||||
*/
|
||||
public boolean isCaseInsensitive() {
|
||||
return this.caseInsensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否在结果中忽略大小写<br>
|
||||
* 如果忽略,则在Entity中调用getXXX时,字段值忽略大小写,默认忽略
|
||||
*
|
||||
* @param isCaseInsensitive 是否在结果中忽略大小写
|
||||
* @return this
|
||||
*/
|
||||
public DbConfig setCaseInsensitive(final boolean isCaseInsensitive) {
|
||||
this.caseInsensitive = isCaseInsensitive;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* INSERT语句中是否返回主键
|
||||
*
|
||||
* @return 是否返回主键
|
||||
*/
|
||||
public boolean isReturnGeneratedKey() {
|
||||
return this.returnGeneratedKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否INSERT语句中默认返回主键(默认返回主键)<br>
|
||||
* 如果false,则在Insert操作后,返回影响行数
|
||||
* 主要用于某些数据库不支持返回主键的情况
|
||||
*
|
||||
* @param isReturnGeneratedKey 是否INSERT语句中默认返回主键
|
||||
* @return this
|
||||
*/
|
||||
public DbConfig setReturnGeneratedKey(final boolean isReturnGeneratedKey) {
|
||||
returnGeneratedKey = isReturnGeneratedKey;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db.config;
|
||||
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.io.resource.NoResourceException;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.driver.DriverUtil;
|
||||
import org.dromara.hutool.setting.Setting;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 基于{@link Setting}类型配置的数据库配置解析
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class SettingConfigParser implements ConfigParser {
|
||||
|
||||
private static final String CONNECTION_PREFIX = "connection.";
|
||||
/**
|
||||
* 数据库配置文件可选路径1
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH = "config/db.setting";
|
||||
/**
|
||||
* 数据库配置文件可选路径2
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH2 = "db.setting";
|
||||
|
||||
/**
|
||||
* 创建默认配置解析器
|
||||
*
|
||||
* @return SettingConfigParser
|
||||
*/
|
||||
public static SettingConfigParser of() {
|
||||
return new SettingConfigParser(null);
|
||||
}
|
||||
|
||||
private final Setting setting;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param setting 自定义配置Setting,{@code null}表示使用默认配置文件,文件位于"config/db.setting"或"db.setting"
|
||||
*/
|
||||
public SettingConfigParser(final Setting setting) {
|
||||
this.setting = null != setting ? setting : createDefaultSetting();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbConfig parse(String group) {
|
||||
if (group == null) {
|
||||
group = StrUtil.EMPTY;
|
||||
}
|
||||
|
||||
final Setting subSetting = setting.getSetting(group);
|
||||
if (MapUtil.isEmpty(subSetting)) {
|
||||
throw new DbRuntimeException("No config for group: [{}]", group);
|
||||
}
|
||||
|
||||
return toDbConfig(subSetting);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义或默认位置数据库配置{@link Setting}
|
||||
*
|
||||
* @return 数据库配置
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private static Setting createDefaultSetting() {
|
||||
Setting setting;
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH, true);
|
||||
} catch (final NoResourceException e) {
|
||||
// 尝试ClassPath下直接读取配置文件
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH2, true);
|
||||
} catch (final NoResourceException e2) {
|
||||
throw new NoResourceException("Default db setting [{}] or [{}] in classpath not found !", DEFAULT_DB_SETTING_PATH, DEFAULT_DB_SETTING_PATH2);
|
||||
}
|
||||
}
|
||||
return setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Setting}数据库配置 转 {@link DbConfig}
|
||||
*
|
||||
* @param setting {@link Setting}数据库配置
|
||||
* @return {@link DbConfig}
|
||||
*/
|
||||
private static DbConfig toDbConfig(final Setting setting) {
|
||||
// 基本信息
|
||||
final String url = setting.getAndRemove(DSKeys.KEY_ALIAS_URL);
|
||||
if (StrUtil.isBlank(url)) {
|
||||
throw new DbRuntimeException("No JDBC URL!");
|
||||
}
|
||||
|
||||
// 移除用户可能误加入的show sql配置项
|
||||
// issue#I3VW0R@Gitee
|
||||
removeShowSqlParams(setting);
|
||||
|
||||
// 自动识别Driver
|
||||
String driver = setting.getAndRemove(DSKeys.KEY_ALIAS_DRIVER);
|
||||
if (StrUtil.isBlank(driver)) {
|
||||
driver = DriverUtil.identifyDriver(url);
|
||||
}
|
||||
|
||||
final DbConfig dbConfig = DbConfig.of()
|
||||
.setUrl(url)
|
||||
.setDriver(driver)
|
||||
.setUser(setting.getAndRemove(DSKeys.KEY_ALIAS_USER))
|
||||
.setPass(setting.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD));
|
||||
|
||||
// 大小写等配置
|
||||
final String caseInsensitive = setting.getAndRemove(DSKeys.KEY_CASE_INSENSITIVE);
|
||||
if (StrUtil.isNotBlank(caseInsensitive)) {
|
||||
dbConfig.setCaseInsensitive(Convert.toBoolean(caseInsensitive));
|
||||
}
|
||||
final String returnGeneratedKey = setting.getAndRemove(DSKeys.KEY_RETURN_GENERATED_KEY);
|
||||
if (StrUtil.isNotBlank(returnGeneratedKey)) {
|
||||
dbConfig.setReturnGeneratedKey(Convert.toBoolean(returnGeneratedKey));
|
||||
}
|
||||
|
||||
// remarks等连接配置,since 5.3.8
|
||||
String connValue;
|
||||
for (final String key : DSKeys.KEY_CONN_PROPS) {
|
||||
connValue = setting.getAndRemove(key);
|
||||
if (StrUtil.isNotBlank(connValue)) {
|
||||
dbConfig.addConnProps(key, connValue);
|
||||
}
|
||||
}
|
||||
|
||||
// 自定义连接属性
|
||||
final Props connProps = new Props();
|
||||
final Set<String> keys = setting.keySet();
|
||||
for (final String key : keys) {
|
||||
if (key.startsWith(CONNECTION_PREFIX)) {
|
||||
connProps.set(StrUtil.subSuf(key, CONNECTION_PREFIX.length()), setting.remove(key));
|
||||
}
|
||||
}
|
||||
dbConfig.setConnProps(connProps);
|
||||
|
||||
// 池属性
|
||||
dbConfig.setPoolProps(setting.toProps());
|
||||
|
||||
return dbConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除配置文件中的Show SQL相关配置项<br>
|
||||
* 此方法用于移除用户配置在分组下的配置项目
|
||||
*
|
||||
* @param setting 配置项
|
||||
* @since 5.7.2
|
||||
*/
|
||||
private static void removeShowSqlParams(final Setting setting) {
|
||||
setting.remove(DSKeys.KEY_SHOW_SQL);
|
||||
setting.remove(DSKeys.KEY_FORMAT_SQL);
|
||||
setting.remove(DSKeys.KEY_SHOW_PARAMS);
|
||||
setting.remove(DSKeys.KEY_SQL_LEVEL);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
@ -10,16 +10,13 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db;
|
||||
|
||||
import org.dromara.hutool.setting.Setting;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class GlobalDbConfigTest {
|
||||
@Test
|
||||
void loadSettingTest() {
|
||||
final Setting dbSetting = GlobalDbConfig.createDbSetting();
|
||||
Assertions.assertNotNull(dbSetting);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 数据库配置,通过统一的配置文件,提供灵活的配置来源,包括但不限于:
|
||||
* <ul>
|
||||
* <li>setting配置文件</li>
|
||||
* <li>XML配置文件</li>
|
||||
* <li>网络配置</li>
|
||||
* <li>自定义Bean</li>
|
||||
* </ul>
|
||||
*/
|
||||
package org.dromara.hutool.db.config;
|
@ -12,6 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.db.ds;
|
||||
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Serializable;
|
||||
|
||||
|
@ -18,19 +18,15 @@ import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
|
||||
import org.dromara.hutool.core.spi.SpiUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.DbUtil;
|
||||
import org.dromara.hutool.db.GlobalDbConfig;
|
||||
import org.dromara.hutool.db.driver.DriverUtil;
|
||||
import org.dromara.hutool.db.config.ConfigParser;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.db.config.SettingConfigParser;
|
||||
import org.dromara.hutool.log.LogUtil;
|
||||
import org.dromara.hutool.setting.Setting;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Closeable;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数据源池,用于支持多数据源。<br>
|
||||
@ -42,8 +38,6 @@ import java.util.Set;
|
||||
*/
|
||||
public class DSPool implements Closeable {
|
||||
|
||||
private static final String CONNECTION_PREFIX = "connection.";
|
||||
|
||||
/**
|
||||
* 获取单例池对象
|
||||
*
|
||||
@ -53,10 +47,7 @@ public class DSPool implements Closeable {
|
||||
return Singleton.get(DSPool.class.getName(), DSPool::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库连接配置文件
|
||||
*/
|
||||
private final Setting setting;
|
||||
private final ConfigParser configParser;
|
||||
/**
|
||||
* 数据源池
|
||||
*/
|
||||
@ -76,33 +67,31 @@ public class DSPool implements Closeable {
|
||||
/**
|
||||
* 构造,通过SPI方式自动获取用户引入的连接池
|
||||
*
|
||||
* @param setting 数据库配置,支持多数据源,{@code null}表示读取classpath:db.setting
|
||||
* @param configParser 数据库配置解析器
|
||||
*/
|
||||
public DSPool(final Setting setting) {
|
||||
this(setting, null);
|
||||
public DSPool(final ConfigParser configParser) {
|
||||
this(configParser, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param setting 数据库配置,支持多数据源,{@code null}表示读取classpath:db.setting
|
||||
* @param configParser 数据库配置解析器
|
||||
* @param factory 数据源工厂,用于创建数据源,{@code null}表示使用SPI自动获取
|
||||
*/
|
||||
public DSPool(final Setting setting, final DSFactory factory) {
|
||||
this.setting = null != setting ? setting : GlobalDbConfig.createDbSetting();
|
||||
DbUtil.setShowSqlGlobal(this.setting);
|
||||
public DSPool(final ConfigParser configParser, final DSFactory factory) {
|
||||
this.configParser = null != configParser ? configParser : SettingConfigParser.of();
|
||||
this.factory = null != factory ? factory : SpiUtil.loadFirstAvailable(DSFactory.class);
|
||||
this.pool = new SafeConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置,用于自定义添加配置项
|
||||
* 获取配置解析器
|
||||
*
|
||||
* @return Setting
|
||||
* @since 4.0.3
|
||||
* @return ConfigParser
|
||||
*/
|
||||
public Setting getSetting() {
|
||||
return this.setting;
|
||||
public ConfigParser getConfigParser() {
|
||||
return this.configParser;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,81 +173,7 @@ public class DSPool implements Closeable {
|
||||
group = StrUtil.EMPTY;
|
||||
}
|
||||
|
||||
final Setting subSetting = setting.getSetting(group);
|
||||
if (MapUtil.isEmpty(subSetting)) {
|
||||
throw new DbRuntimeException("No config for group: [{}]", group);
|
||||
}
|
||||
|
||||
final DbConfig dbConfig = toDbConfig(subSetting);
|
||||
|
||||
final DbConfig dbConfig = this.configParser.parse(group);
|
||||
return DSWrapper.wrap(factory.createDataSource(dbConfig), dbConfig.getDriver());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Setting}数据库配置 转 {@link DbConfig}
|
||||
*
|
||||
* @param setting {@link Setting}数据库配置
|
||||
* @return {@link DbConfig}
|
||||
*/
|
||||
private static DbConfig toDbConfig(final Setting setting) {
|
||||
// 基本信息
|
||||
final String url = setting.getAndRemove(DSKeys.KEY_ALIAS_URL);
|
||||
if (StrUtil.isBlank(url)) {
|
||||
throw new DbRuntimeException("No JDBC URL!");
|
||||
}
|
||||
|
||||
// 移除用户可能误加入的show sql配置项
|
||||
// issue#I3VW0R@Gitee
|
||||
removeShowSqlParams(setting);
|
||||
|
||||
// 自动识别Driver
|
||||
String driver = setting.getAndRemove(DSKeys.KEY_ALIAS_DRIVER);
|
||||
if (StrUtil.isBlank(driver)) {
|
||||
driver = DriverUtil.identifyDriver(url);
|
||||
}
|
||||
|
||||
final DbConfig dbConfig = DbConfig.of()
|
||||
.setUrl(url)
|
||||
.setDriver(driver)
|
||||
.setUser(setting.getAndRemove(DSKeys.KEY_ALIAS_USER))
|
||||
.setPass(setting.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD));
|
||||
|
||||
// remarks等连接配置,since 5.3.8
|
||||
String connValue;
|
||||
for (final String key : DSKeys.KEY_CONN_PROPS) {
|
||||
connValue = setting.getAndRemove(key);
|
||||
if (StrUtil.isNotBlank(connValue)) {
|
||||
dbConfig.addConnProps(key, connValue);
|
||||
}
|
||||
}
|
||||
|
||||
// 自定义连接属性
|
||||
final Props connProps = new Props();
|
||||
final Set<String> keys = setting.keySet();
|
||||
for (final String key : keys) {
|
||||
if (key.startsWith(CONNECTION_PREFIX)) {
|
||||
connProps.set(StrUtil.subSuf(key, CONNECTION_PREFIX.length()), setting.remove(key));
|
||||
}
|
||||
}
|
||||
dbConfig.setConnProps(connProps);
|
||||
|
||||
// 池属性
|
||||
dbConfig.setPoolProps(setting.toProps());
|
||||
|
||||
return dbConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除配置文件中的Show SQL相关配置项<br>
|
||||
* 此方法用于移除用户配置在分组下的配置项目
|
||||
*
|
||||
* @param setting 配置项
|
||||
* @since 5.7.2
|
||||
*/
|
||||
private static void removeShowSqlParams(final Setting setting) {
|
||||
setting.remove(DSKeys.KEY_SHOW_SQL);
|
||||
setting.remove(DSKeys.KEY_FORMAT_SQL);
|
||||
setting.remove(DSKeys.KEY_SHOW_PARAMS);
|
||||
setting.remove(DSKeys.KEY_SQL_LEVEL);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ import cn.beecp.BeeDataSource;
|
||||
import cn.beecp.BeeDataSourceConfig;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -16,7 +16,7 @@ import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds.dbcp;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -16,7 +16,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds.hikari;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -16,7 +16,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DSUtil;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds.pooled;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.pool.Poolable;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
@ -13,7 +13,7 @@
|
||||
package org.dromara.hutool.db.ds.pooled;
|
||||
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
@ -18,7 +18,7 @@ import org.dromara.hutool.core.pool.ObjectPool;
|
||||
import org.dromara.hutool.core.pool.partition.PartitionObjectPool;
|
||||
import org.dromara.hutool.core.pool.partition.PartitionPoolConfig;
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.db.ds.simple.AbstractDataSource;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
package org.dromara.hutool.db.ds.simple;
|
||||
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
package org.dromara.hutool.db.ds.simple;
|
||||
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds.tomcat;
|
||||
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
||||
import org.dromara.hutool.db.ds.DSFactory;
|
||||
import org.dromara.hutool.db.ds.DbConfig;
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.setting.props.Props;
|
||||
|
||||
/**
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
package org.dromara.hutool.db.ds;
|
||||
|
||||
import org.dromara.hutool.db.config.DbConfig;
|
||||
import org.dromara.hutool.db.ds.simple.SimpleDataSource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
Loading…
x
Reference in New Issue
Block a user