mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add global config
This commit is contained in:
parent
58b6d4851a
commit
b3824c859c
CHANGELOG.md
hutool-db/src/main/java/cn/hutool/db
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.0.M3 (2022-04-13)
|
||||
# 5.8.0.M3 (2022-04-14)
|
||||
|
||||
### ❌不兼容特性
|
||||
* 【core 】 StreamProgress#progress方法参数变更为2个(pr#594@Gitee)
|
||||
@ -20,6 +20,7 @@
|
||||
* 【core 】 Img增加全覆盖水印pressTextFull(pr#595@Gitee)
|
||||
* 【core 】 ByteUtil.numberToBytes增加Byte判断(issue#2252@Github)
|
||||
* 【core 】 CopyOptions添加converter,可以自定义非全局类型转换
|
||||
* 【core 】 添加了设置从绝对路径加载数据库配置文件的功能(pr#2253@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复UserAgentUtil识别Linux出错(issue#I50YGY@Gitee)
|
||||
|
@ -4,7 +4,6 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.db.dialect.Dialect;
|
||||
import cn.hutool.db.dialect.DialectFactory;
|
||||
import cn.hutool.db.ds.AbstractDSFactory;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
import cn.hutool.db.sql.SqlLog;
|
||||
import cn.hutool.log.Log;
|
||||
@ -210,6 +209,7 @@ public final class DbUtil {
|
||||
* @param isFormatSql 是否格式化显示的SQL
|
||||
* @param isShowParams 是否打印参数
|
||||
* @param level SQL打印到的日志等级
|
||||
* @see GlobalDbConfig#setShowSql(boolean, boolean, boolean, Level)
|
||||
* @since 4.1.7
|
||||
*/
|
||||
public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
|
||||
@ -221,6 +221,7 @@ public final class DbUtil {
|
||||
* 如果忽略,则在Entity中调用getXXX时,字段值忽略大小写,默认忽略
|
||||
*
|
||||
* @param caseInsensitive 否在结果中忽略大小写
|
||||
* @see GlobalDbConfig#setCaseInsensitive(boolean)
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static void setCaseInsensitiveGlobal(boolean caseInsensitive) {
|
||||
@ -233,6 +234,7 @@ public final class DbUtil {
|
||||
* 主要用于某些数据库不支持返回主键的情况
|
||||
*
|
||||
* @param returnGeneratedKey 是否INSERT语句中默认返回主键
|
||||
* @see GlobalDbConfig#setReturnGeneratedKey(boolean)
|
||||
* @since 5.3.10
|
||||
*/
|
||||
public static void setReturnGeneratedKeyGlobal(boolean returnGeneratedKey) {
|
||||
@ -240,12 +242,13 @@ public final class DbUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置从绝对路径加载数据库配置文件
|
||||
* (特殊情况使用,比如写Minecraft插件的时候)
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @param absolutePath 配置文件的绝对路径
|
||||
* @param dbSettingPath 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
* @see GlobalDbConfig#setDbSettingPath(String)
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static void setCustomizeDbSettingPath(String absolutePath) {
|
||||
AbstractDSFactory.CUSTOMIZE_DB_SETTING_PATH = absolutePath;
|
||||
public static void setDbSettingPathGlobal(String dbSettingPath) {
|
||||
GlobalDbConfig.setDbSettingPath(dbSettingPath);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.io.resource.NoResourceException;
|
||||
import cn.hutool.db.sql.SqlLog;
|
||||
import cn.hutool.log.level.Level;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
/**
|
||||
* DB全局配置配置项
|
||||
@ -10,6 +12,15 @@ import cn.hutool.log.level.Level;
|
||||
* @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";
|
||||
|
||||
/**
|
||||
* 是否大小写不敏感(默认大小写不敏感)
|
||||
*/
|
||||
@ -18,6 +29,12 @@ public class GlobalDbConfig {
|
||||
* 是否INSERT语句中默认返回主键(默认返回主键)
|
||||
*/
|
||||
protected static boolean returnGeneratedKey = true;
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private static String dbSettingPath = null;
|
||||
|
||||
/**
|
||||
* 设置全局是否在结果中忽略大小写<br>
|
||||
@ -40,6 +57,46 @@ public class GlobalDbConfig {
|
||||
returnGeneratedKey = isReturnGeneratedKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
*
|
||||
* @param customDbSettingPath 自定义数据库配置文件路径(绝对路径或相对classpath路径)
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static void setDbSettingPath(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 (NoResourceException e3) {
|
||||
throw new NoResourceException("Customize db setting file [{}] not found !", dbSettingPath);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH, true);
|
||||
} catch (NoResourceException e) {
|
||||
// 尝试ClassPath下直接读取配置文件
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH2, true);
|
||||
} catch (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
|
||||
*
|
||||
|
@ -1,11 +1,11 @@
|
||||
package cn.hutool.db.ds;
|
||||
|
||||
import cn.hutool.core.io.resource.NoResourceException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.DbUtil;
|
||||
import cn.hutool.db.GlobalDbConfig;
|
||||
import cn.hutool.db.dialect.DriverUtil;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
@ -24,19 +24,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public abstract class AbstractDSFactory extends DSFactory {
|
||||
private static final long serialVersionUID = -6407302276272379881L;
|
||||
|
||||
/**
|
||||
* 数据库配置文件可选路径1
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH = "config/db.setting";
|
||||
/**
|
||||
* 数据库配置文件可选路径2
|
||||
*/
|
||||
private static final String DEFAULT_DB_SETTING_PATH2 = "db.setting";
|
||||
/**
|
||||
* 自定义数据库配置文件路径(绝对路径,特殊情况使用,比如写Minecraft插件的时候)
|
||||
*/
|
||||
public static String CUSTOMIZE_DB_SETTING_PATH = null;
|
||||
|
||||
/**
|
||||
* 数据库连接配置文件
|
||||
*/
|
||||
@ -52,37 +39,15 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
* @param dataSourceName 数据源名称
|
||||
* @param dataSourceClass 数据库连接池实现类,用于检测所提供的DataSource类是否存在,当传入的DataSource类不存在时抛出ClassNotFoundException<br>
|
||||
* 此参数的作用是在detectDSFactory方法自动检测所用连接池时,如果实现类不存在,调用此方法会自动抛出异常,从而切换到下一种连接池的检测。
|
||||
* @param setting 数据库连接配置
|
||||
* @param setting 数据库连接配置,如果为{@code null},则读取全局自定义或默认配置
|
||||
*/
|
||||
public AbstractDSFactory(String dataSourceName, Class<? extends DataSource> dataSourceClass, Setting setting) {
|
||||
super(dataSourceName);
|
||||
//此参数的作用是在detectDSFactory方法自动检测所用连接池时,如果实现类不存在,调用此方法会自动抛出异常,从而切换到下一种连接池的检测。
|
||||
Assert.notNull(dataSourceClass);
|
||||
if (CUSTOMIZE_DB_SETTING_PATH != null) {
|
||||
try {
|
||||
setting = new Setting(CUSTOMIZE_DB_SETTING_PATH, false);
|
||||
} catch (NoResourceException e3) {
|
||||
throw new NoResourceException("Customize db setting file [{}] not found !", CUSTOMIZE_DB_SETTING_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
if (null == setting) {
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH, true);
|
||||
} catch (NoResourceException e) {
|
||||
// 尝试ClassPath下直接读取配置文件
|
||||
try {
|
||||
setting = new Setting(DEFAULT_DB_SETTING_PATH2, true);
|
||||
} catch (NoResourceException e2) {
|
||||
throw new NoResourceException("Default db setting [{}] or [{}] in classpath not found !", DEFAULT_DB_SETTING_PATH, DEFAULT_DB_SETTING_PATH2);
|
||||
}
|
||||
}
|
||||
if (CUSTOMIZE_DB_SETTING_PATH != null) {
|
||||
try {
|
||||
setting = new Setting(CUSTOMIZE_DB_SETTING_PATH, false);
|
||||
} catch (NoResourceException e3) {
|
||||
throw new NoResourceException("Customize db setting file [{}] not found !", CUSTOMIZE_DB_SETTING_PATH);
|
||||
}
|
||||
}
|
||||
setting = GlobalDbConfig.createDbSetting();
|
||||
}
|
||||
|
||||
// 读取配置,用于SQL打印
|
||||
|
Loading…
Reference in New Issue
Block a user