add multi datasource support

This commit is contained in:
开源海哥 2023-03-31 09:46:36 +08:00
parent 3006ab30da
commit f0fba210aa
2 changed files with 26 additions and 7 deletions

View File

@ -15,19 +15,39 @@
*/ */
package com.mybatisflex.core.datasource; package com.mybatisflex.core.datasource;
import java.util.function.Supplier;
public class DataSourceKey { public class DataSourceKey {
private static ThreadLocal<String> keyThreadLocal = new ThreadLocal<>(); private static ThreadLocal<String> keyThreadLocal = new ThreadLocal<>();
public static void use(String environmentId){ public static void use(String environmentId) {
keyThreadLocal.set(environmentId); keyThreadLocal.set(environmentId);
} }
public static void clear(){ public static <T> T use(String environmentId, Supplier<T> supplier) {
try {
use(environmentId);
return supplier.get();
} finally {
clear();
}
}
public static void use(String environmentId, Runnable runnable) {
try {
use(environmentId);
runnable.run();
} finally {
clear();
}
}
public static void clear() {
keyThreadLocal.remove(); keyThreadLocal.remove();
} }
public static String get(){ public static String get() {
return keyThreadLocal.get(); return keyThreadLocal.get();
} }

View File

@ -93,14 +93,13 @@ public class DataSourceBuilder {
public static String attrToCamel(String string) { public static String attrToCamel(String string) {
String temp = string.toLowerCase(); int strLen = string.length();
int strLen = temp.length();
StringBuilder sb = new StringBuilder(strLen); StringBuilder sb = new StringBuilder(strLen);
for (int i = 0; i < strLen; i++) { for (int i = 0; i < strLen; i++) {
char c = temp.charAt(i); char c = string.charAt(i);
if (c == '-') { if (c == '-') {
if (++i < strLen) { if (++i < strLen) {
sb.append(Character.toUpperCase(temp.charAt(i))); sb.append(Character.toUpperCase(string.charAt(i)));
} }
} else { } else {
sb.append(c); sb.append(c);