在springboot中有需要连接多个数据源的情况。
首先配置文件application.properties中添加两个数据源连接字符串
mybatis.type-aliases-package=com.murphy.boot.model
spring.datasource.source1.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.source1.url = jdbc:oracle:thin:@localhost:1521/ORCL
spring.datasource.source1.username = DCMSYA
spring.datasource.source1.password = DCMS
#使用Druid数据源
spring.datasource.source1.initialSize=5
# 初始化大小,最小,最大
spring.datasource.source1.minIdle=5
spring.datasource.source1.maxActive= 20
# 配置获取连接等待超时的时间
spring.datasource.source1.maxWait= 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.source1.timeBetweenEvictionRunsMillis= 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.source1.minEvictableIdleTimeMillis= 300000
spring.datasource.source1.validationQuery= select 1 from dual
spring.datasource.source1.testWhileIdle= true
spring.datasource.source1.testOnBorrow= false
spring.datasource.source1.testOnReturn= false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.source1.poolPreparedStatements= true
spring.datasource.source1.maxPoolPreparedStatementPerConnectionSize= 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.source1.filters= stat,slf4j
spring.datasource.source2.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.source2.url = jdbc:oracle:thin:@localhost:1521/ORCL
spring.datasource.source2.username = DCMSYA_LIVE
spring.datasource.source2.password = DCMS
#使用Druid数据源
spring.datasource.source2.initialSize=5
# 初始化大小,最小,最大
spring.datasource.source2.minIdle=5
spring.datasource.source2.maxActive= 20
# 配置获取连接等待超时的时间
spring.datasource.source2.maxWait= 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.source2.timeBetweenEvictionRunsMillis= 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.source2.minEvictableIdleTimeMillis= 300000
spring.datasource.source2.validationQuery= select 1 from dual
spring.datasource.source2.testWhileIdle= true
spring.datasource.source2.testOnBorrow= false
spring.datasource.source2.testOnReturn= false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.source2.poolPreparedStatements= true
spring.datasource.source2.maxPoolPreparedStatementPerConnectionSize= 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.source2.filters= stat,slf4j
添加数据库资源类DataSourceOneConfig
@Configuration
//扫描Mapper basePackages要精确到source1目录便于进行不同数据源的区分
@MapperScan(basePackages = "com.murphy.boot.mapper.source1", sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSourceOneConfig {
@Bean(name = "dataSourceOne")
@ConfigurationProperties(prefix = "spring.datasource.source1")
@Primary //设置主数据源
public DataSource DataSourceOne(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
@Bean
public ServletRegistrationBean druidStatViewServlet() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
registrationBean.addInitParameter("allow", "127.0.0.1"); // IP白名单 (没有配置或者为空,则允许所有访问)
registrationBean.addInitParameter("deny", ""); // IP黑名单 (存在共同时,deny优先于allow)
registrationBean.addInitParameter("loginUsername", "admin");
registrationBean.addInitParameter("loginPassword", "admin");
registrationBean.addInitParameter("resetEnable", "false");
return registrationBean;
}
@Bean
public FilterRegistrationBean druidWebStatViewFilter() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(new WebStatFilter());
registrationBean.addInitParameter("urlPatterns", "/*");
registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
return registrationBean;
}
@Bean(name = "sqlSessionFactoryOne")
@Primary
public SqlSessionFactory sqlSessionFactoryOne(@Qualifier("dataSourceOne") DataSource dataSource)throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "dataSourceTransactionManagerOne")
@Primary
public DataSourceTransactionManager dataSourceTransactionManagerOne(@Qualifier("dataSourceOne") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplateOne")
@Primary
public SqlSessionTemplate sqlSessionTemplateOne(@Qualifier("sqlSessionFactoryOne") SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
本人用的是 tk.mybatis和 druid连接池 jar包:
<!-- druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!--tk通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
同样的,创建DataSourceTwoConifg,设置配置文件为 spring.datasource.source2,并指定对应的mapper文件位置为source2,。
创建Mapper文件,先创建父接口CommnMapper
/**
* @Author Murphy
* @Date 2018-10-18 14:50
*/
public interface CommnMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
在mapper文件夹下创建source1和source2,即两个数据源类中配置的地址。
分别创建对应的mapper,集成CommnMapper
/**
* @Author Murphy
* @Date 2018-10-18 14:53
*/
@Component
@Mapper
public interface CaseDeptDealOneMapper extends CommnMapper<CaseDeptDeal> {
}
分别访问两个文件夹下的mapper就可以访问两个数据库了。
目录结构为:
测试代码
@Autowired
private CaseModelOneMapper userInfoOneMapper;
@Autowired
private CaseModelTwoMapper userInfoTwoMapper;
@Test
public void getUer(){
List<Casehistory> caseModels = userInfoTwoMapper.selectAll();
for (int i = 0; i < caseModels.size(); i++) {
System.out.println(caseModels.get(i).getCaseid());
}
}