如何为存储库设置特定的数据源? [英] How to set a specific DataSource for a Repository?

查看:23
本文介绍了如何为存储库设置特定的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想创建一个测试环境,通常我想使用测试数据源,但一些 CrudRepository 应该在不同的数据库上运行(生产数据库;只读操作).

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?

公共接口 MyRepository 扩展了 CrudRepository{}

推荐答案

@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.

链接到教程...

这篇关于如何为存储库设置特定的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆