业务需要,配置不同账号访问不同数据库。项目原来使用的单数据源+MyBatis-plus。
按下面代码整合一切正常。直接上代码:
排除SpringBoot的自动配置类DataSourceAutoConfiguration
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class
})
在properties中配置多数据源连接信息,名称为consultant和dw
spring.datasource.consultant.jdbc-url=jdbc:mysql://xxxx:3306/consultant?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
spring.datasource.consultant.username=dev_w
spring.datasource.consultant.password=D83GskdgdseGDDev
spring.datasource.consultant.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dw.jdbc-url=jdbc:mysql://xxxx:3306/up_dw?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
spring.datasource.dw.username=bs
spring.datasource.dw.password=oDZjmrircB+EYoITyEnBPcRp
spring.datasource.dw.driver-class-name=com.mysql.cj.jdbc.Driver
创建数据库配置类
配置2个数据源
@Configuration
public class DataSourceConfig {
@Bean(name = "consultantDataSource")
@ConfigurationProperties(prefix = "spring.datasource.consultant")
public DataSource consultantDbDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dwDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dw")
public DataSource dwDbDataSource() {
return DataSourceBuilder.create().build();
}
}
dw库SqlSessionFactory,注意扫描不同的mapper接口包和对应的xml文件包
@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.dw.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryDw")
public class DwDBConfig {
@Autowired
@Qualifier("dwDataSource")
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryDw() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dw/*.xml"));
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateDw() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryDw());
}
}
consultant库SqlSessionFactory
@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.communication.mapper"},
sqlSessionFactoryRef = "sqlSessionFactoryConsultant")
public class ConsultantDBConfig {
@Autowired
@Qualifier("consultantDataSource")
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryConsultant() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/consultant/*.xml"));
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateConsultant() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryConsultant());
}
}
整合完毕。