springboot 多数据源配置
配置多数据源,需要自己编写对应的DataSource等bean ,且多个数据源,必须设置默认数据库
1.配置默认数据库 (如果有mybatis xml配置文件,需要在
SqlSessionFactory 的
bean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/domain/dao/mybatis-mapper/*Mapper.xml"));
中进行指定
)
@Configuration @MapperScan(basePackages = "com.domain.dao.datasourceA", sqlSessionTemplateRef = "daoSqlSessionTemplate") public class DaoA { @Bean(name = "dao") @ConfigurationProperties(prefix = "spring.datasource.daoA") @Primary public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "daoSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("dao") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); bean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/domain/dao/mybatis-mapper/*Mapper.xml")); //如果不同的数据源,有对应的xml ,使用上面的方法进行设置xml位置 return bean.getObject(); } @Bean(name = "daoTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("dao") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "daoSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("daoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
2.其他数据库配置,多个数据源剩下的都用这种配置
Configuration @MapperScan(basePackages = "com.domain.dao.datasourceB", sqlSessionTemplateRef = "BSqlSessionTemplate") public class DaoB { @Bean(name = "B") @ConfigurationProperties(prefix = "spring.datasource.daoB") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "BSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("B") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); bean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:com/domain/B/mybatis-mapper/*Mapper.xml")); return bean.getObject(); } @Bean(name = "BTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("B") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "BSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("BSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
3数据库连接配置,注意不同的配置,需要对应到Datasource的
@ConfigurationProperties(prefix = "spring.datasource.daoB")
Yaml配置
spring:
datasource:
daoA:
jdbc-url: jdbc:mysql://XXXX:8001/a?Unicode=true&characterEncoding=UTF-8
username: aa
password: aa
driver-class-name: com.mysql.jdbc.Driver
daoB:
jdbc-url: jdbc:mysql://xxxx:8001/b?Unicode=true&characterEncoding=UTF-8
username: bb
password: bb
driver-class-name: com.mysql.jdbc.Driver
Application.properties 配置
@ConfigurationProperties(prefix = "spring.datasource.hikari.daoB")
spring.datasource.hikari.daoA.jdbc-url=jdbc:mysql://127.0.0.1:3306/a?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.hikari.daoA.username=root
spring.datasource.hikari.daoA.password=password
spring.datasource.hikari.daoA.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.daoB.jdbc-url=jdbc:mysql://127.0.0.1:3306/b?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.hikari.daoB.username=root
spring.datasource.hikari.daoB.password=password
spring.datasource.hikari.daoB.driver-class-name=com.mysql.cj.jdbc.Driver