springboot配置多数据源
最近在做一个换电站的项目需要用到多数据源,配置方法如下
一:在yml文件里先把数据源信息配置好
spring: application: name: base-server datasource: type: com.alibaba.druid.pool.DruidDataSource common: username: user password: pwd jdbc-url: jdbc:mysql://127.0.0.1:3306/database_name?useUnicode=true&useSSL=false&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.jdbc.Driver manager: username: user password: pwd jdbc-url: jdbc:mysql://127.0.0.1:3306/database_name?useUnicode=true&useSSL=false&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.jdbc.Driver
二:配置默认的数据源如下图所示(在bean上加@Primary注解):
package com.polaris_base.config.datasource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * @ClassName DataBase1Config * @Description polaris_manager数据库配置 * @Author 名一 * @Date 2020/9/24 0024 14:20 * @Version 1.0 */ @Configuration @MapperScan(basePackages = "com.polaris_base.mapper.manager", sqlSessionFactoryRef = "managerSqlSessionFactory") public class DataSourceConfigByManager { private Logger logger = LoggerFactory.getLogger(DataSourceConfigByManager.class); // 将这个对象放入Spring容器中 @Bean(name = "managerDataSource") // 表示这个数据源是默认数据源 @Primary // 读取application.properties中的配置参数映射成为一个对象 // prefix表示参数的前缀 @ConfigurationProperties(prefix = "spring.datasource.manager") public DataSource getDateSourceManager(){ DataSource dataSource = DataSourceBuilder.create().build(); logger.info("================================================"); logger.info("DataSourceConfigByManager.dataSource:{}", dataSource); logger.info("================================================"); return dataSource; } @Bean(name = "managerSqlSessionFactory") // 表示这个数据源是默认数据源 @Primary // @Qualifier表示查找Spring容器中名字为test1DataSource的对象 public SqlSessionFactory managerSqlSessionFactory(@Qualifier("managerDataSource") DataSource datasource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/manager/*.xml")); return bean.getObject(); } @Bean("managerSqlSessionTemplate") // 表示这个数据源是默认数据源 @Primary public SqlSessionTemplate managerSqlSessionTemplate(@Qualifier("managerSqlSessionFactory") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } }
三:配置第二个数据源,如需配置更多可按照此实例继续扩展该,实例只展示两个。
package com.polaris_base.config.datasource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; import java.io.IOException; /** * @ClassName DataBase2Config * @Description polaris_currency数据库配置 * @Author 名一 * @Date 2021/3/16 0024 14:21 * @Version 1.0 */ @Configuration @MapperScan(basePackages = "com.polaris_base.mapper.common", sqlSessionFactoryRef = "commonSqlSessionFactory") public class DataSourceConfigByCommon { private Logger logger = LoggerFactory.getLogger(DataSourceConfigByCommon.class); @Bean(name = "commonDataSource") @ConfigurationProperties(prefix = "spring.datasource.common") public DataSource getCurrencyDataSource(){ DataSource dataSource = DataSourceBuilder.create().build(); logger.info("================================================"); logger.info("DataSourceConfigByCommon.dataSource:{}",dataSource); logger.info("================================================"); return dataSource; } @Bean(name = "commonSqlSessionFactory") public SqlSessionFactory currencySqlSessionFactory(@Qualifier("commonDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/common/*.xml")); return bean.getObject(); } @Bean("commonSqlSessionTemplate") public SqlSessionTemplate currencySqlSessionTemplate( @Qualifier("commonSqlSessionFactory") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } @Bean(name = "commonTransactionManager") public PlatformTransactionManager testTransactionManager(@Qualifier("commonDataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource); return dataSourceTransactionManager; } }
到此多数据源就配置完了,需要注意的是配置完多数据源之后每个数据源的mapper文件和xml需要相互隔离开来。有兴趣的小伙伴赶紧试试吧。。。