springboot druid 多数据源配置
Posted enots
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot druid 多数据源配置相关的知识,希望对你有一定的参考价值。
由于工作当中,需要配置双数据源进行数据同步,以目录进行区别主副库的编写。这里记录一下,方便大家。
1、pom配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>$druid-spring-boot-starter.version</version>
</dependency>
2、yml配置
spring:
datasource:
druid:
primary:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@192.168.1.1:1521:orcl # 数据库名称
username: test
password: 123456
min-idle: 1 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-active: 8
max-wait: 60000
secondary:
driver-class-name: oracle.jdbc.driver.OracleDriver # 驱动包 todo 光谷库
url: jdbc:oracle:thin:@192.168.1.2:1521:orcl # 数据库名称
username: test
password: inner
min-idle: 1 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-active: 8
max-wait: 60000
3、配置类编写
@Configuration
public class DataSourcePrimaryConfig
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.primary")
@Primary
public DruidDataSource primaryDataSource()
return DruidDataSourceBuilder.create().build();
@Bean
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources(
"classpath*:mybatis/mapping/*.xml"));
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis.cfg.xml"));
return bean.getObject();
@Bean
@Primary
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource)
return new DataSourceTransactionManager(dataSource);
@Bean
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception
return new SqlSessionTemplate(sqlSessionFactory);
@Configuration
public class DataSourceSecondaryConfig
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.secondary")
public DruidDataSource secondaryDataSource()
return DruidDataSourceBuilder.create().build();
@Bean
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources(
"classpath*:mybatis/mapping/secondary/*.xml"));
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis.cfg.xml"));
return bean.getObject();
@Bean
public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource)
return new DataSourceTransactionManager(dataSource);
@Bean
public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception
return new SqlSessionTemplate(sqlSessionFactory);
4、MyBatisMapperScannerConfig 配置
@Configuration
public class MyBatisMapperScannerConfig
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("primarySqlSessionFactory");
mapperScannerConfigurer.setSqlSessionTemplateBeanName("primarySqlSessionTemplate");
mapperScannerConfigurer.setBasePackage("com.onecloud.**.mapper");
//初始化扫描器的相关配置,这里我们要创建一个Mapper的父类
Properties properties = new Properties();
properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "ORACLE");
//主键UUID回写方法执行顺序 默认AFTER
properties.setProperty("ORDER","BEFORE");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
MyBatisSecondaryMapperScannerConfig 参照上面
以上是关于springboot druid 多数据源配置的主要内容,如果未能解决你的问题,请参考以下文章
springboot配置druid多数据源(mysqlpostgresqloracle)
在Springboot2.0项目中使用Druid配置多数据源