refactor: optimize MapperInvocationHandler.java

This commit is contained in:
Michael Yang 2024-02-26 09:30:59 +08:00
parent 1095d69155
commit 203f4d5236

View File

@ -25,6 +25,7 @@ import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.reflection.ExceptionUtil;
import javax.sql.DataSource;
import java.lang.reflect.InvocationHandler;
@ -76,25 +77,32 @@ public class MapperInvocationHandler implements InvocationHandler {
//优先获取用户自己配置的 dbType
DbType dbType = DialectFactory.getHintDbType();
DbType dbTypeGlobal = DialectFactory.getGlobalDbType();
//当前线程没有设置dbType,但是全局设置了dbTypeGlobal那么就使用全局的dbTypeGlobal
if(dbTypeGlobal!=null&&dbType==null){
dbType = dbTypeGlobal ;
}
if (dbType == null) {
if (shardingDataSourceKey != null && dataSource != null) {
//使用最终分片获取数据源类型
dbType = dataSource.getDbType(shardingDataSourceKey);
}
if (dbType == null && dataSourceKey != null && dataSource != null) {
dbType = dataSource.getDbType(dataSourceKey);
}
//设置了dbTypeGlobal那么就使用全局的dbTypeGlobal
if (dbType == null) {
dbType = DialectFactory.getGlobalDbType();
}
if (dbType == null) {
dbType = FlexGlobalConfig.getDefaultConfig().getDbType();
}
DialectFactory.setHintDbType(dbType);
needClearDbType = true;
}
return method.invoke(mapper, args);
} catch (InvocationTargetException e) {
throw e.getCause();
} catch (Throwable e) {
throw ExceptionUtil.unwrapThrowable(e);
} finally {
if (needClearDbType) {
DialectFactory.clearHintDbType();