如何为存储库设置特定的DataSource?

问题描述:

是否可以将特定的 DataSource 分配给 @Repository

Is it possible to assign a specific DataSource to a @Repository?

我想创建一个测试环境,一般我想使用test-datasource,但是一些 CrudRepository 应该在不同的地方运行DB(生产数据库;只读操作)。

I'd like to create a test environment where in general I want to use the test-datasource, but a few CrudRepository should operate on a different DB (the production DB; read-only operations).

我可以告诉spring哪个数据源用于显式存储库吗?

Can I tell spring which datasource to use for a repository explicit?

public interface MyRepository扩展CrudRepository< Customer,Long> {}

@EnableJpaRepositories 是你的问题的答案。根据非正式文档,这应该与 CrudRepository 一起使用。

@EnableJpaRepositories is the answer to your question. This should work with CrudRepository according to the informal documentations.

请参阅此详细教程,了解如何执行此操作。我没有花时间在这里发布代码,因为你可以直接在其中更清楚地提及它。

Refer this detail tutorial on how to do this. I didn't put my effort to post the codes here as you may directly refer it much clearer in it.

指向教程的链接......