在整合了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();
	}
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值