由于工作当中,需要配置双数据源进行数据同步,以目录进行区别主副库的编写。这里记录一下,方便大家。
1、pom配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<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 DataSourceConfig {
@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/primary/*.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);
}
@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 primaryMyBatisMapperScannerConfig() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("primarySqlSessionFactory");
mapperScannerConfigurer.setSqlSessionTemplateBeanName("primarySqlSessionTemplate");
mapperScannerConfigurer.setBasePackage("com.onecloud.**.mapper.primary");
buildProperties(mapperScannerConfigurer);
return mapperScannerConfigurer;
}
@Bean
public MapperScannerConfigurer secondaryMyBatisMapperScannerConfig() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("secondarySqlSessionFactory");
mapperScannerConfigurer.setSqlSessionTemplateBeanName("secondarySqlSessionTemplate");
mapperScannerConfigurer.setBasePackage("com.onecloud.**.mapper.secondary");
buildProperties(mapperScannerConfigurer);
return mapperScannerConfigurer;
}
private void buildProperties(MapperScannerConfigurer mapperScannerConfigurer){
//初始化扫描器的相关配置,这里我们要创建一个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);
}
}