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,6 +15,8 @@
*/
package com.mybatisflex.core.datasource;
import java.util.function.Supplier;
public class DataSourceKey {
private static ThreadLocal<String> keyThreadLocal = new ThreadLocal<>();
@ -23,6 +25,24 @@ public class DataSourceKey {
keyThreadLocal.set(environmentId);
}
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();
}

View File

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