简体   繁体   English

数据源注入到Crud Repository Spring Boot

[英]Data source inject to Crud Repository spring boot

Is there any way to inject or set up data source to Crud Repository? 有什么方法可以将数据源注入或设置到Crud存储库吗? I need one repository and multiple database with same schema. 我需要一个具有相同架构的存储库和多个数据库。 I tried to make hash map with database name and data source and use something like that https://spring.io/blog/2007/01/23/dynamic-datasource-routing/ but it doesn't work 我试图用数据库名称和数据源制作哈希映射,并使用类似https://spring.io/blog/2007/01/23/dynamic-datasource-routing/的方法,但是它不起作用

This solved my problem 这解决了我的问题

@Component
public class RoutingDataSource extends AbstractRoutingDataSource {

    @Autowired
    private DatabaseMap databaseMap;

    @Override
    public void afterPropertiesSet() {
        setTargetDataSources(databaseMap.getSourcesMap());
        setDefaultTargetDataSource(databaseMap.getSourcesMap().get("DEFAULT"));
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DatabaseContextHolder.getDatabaseType();
    }

}

@Configuration
public class DatabaseLoader {
    @Bean
    public DatabaseMap databaseMap() {
    //init databases using DataSourceBuilder
    return databaseMap;
    }
}

I change context same way as here https://spring.io/blog/2007/01/23/dynamic-datasource-routing/ 我使用与此处相同的方式更改上下文https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM