时间问题处理
This commit is contained in:
1 parent
41190a25ae
commit
d910c1afe8
66 files changed
+604
-393
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** MySQL连接与JVM本地时间保持一致,禁止仅修改驱动时区而遗留数据库会话时区。 */
|
||||
public final class DatabaseTimeZone {
|
||||
private DatabaseTimeZone() { }
|
||||
|
||||
/**
|
||||
* 仅替换时区选项,保留连接地址、SSL及其他参数,不记录URL(其中可能含凭证)。
|
||||
* 需要Connector/J 8.0.23+;serverTimezone旧配置也统一归一化,避免其覆盖LOCAL。
|
||||
*/
|
||||
public static String normalizeMySqlUrl(String url) {
|
||||
if (url == null || !url.startsWith("jdbc:mysql:")) {
|
||||
return url;
|
||||
}
|
||||
int separator = url.indexOf('?');
|
||||
String base = separator < 0 ? url : url.substring(0, separator);
|
||||
List<String> options = new ArrayList<>();
|
||||
if (separator >= 0) {
|
||||
for (String option : url.substring(separator + 1).split("&")) {
|
||||
String key = option.split("=", 2)[0];
|
||||
if (!option.isEmpty() && !"serverTimezone".equalsIgnoreCase(key)
|
||||
&& !"connectionTimeZone".equalsIgnoreCase(key)
|
||||
&& !"forceConnectionTimeZoneToSession".equalsIgnoreCase(key)) {
|
||||
options.add(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
options.add("connectionTimeZone=LOCAL");
|
||||
options.add("forceConnectionTimeZoneToSession=true");
|
||||
return base + "?" + String.join("&", options);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user