|
@@ -71,7 +71,7 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|
* @throws NoSuchFieldException
|
|
* @throws NoSuchFieldException
|
|
* @throws IllegalAccessException
|
|
* @throws IllegalAccessException
|
|
*/
|
|
*/
|
|
- public boolean changeDataSource(GenConfig genConfig) throws NoSuchFieldException, IllegalAccessException {
|
|
|
|
|
|
+ public boolean changeDataSource(GenConfig genConfig, boolean isStateScode) throws NoSuchFieldException, IllegalAccessException {
|
|
String url = genConfig.url;
|
|
String url = genConfig.url;
|
|
String driverClassName = genConfig.driverClassName;
|
|
String driverClassName = genConfig.driverClassName;
|
|
String username = genConfig.username;
|
|
String username = genConfig.username;
|
|
@@ -84,10 +84,10 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|
*/
|
|
*/
|
|
if (DataSourceType.ORACLE.getDataSourceName().equals(dbtype)) {
|
|
if (DataSourceType.ORACLE.getDataSourceName().equals(dbtype)) {
|
|
dbName = url.substring(url.lastIndexOf(":") + 1);
|
|
dbName = url.substring(url.lastIndexOf(":") + 1);
|
|
- }else if(DataSourceType.SQLSERVER.getDataSourceName().equals(dbtype)){
|
|
|
|
|
|
+ } else if (DataSourceType.SQLSERVER.getDataSourceName().equals(dbtype)) {
|
|
dbName = url.substring(url.indexOf("DatabaseName=") + 13);
|
|
dbName = url.substring(url.indexOf("DatabaseName=") + 13);
|
|
url += UrlSuffix.SQLSERVER;
|
|
url += UrlSuffix.SQLSERVER;
|
|
- }else if(DataSourceType.DM.getDataSourceName().equals(dbtype)){
|
|
|
|
|
|
+ } else if (DataSourceType.DM.getDataSourceName().equals(dbtype)) {
|
|
dbName = url.substring(url.lastIndexOf("=") + 1);
|
|
dbName = url.substring(url.lastIndexOf("=") + 1);
|
|
} else {
|
|
} else {
|
|
dbName = url.substring(url.lastIndexOf("/") + 1);
|
|
dbName = url.substring(url.lastIndexOf("/") + 1);
|
|
@@ -121,12 +121,18 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|
super.afterPropertiesSet();
|
|
super.afterPropertiesSet();
|
|
// 切换数据源
|
|
// 切换数据源
|
|
setDataSource(dbtype);
|
|
setDataSource(dbtype);
|
|
|
|
+
|
|
|
|
+ // 校验是否切换数据源信息
|
|
|
|
+ if (isStateScode) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
/*
|
|
/*
|
|
** 修改mybatis的数据源
|
|
** 修改mybatis的数据源
|
|
* !!!重要,不修改mybatis的数据源的话,
|
|
* !!!重要,不修改mybatis的数据源的话,
|
|
* 即使切换了数据源之后还是会出现默认数据源的情况
|
|
* 即使切换了数据源之后还是会出现默认数据源的情况
|
|
*/
|
|
*/
|
|
- SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)SpringContextUtils.getBean(SqlSessionFactory.class);
|
|
|
|
|
|
+ SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) SpringContextUtils.getBean(SqlSessionFactory.class);
|
|
Configuration configuration = sqlSessionFactory.getConfiguration();
|
|
Configuration configuration = sqlSessionFactory.getConfiguration();
|
|
/*
|
|
/*
|
|
* !!!重要,设置databaseId,用于在mapper.xml中找到对应的_databaseId,
|
|
* !!!重要,设置databaseId,用于在mapper.xml中找到对应的_databaseId,
|