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);
    }
Logo

快速构建 Web 应用程序

更多推荐