Commit 8c4d2b5f by gechengyang

整合多数据基础版本,待完善。

parent 9846dda4
...@@ -13,6 +13,7 @@ import com.brilliance.mda.runtime.mda.driver.MdaDriver; ...@@ -13,6 +13,7 @@ import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv; import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.AbstractDaoSession; import com.brilliance.mda.runtime.mda.impl.AbstractDaoSession;
import com.brilliance.mda.runtime.mda.impl.Argument; import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.support.mybatis.MybatisArgumentAdapter; import com.brilliance.mda.support.mybatis.MybatisArgumentAdapter;
import com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType; import com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType;
import com.brilliance.mda.support.mybatis.count.CounterService; import com.brilliance.mda.support.mybatis.count.CounterService;
...@@ -47,9 +48,19 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -47,9 +48,19 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
private static final String SQLID_DYNCREAD = "dyncRead"; private static final String SQLID_DYNCREAD = "dyncRead";
private static final String SQLID_DYNCDEL = "dyncDelete"; private static final String SQLID_DYNCDEL = "dyncDelete";
private static ThreadLocal<String> dbLocal=new ThreadLocal<>(); private static ThreadLocal<Map> dbLocal=new ThreadLocal<>();
private static final String DATASOURCE_DB1="dataSource"; private static final String DATASOURCE_DB1="dataSource";
private static final String DATASOURCE_DB2="dataSource2"; private static final String DATASOURCE_DB2="dataSource2";
private static final String DEFAULT_DATASOURCE=DATASOURCE_DB1;
static
{
Map map=new HashMap();
map.put(DEFAULT_DATASOURCE,DEFAULT_DATASOURCE);
map.put(DATASOURCE_DB1,DATASOURCE_DB1);
map.put(DATASOURCE_DB2,DATASOURCE_DB2);
dbLocal.set(map);
}
@Autowired @Autowired
...@@ -73,6 +84,34 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -73,6 +84,34 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
public MyBatisDaoSession() { public MyBatisDaoSession() {
} }
public SqlSessionTemplate getSqlSessionTemplateByChangeDb(String dbName)
{
Map dbMap=dbLocal.get();
if(MdaUtils.isEmpty(dbName))
{
dbMap.put(DEFAULT_DATASOURCE,DEFAULT_DATASOURCE);
}
else
{
dbMap.put(DEFAULT_DATASOURCE,dbName);
}
return getSqlSessionTemplate(dbName);
}
public SqlSessionTemplate getSqlSessionTemplate(String dbName)
{
SqlSessionTemplate sqlSessionTemplate=this.sqlSessionTemplate1;
if(DATASOURCE_DB1.equals(dbName))
{
sqlSessionTemplate=this.sqlSessionTemplate1;
}
if(DATASOURCE_DB2.equals(dbName))
{
sqlSessionTemplate=this.sqlSessionTemplate2;
}
return sqlSessionTemplate;
}
public void setNamespacePackage(String namespacePackage) { public void setNamespacePackage(String namespacePackage) {
this.namespacePackage = namespacePackage; this.namespacePackage = namespacePackage;
} }
...@@ -234,7 +273,8 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -234,7 +273,8 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
public void dbConnect() { public void dbConnect() {
//this.sqlSessionTemplate.getSqlSessionFactory().openSession(false); //this.sqlSessionTemplate.getSqlSessionFactory().openSession(false);
this.getSqlSessionTemplate(null).getSqlSessionFactory().openSession(false); // this.getSqlSessionTemplate(null).getSqlSessionFactory().openSession(false);
this.getSqlSessionTemplateByChangeDb(null).getSqlSessionFactory().openSession(false);
} }
// @Override // @Override
...@@ -245,23 +285,6 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -245,23 +285,6 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
public SqlSessionTemplate getSqlSessionTemplate(String dbName)
{
SqlSessionTemplate sqlSessionTemplate=this.sqlSessionTemplate1;
if(DATASOURCE_DB1.equals(dbName))
{
sqlSessionTemplate=this.sqlSessionTemplate1;
}
if(DATASOURCE_DB2.equals(dbName))
{
sqlSessionTemplate=this.sqlSessionTemplate2;
}
return sqlSessionTemplate;
}
public <T> T dyncReadOneTest(String dbName,Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) { public <T> T dyncReadOneTest(String dbName,Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) {
SqlSessionTemplate sqlSessionTemplate=getSqlSessionTemplate(dbName); SqlSessionTemplate sqlSessionTemplate=getSqlSessionTemplate(dbName);
String sqlId = this.getSqlId(clazz, "dyncRead"); String sqlId = this.getSqlId(clazz, "dyncRead");
...@@ -275,7 +298,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -275,7 +298,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
public void dbDisconnect() { public void dbDisconnect() {
try { try {
this.getSqlSessionTemplate(null).getConnection().close(); this.getSqlSessionTemplateByChangeDb(null).getConnection().close();
} catch (SQLException var2) { } catch (SQLException var2) {
var2.printStackTrace(); var2.printStackTrace();
} }
...@@ -289,7 +312,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -289,7 +312,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
} }
public String dbName() { public String dbName() {
return this.getSqlSessionTemplate(null).getConfiguration().getDatabaseId(); return this.getSqlSessionTemplateByChangeDb(null).getConfiguration().getDatabaseId();
} }
public void dbExecuteSQL(String sql, Object... objects) { public void dbExecuteSQL(String sql, Object... objects) {
...@@ -406,7 +429,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -406,7 +429,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
private <T> T dyncReadOne(Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) { private <T> T dyncReadOne(Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) {
String sqlId = this.getSqlId(clazz, "dyncRead"); String sqlId = this.getSqlId(clazz, "dyncRead");
T entity = this.getSqlSessionTemplate(null).selectOne(sqlId, adapter.getSqlParams()); T entity = this.getSqlSessionTemplateByChangeDb(null).selectOne(sqlId, adapter.getSqlParams());
if (entity == null) { if (entity == null) {
this.setNoMoreRow(); this.setNoMoreRow();
} }
...@@ -416,7 +439,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -416,7 +439,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
private <T> List<T> dyncRead(Class<T> clazz, MybatisArgumentAdapter adapter) { private <T> List<T> dyncRead(Class<T> clazz, MybatisArgumentAdapter adapter) {
String sqlId = this.getSqlId(clazz, "dyncRead"); String sqlId = this.getSqlId(clazz, "dyncRead");
List<T> list = this.getSqlSessionTemplate(null).selectList(sqlId, adapter.getSqlParams()); List<T> list = this.getSqlSessionTemplateByChangeDb(null).selectList(sqlId, adapter.getSqlParams());
if (list == null || list.size() == 0) { if (list == null || list.size() == 0) {
this.setNoMoreRow(); this.setNoMoreRow();
} }
...@@ -426,7 +449,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -426,7 +449,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
private <T> List<T> dyncRead(Class<T> clazz, Map params) { private <T> List<T> dyncRead(Class<T> clazz, Map params) {
String sqlId = this.getSqlId(clazz, "dyncRead"); String sqlId = this.getSqlId(clazz, "dyncRead");
List<T> list = this.getSqlSessionTemplate(null).selectList(sqlId, params); List<T> list = this.getSqlSessionTemplateByChangeDb(null).selectList(sqlId, params);
if (list == null || list.size() == 0) { if (list == null || list.size() == 0) {
this.setNoMoreRow(); this.setNoMoreRow();
} }
...@@ -439,7 +462,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -439,7 +462,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
} }
private int executeUpdate(String mybatisSqlId, Object params) { private int executeUpdate(String mybatisSqlId, Object params) {
int cnt = this.getSqlSessionTemplate(null).update(mybatisSqlId, params); int cnt =this.getSqlSessionTemplateByChangeDb(null).update(mybatisSqlId, params);
return cnt; return cnt;
} }
......
...@@ -26,8 +26,8 @@ public class DataSourceConfig { ...@@ -26,8 +26,8 @@ public class DataSourceConfig {
* spring.datasource.db1:application.properteis中对应属性的前缀 * spring.datasource.db1:application.properteis中对应属性的前缀
* @return * @return
*/ */
@Bean(name = "dataSource") @Bean(name = "eibs")
@ConfigurationProperties(prefix = "spring.datasource.druid") @ConfigurationProperties(prefix = "spring.datasource.eibs")
// @Bean(name = "db1") // @Bean(name = "db1")
// @ConfigurationProperties(prefix = "spring.datasource.db1") // @ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource dataSourceOne() { public DataSource dataSourceOne() {
...@@ -41,12 +41,27 @@ public class DataSourceConfig { ...@@ -41,12 +41,27 @@ public class DataSourceConfig {
*/ */
//@Bean(name = "db2") //@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2") //@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean(name = "dataSource2") @Bean(name = "swd")
@ConfigurationProperties(prefix = "spring.datasource2") @ConfigurationProperties(prefix = "spring.dataource.swd")
public DataSource dataSourceTwo() { public DataSource dataSourceTwo() {
return DataSourceBuilder.create().build(); return DataSourceBuilder.create().build();
} }
/**
* 数据源3
* spring.datasource.db2:application.properteis中对应属性的前缀
* @return
*/
//@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean(name = "fxd")
@ConfigurationProperties(prefix = "spring.dataource.fxd")
public DataSource dataSourceThree() {
return DataSourceBuilder.create().build();
}
/** /**
* 动态数据源: 通过AOP在不同数据源之间动态切换 * 动态数据源: 通过AOP在不同数据源之间动态切换
* @return * @return
...@@ -59,8 +74,9 @@ public class DataSourceConfig { ...@@ -59,8 +74,9 @@ public class DataSourceConfig {
dynamicDataSource.setDefaultTargetDataSource(dataSourceOne()); dynamicDataSource.setDefaultTargetDataSource(dataSourceOne());
// 配置多数据源 // 配置多数据源
Map<Object, Object> dsMap = new HashMap<>(); Map<Object, Object> dsMap = new HashMap<>();
dsMap.put("dataSource", dataSourceOne()); dsMap.put("eibs", dataSourceOne());
dsMap.put("dataSource2", dataSourceTwo()); dsMap.put("swd", dataSourceTwo());
dsMap.put("three", dataSourceThree());
dynamicDataSource.setTargetDataSources(dsMap); dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource; return dynamicDataSource;
...@@ -78,9 +94,9 @@ public class DataSourceConfig { ...@@ -78,9 +94,9 @@ public class DataSourceConfig {
} }
@Bean(name = "sqlSessionFactory1") @Bean(name = "eibs")
@Primary @Primary
public SqlSessionFactory getSqlSessionFactory1(@Qualifier("dataSource") DataSource dataSource) throws Exception{ public SqlSessionFactory getEibs(@Qualifier("eibs") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean(); SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource); sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml")); // sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
...@@ -90,13 +106,13 @@ public class DataSourceConfig { ...@@ -90,13 +106,13 @@ public class DataSourceConfig {
@Bean(name = "transactionManager") @Bean(name = "transactionManager")
@Primary @Primary
public DataSourceTransactionManager getTransactionManager1(@Qualifier("dataSource")DataSource dataSource) { public DataSourceTransactionManager getTransactionManager1(@Qualifier("eibs")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource); return new DataSourceTransactionManager(dataSource);
} }
@Bean(name = "sqlSessionTemplate1") @Bean(name = "eibs")
@Primary @Primary
public SqlSessionTemplate getSqlSessionTemplate1(@Qualifier("sqlSessionFactory1")SqlSessionFactory sqlSessionFactory) throws Exception { public SqlSessionTemplate getSqlSessionTemplate1(@Qualifier("eibs")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory); return new SqlSessionTemplate(sqlSessionFactory);
} }
...@@ -104,22 +120,42 @@ public class DataSourceConfig { ...@@ -104,22 +120,42 @@ public class DataSourceConfig {
//----------------------------------------- //-----------------------------------------
@Bean(name = "sqlSessionFactory2") @Bean(name = "swd")
public SqlSessionFactory getSqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception{ public SqlSessionFactory getSqlSessionFactory2(@Qualifier("swd") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean(); SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource); sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml")); // sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml")); sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject(); return sqlfb.getObject();
} }
//
@Bean(name = "transactionManager2") @Bean(name = "transactionManager2")
public DataSourceTransactionManager getTransactionManager2(@Qualifier("dataSource2")DataSource dataSource) { public DataSourceTransactionManager getTransactionManager2(@Qualifier("swd")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "swd")
public SqlSessionTemplate getSqlSessionTemplate2(@Qualifier("swd")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "fxd")
public SqlSessionFactory getSqlSessionFactory3(@Qualifier("swd") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject();
}
//
@Bean(name = "transactionManager3")
public DataSourceTransactionManager getTransactionManager3(@Qualifier("fxd")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource); return new DataSourceTransactionManager(dataSource);
} }
@Bean(name = "sqlSessionTemplate2") @Bean(name = "fxd")
public SqlSessionTemplate getSqlSessionTemplate2(@Qualifier("sqlSessionFactory2")SqlSessionFactory sqlSessionFactory) throws Exception { public SqlSessionTemplate getSqlSessionTemplate3(@Qualifier("fxd")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory); return new SqlSessionTemplate(sqlSessionFactory);
} }
} }
...@@ -1699,7 +1699,9 @@ public class Platform ...@@ -1699,7 +1699,9 @@ public class Platform
if(beginDB==null) if(beginDB==null)
{ {
//log.debug("mdbconnect "+db); //log.debug("mdbconnect "+db);
// ctx.getDaoSession().dbConnect(db); //TODO ??/
// GCY测试中
// ctx.getDaoSession()
Platform.setLastDB(db); Platform.setLastDB(db);
} }
} }
......
...@@ -5,73 +5,25 @@ server: ...@@ -5,73 +5,25 @@ server:
spring: spring:
datasource: datasource:
druid: swd:
driver-class-name: oracle.jdbc.driver.OracleDriver jdbc-url: jdbc:postgresql://192.168.0.110:55435/swd?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true
url: jdbc:oracle:thin:@114.115.138.98:9400:XE username: postgres
username: TEST password: postgres
password: test driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 下面为连接池的补充设置,应用到上面所有数据源中 fxd:
# 初始化大小,最小,最大 jdbc-url: jdbc:postgresql://192.168.0.110:55435/fxd?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true
# druid 配置: https://github.com/alibaba/druid/wiki/ username: postgres
#初始化链接数 password: postgres
initialSize: 1 driver-class-name: org.postgresql.Driver
minIdle: 1
maxActive: 20 eibs:
# 配置获取连接等待超时的时间 jdbc-url: jdbc:postgresql://192.168.0.110:55435/eibs?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true
maxWait: 60000 username: postgres
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 password: postgres
timeBetweenEvictionRunsMillis: 60000 driver-class-name: org.postgresql.Driver
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,stat用于监控界面,'wall'用于防火墙防御sql注入, slf4j用于druid记录sql日志
filters: stat,slf4j
#,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: false
test1:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@114.115.138.98:9400:XE
username: TEST
password: test
type: com.alibaba.druid.pool.DruidDataSource
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
# druid 配置: https://github.com/alibaba/druid/wiki/
#初始化链接数
initialSize: 1
minIdle: 1
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,stat用于监控界面,'wall'用于防火墙防御sql注入, slf4j用于druid记录sql日志
filters: stat,slf4j
#,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: false
redis: redis:
host: 127.0.0.1 host: 127.0.0.1
database: 0 database: 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment