码迷,mamicode.com
首页 > 编程语言 > 详细

springboot druid 多数据源配置

时间:2019-07-01 10:34:44      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:max   word   ali   数据源   配置   span   连接池   行数据   tsql   

由于工作当中,需要配置双数据源进行数据同步,以目录进行区别主副库的编写。这里记录一下,方便大家。

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 多数据源配置

标签:max   word   ali   数据源   配置   span   连接池   行数据   tsql   

原文地址:https://www.cnblogs.com/enots/p/11112533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!