mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
!510 mybatis-flex-solon-plugin:增加默认数据源的可控性
Merge pull request !510 from 西东/main
This commit is contained in:
commit
e9e3f4db0f
@ -44,15 +44,30 @@ public class FlexDataSource extends AbstractDataSource {
|
||||
private final Map<String, DataSource> dataSourceMap = new HashMap<>();
|
||||
private final Map<String, DbType> dbTypeHashMap = new HashMap<>();
|
||||
|
||||
private final DbType defaultDbType;
|
||||
private final String defaultDataSourceKey;
|
||||
private final DataSource defaultDataSource;
|
||||
private DbType defaultDbType;
|
||||
private String defaultDataSourceKey;
|
||||
private DataSource defaultDataSource;
|
||||
|
||||
public FlexDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
this(dataSourceKey, dataSource, true);
|
||||
}
|
||||
|
||||
public FlexDataSource(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
|
||||
setDefaultDataSourceDo(dataSourceKey, dataSource, needDecryptDataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认数据源(提供动态可控性)
|
||||
*/
|
||||
public void setDefaultDataSource(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
|
||||
if (defaultDataSourceKey.equals(dataSourceKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDefaultDataSourceDo(dataSourceKey, dataSource, needDecryptDataSource);
|
||||
}
|
||||
|
||||
private void setDefaultDataSourceDo(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
|
||||
if (needDecryptDataSource) {
|
||||
DataSourceManager.decryptDataSource(dataSource);
|
||||
}
|
||||
@ -269,6 +284,4 @@ public class FlexDataSource extends AbstractDataSource {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
package com.mybatisflex.solon.integration;
|
||||
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import org.noear.solon.data.datasource.RoutingDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 用与 solon 的事务对接
|
||||
*
|
||||
* @author noear
|
||||
* @since 2.8
|
||||
*/
|
||||
public class FlexRoutingDataSource extends FlexDataSource implements RoutingDataSource {
|
||||
public FlexRoutingDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
super(dataSourceKey, dataSource);
|
||||
}
|
||||
|
||||
public FlexRoutingDataSource(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
|
||||
super(dataSourceKey, dataSource, needDecryptDataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource determineCurrentTarget() {
|
||||
return getDataSource();
|
||||
}
|
||||
}
|
||||
@ -64,7 +64,7 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault {
|
||||
|
||||
@Override
|
||||
protected DataSource getDataSource() {
|
||||
return new FlexRoutingDataSource(dsWrap.name(), dsWrap.raw());
|
||||
return dsWrap.raw();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -21,6 +21,7 @@ import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||
import com.mybatisflex.core.row.RowMapperInvoker;
|
||||
import com.mybatisflex.solon.aot.MybatisRuntimeNativeRegistrar;
|
||||
@ -90,6 +91,12 @@ public class XPluginImpl implements Plugin {
|
||||
boolean isInit = MybatisFlexBootstrap.getInstance().getDataSource() == null;
|
||||
MybatisFlexBootstrap.getInstance().addDataSource(bw.name(), bw.raw());
|
||||
|
||||
if (bw.typed()) {
|
||||
//控制默认数据源
|
||||
FlexDataSource flexDataSource = (FlexDataSource) MybatisFlexBootstrap.getInstance().getDataSource();
|
||||
flexDataSource.setDefaultDataSource(bw.name(), bw.raw(), true);
|
||||
}
|
||||
|
||||
if (isInit) {
|
||||
initDo(context);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user