SpringBoot配置多个数据库
Java项目配置多个数据库
·
在开发过程中,可能出现需要连接多个数据库情况,此时就需要在项目中配置多数据库;
或者当前项目需要使用除当前数据以外的数据库数据作为一些基础
基础说明
1.项目使用 SpringBoot
2.数据库为两个 5.7.X的MySQL
3,Maven配置,根据版本自行调整
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
数据源配置
本项目使用yml进行配置
spring:
datasource:
primary:
type: com.alibaba.druid.pool.DruidDataSource
jdbc-url: jdbc:mysql://localhost:3306/locat_a?characterEncoding=utf8
username: root
password: 123
# 数据库连接驱动
driver-class-name: com.mysql.cj.jdbc.Driver
secondary:
type: com.alibaba.druid.pool.DruidDataSource
jdbc-url: jdbc:mysql://localhost:3306/locat_b?characterEncoding=utf8
username: root
password: 456
driver-class-name: com.mysql.cj.jdbc.Driver
配置类
MySQL1配置类
@Configuration
@MapperScan(basePackages = "com.test.mapper.primary", sqlSessionTemplateRef = "MasterSqlSessionTemplate")
public class DruidPrimaryDataSourceConfig {
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "MasterSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/test/mapper/primary/*.xml"));
return bean.getObject();
}
@Bean(name = "MasterTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@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);
}
}
MySQL2配置类
@Configuration
@MapperScan(basePackages = "com.test.mapper.secondary", sqlSessionTemplateRef = "SalverSqlSessionTemplate")
public class DruidSecondaryDataSourceConfig {
@Bean(name = "salverDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource testDataSource() { return DataSourceBuilder.create().build(); }
@Bean(name = "SalverSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("salverDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/test/mapper/secondary/*.xml"));
return bean.getObject();
}
@Bean(name = "SalverTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("salverDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "SalverSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("SalverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
注:
1.相同颜色的方框需要保持相同;
2.椭圆框需要保证数据存在
3.@Primary 注解,只有主数据库可以进行配置,但是不可以两个配置类都是用此注解,出现多个会报错
4.两个数据库对应的DAO需要分开,便于进行配置,也便于查找问题
补充
如果bean名称相同,需要使用别名;
@Mapper
@Component("twoUserMapper")
public interface UserMapper {
}
service层引入数据
@Autowired
@Qualifier("twoUserMapper")
private com.test.mapper.secondary.UserMapper twoUserMapper;

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)