在整合了flyway的情况下实现多数据源

1、配置双数据源连接工厂

主数据源
@Configuration
@MapperScan(basePackages = {
          
   xxx.xxx.mapper.main}, sqlSessionFactoryRef = "mainSqlSessionFactory")
public class MainDataSourceConfig {
          
   
	@Autowired
	@Qualifier("mainDb")
	private DataSource dataSource;

	@Bean(name = "mainSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
          
   
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/main/*.xml"));  //加载该路径下的xml文件
		return bean.getObject();
	}

	@Bean(name = "mainDataSource")
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
          
   
		return new SqlSessionTemplate(sqlSessionFactory());
	}
}
副数据源
@Configuration
@MapperScan(basePackages = {
          
   xxx.xxx.mapper.second}, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {
          
   
	@Autowired
	@Qualifier("secondDb")
	private DataSource dataSource;

	@Bean(name = "secondSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
          
   
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/second/*.xml"));  //加载该路径下的xml文件
		return bean.getObject();
	}

	@Bean(name = "secondDataSource")
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
          
   
		return new SqlSessionTemplate(sqlSessionFactory());
	}
}

2、配置数据源的连接信息

@Configuration
public class DataSourceConfig {
          
   

	@Bean(name = "mainDb")
	@ConfigurationProperties(prefix = "spring.datasource.main")
	public DataSource mainDataSource() {
          
   
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name = "secondDb")
	@ConfigurationProperties(prefix = "spring.datasource.second")
	public DataSource secondDataSource() {
          
   
		return DataSourceBuilder.create().build();
	}
}

3、双数据源的配置信息

spring:
  datasource:
    main:
      username: root
      password: 1234
      jdbc-url: jdbc:mysql://localhost:3306/xxx  #key为jdbc-url
      driver-class-name: com.mysql.cj.jdbc.Driver
  
    second:
      username: root
      password: 123456
      jdbc-url: xxxxxxxxxxxxxxxx
      driver-class-name: com.mysql.cj.jdbc.Driver

4、配置flyway信息

spring:
  flyway:
    clean-disabled: false
    locations: classpath:db/migration
    table: xxxxx
    enabled: true
    url:  xxxxx  #主数据源连接地址
    user: root
    password: 1234
配置类
@Configuration
@EnableTransactionManagement
public class FlywayConfig {
          
   
	
	private static final String SQL_LOCATION = "/db/xxxxx";
	private static final String ENCODING = "UTF-8";

   @Autowird
   @Qualifier("secondDb")
	private DataSource dataSource;

   public void migrate() {
          
   
		Flyway flyway = Flyway.configure()
						.dataSource(dataSource)
						.locations(SQL_LOCATION)
						.encoding(ENCODING)
						.baslineOnMigrate(true)
						.table("xxx")
						.load();
		flyway.migrate();
	}
}
经验分享 程序员 微信小程序 职场和发展