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