【发布时间】:2016-04-28 14:06:44
【问题描述】:
我有一组实体类和存储库定义以及 N 个数据库,所有这些都有架构。如何实例化指向 N 个数据库中的每一个的 N 个存储库。
@Entity
public class Student {
}
public interface StudentRepository extends JpaRepository< Student, Long> {
}
制作与数据库数量一样多的存储库接口副本 (http://www.baeldung.com/spring-data-jpa-multiple-databases) 和使用动态数据源路由 (http://spring.io/blog/2007/01/23/dynamic-datasource-routing/) 是我找到的两个解决方案。
但是,我需要的是类似的东西
@Component
public class Foo {
@Autowired
StudentRepository studentRepositoryForDatabase1;
@Autowired
StudentRepository studentRepositoryForDatabase2;
}
这可行吗?
【问题讨论】:
-
不是重复的,但可能是相关的 - stackoverflow.com/questions/12612288/…。基本上使用单个 repo 并切换数据源(虽然可能不是你想要的)
标签: java spring jpa spring-data spring-data-jpa