springboot同时配置mysql和SqlServer多个数据源
Posted guxiaohai_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot同时配置mysql和SqlServer多个数据源相关的知识,希望对你有一定的参考价值。
一:简介
近期在开发项目中,需要同时从mysql和SqlServer数据库拿取数据。本示例以springboot架子简单介绍下如何同时配置并拿取到不同数据库数据。
二:编码
- application.yml文件配置
spring:
datasource:
#mysql配置
master: #数据源名称
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://ip:port/数据库名称?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: 账户
password: 密码
type: com.alibaba.druid.pool.DruidDataSource
#SqlServer配置
other: #数据源名称
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc-url: jdbc:sqlserver://ip:port;Databasename=数据库名称
username: 账户
password: 密码
min-evictable-idle-time-millis: 1800000 #连接池空闲连接的有效时间 ,设置30分钟
initial-size: 5
max-active: 10
max-wait: 60000
min-idle: 5
- 集成mysql数据库Configuration初始化配置
@Configuration
@MapperScan(basePackages ="mapper路径(如:com.a.b.mapper)", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisDbMasterConfig {
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
//这里需要注意下:spring.datasource.master中,master名称是application.yml数据库配置的数据源名称
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("entity路径(如:com.a.b.entity)");
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("xml文件路径(如:classpath:com/a/b/mapper/*.xml)"));
return factoryBean.getObject();
}
@Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 集成SqlServer数据库Configuration初始化配置
@Configuration
@MapperScan(basePackages = "mapperThree路径(如:com.a.b.mapperThree)", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig {
//这里需要注意下:spring.datasource.other中,other名称是application.yml数据库配置的数据源名称
@Bean(name = "otherDataSource")
@ConfigurationProperties(prefix = "spring.datasource.other")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "otherTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "otherSqlSessionFactory")
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("xml文件路径(如:classpath:com/a/b/mapperThree/*.xml)"));
return factoryBean.getObject();
}
@Bean(name = "otherSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
…后面就是根据初始化类里面的路径写dao层和mapper xml文件sql数据语句了。。。
以上是关于springboot同时配置mysql和SqlServer多个数据源的主要内容,如果未能解决你的问题,请参考以下文章
mysql密码为空,springboot2的yaml配置时如何填写
链接SpringBoot配置SSL同时支持http和https访问
走进JavaWeb技术世界16:极简配置的SpringBoot