springboot配置多数据源

今天碰到一个问题,由于远程数据库,没有权限操作,而笔者又要建一张表来进行业务开发和调试,于是这时候就想到了多数据源的配置,下面简要记录一下配置过程。


一、导入依赖

笔者用的是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})

完成以上五步之后,笔者的多数据源就可以使用了,当然每个数据源对应的是自己配置的扫描路径下的接口,混合调用时不行滴~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘丶沐逸尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值