若依配置多数据源
1.在application.yml配置新增的数据库octmes:# 从数据源开关/默认关闭enabled: trueurl: jdbc:mysql://localhost:3306/octmes?useUnicode=true&characterEncoding=UTF-8username: rootpassword: root
·
1.在application.yml配置新增的数据库
octmes: # 从数据源开关/默认关闭 enabled: true url: jdbc:mysql://localhost:3306/octmes?useUnicode=true&characterEncoding=UTF-8 username: root password: root
2.在DataSourceType.java 设置数据库名称
/** * 主库 */ MASTER, /** * 从库 */ OCTMES
3.在DruidConfig.java中注册数据源
@Bean @ConfigurationProperties("spring.datasource.druid.octmes") @ConditionalOnProperty(prefix = "spring.datasource.druid.octmes", name = "enabled", havingValue = "true") public DataSource octmesDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource masterDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); setDataSource(targetDataSources, DataSourceType.OCTMES.name(), "octmesDataSource"); return new DynamicDataSource(masterDataSource, targetDataSources); }
更多推荐
已为社区贡献5条内容
所有评论(0)