今天碰到一个问题,由于远程数据库,没有权限操作,而笔者又要建一张表来进行业务开发和调试,于是这时候就想到了多数据源的配置,下面简要记录一下配置过程。
一、导入依赖
笔者用的是mysql+durid的形式所以依赖如下:
<!--导入mysql驱动引擎-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--导入druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
二、在application.properties配置文件中进行配置
#数据源一
spring.datasource.muyichen.url=jdbc:mysql://127.0.0.1:3306/muyichen?characterEncoding=utf-8&useSSL=false
spring.datasource.muyichen.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.muyichen.username=root
spring.datasource.muyichen.password=
#数据源二
spring.datasource.test.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false
spring.datasource.test.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.test.username=root
spring.datasource.test.password=
#mybatis
mybatis.type-aliases-package=com.muyichen.dao
mybatis.mapper-locations=classpath:mapper/*.xml
三、构建属性配置文件
这个文件主要用于从配置文件中获取自定义的属性
@Data
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceConfigProperties {
private DataSourceProperties muyichen;
private DataSourceProperties test;
@Data
public static class DataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
}
}
四、构建多数据源配置文件
笔者在这里没有进行数据源的主从区分,如需要的话在对应的数据源的datasource对象上方添加@Primary注解
/**
* 多数据源配置
*
* @Author muyichen
* @Date 2021年3月17日
*/
@Configuration
@EnableConfigurationProperties(DataSourceConfigProperties.class)
@MapperScan(basePackages = {"com.muyichen.dao"}, sqlSessionFactoryRef = "muyichenSqlSessionFactory")
@MapperScan(basePackages = {"com.test.dao"}, sqlSessionFactoryRef = "testSqlSessionFactory")
public class PrimaryDataSourceConfig {
@Bean(name = "muyichenDataSource")
public DruidDataSource getMuyichenDataSource(DataSourceConfigProperties properties) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(properties.getMuyichen().getDriverClassName());
dataSource.setUrl(properties.getMuyichen().getUrl());
dataSource.setUsername(properties.getMuyichen().getUsername());
dataSource.setPassword(properties.getMuyichen().getPassword());
return dataSource;
}
@Bean(name = "muyichenSqlSessionFactory")
public SqlSessionFactory muyichenSqlSessionFactory(@Qualifier("muyichenDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:muyichen/*.xml"));
return bean.getObject();
}
@Bean(name = "muyichenTransactionManager")
public DataSourceTransactionManager muyichenTransactionManager(@Qualifier("muyichenDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "muyichenSqlSessionTemplate")
public SqlSessionTemplate muyichenSqlSessionTemplate(@Qualifier("muyichenSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "testDataSource")
public DruidDataSource getTestDataSource(DataSourceConfigProperties properties) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(properties.getTest().getDriverClassName());
dataSource.setUrl(properties.getTest().getUrl());
dataSource.setUsername(properties.getTest().getUsername());
dataSource.setPassword(properties.getTest().getPassword());
return dataSource;
}
@Bean(name = "testSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:test/*.xml"));
return bean.getObject();
}
@Bean(name = "testTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "testSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
五、修改启动类中的注解
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
DruidDataSourceAutoConfigure.class})
完成以上五步之后,笔者的多数据源就可以使用了,当然每个数据源对应的是自己配置的扫描路径下的接口,混合调用时不行滴~~