Commit c266a268 by s_guodong

测试多数据源

parent 880890a7
......@@ -79,15 +79,11 @@
<groupId>com.brilliance</groupId>
<artifactId>jrxx-bean</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/jrxx-bean.jar</systemPath>
</dependency>
<dependency>
<groupId>com.brilliance</groupId>
<artifactId>jrxx</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/jrxx.jar</systemPath>
</dependency>
</dependencies>
......
......@@ -45,6 +45,50 @@ spring:
password:
datasource1:
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
datasource2:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
#driverClassName: com.mysql.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
envConfig:
rootPath: workRoot
cache:
......@@ -53,4 +97,6 @@ envConfig:
#缓存失效时间(秒)
expireAfterAccess: 3600
#序列化方式
serializer: kryo
\ No newline at end of file
serializer: kryo
package com.brilliance.mda.support.mybatis;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.HashMap;
/**
* @Description 注册数据源头
* @Author s_guodong
* @Date 2023/5/24
*/
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DataSourceConfiguration {
/**
* 数据源1
*/
@Bean(name = "datasource1")
@Qualifier("datasource1")
@ConfigurationProperties(prefix = "spring.datasource1")
public DruidDataSource datasource1() {
return new DruidDataSource();
}
/**
* 数据源2
*/
@Bean(name = "datasource2")
@Qualifier("datasource2")
@ConfigurationProperties(prefix = "spring.datasource2")
public DruidDataSource datasource2() {
return new DruidDataSource();
}
@Bean(name = "dynamicDataSource")
@Primary
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.myMap = new HashMap<>(2);
dynamicDataSource.myMap.put("datasource1", datasource1());
dynamicDataSource.myMap.put("datasource2", datasource2());
dynamicDataSource.setTargetDataSources(dynamicDataSource.myMap);
dynamicDataSource.setDefaultTargetDataSource(datasource1());
DynamicDataSourceContextHolder.dataSourceIds.addAll(dynamicDataSource.myMap.keySet());
return dynamicDataSource;
}
}
package com.brilliance.mda.support.mybatis;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import java.util.Map;
/**
* @Description
* @Author s_guodong
* @Date 2023/5/24
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
public Map<Object, Object> myMap = null;
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDataSourceType();
}
}
package com.brilliance.mda.support.mybatis;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Author s_guodong
* @Date 2023/5/24
*/
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static List<Object> dataSourceIds = new ArrayList<Object>();
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
public static String getDataSourceType() {
return contextHolder.get();
}
public static void clearDataSourceType() {
contextHolder.remove();
}
public static boolean containsDataSource(String dataSourceId) {
return dataSourceIds.contains(dataSourceId);
}
}
package com.brilliance.mda.support.mybatis.dync.mapper;
import com.brilliance.mda.support.mybatis.DynamicDataSourceContextHolder;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -16,6 +17,7 @@ public class DbExecuteMapper {
final String FNAME = this.getClass().getName();
public List<Map<String,Object>> dyncReadForMap(Map<String,Object> params){
DynamicDataSourceContextHolder.setDataSourceType("datasource2");
return sqlSessionTemplate.selectList(FNAME+".dyncReadForMap",params);
}
public int dyncUpdateForMap(Map<String,Object> params){
......
......@@ -86,9 +86,7 @@
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.2</version>
<scope>system</scope>
<systemPath>${basedir}/../lib/ojdbc6-12.1.0.2.jar</systemPath>
<version>11.2.0.3</version>
</dependency>
</dependencies>
......
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