当前位置: 首页 > 知识库问答 >
问题:

在Spring Boot(v2.6.6)中,如何从两个数据源中提取并分别访问它们?

李法
2023-03-14

我有两个数据库,我需要同时实例,它们运行在oracle weblogic服务器上,而不是本地。在配置器类中,我指定了源并测试了连接,并且两个数据库都正确拉取数据。(一次使用一个,或与一个作为@Primary)

@Configuration
public class appDatabaseConfiguration {

 @Bean
 @Primary
 public DataSource dataSourceA() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate corpdwJndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBAProperties>");
 }
 
 @Bean
 public DataSource dataSourceB() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate jndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBBProperties>");
 }
}

然而,我无法使用@Qualifier告诉实例化要选择配置中的哪个bean-

@Service
public class DaoImpl implements Dao{
    @Autowired
    @Qualifier("dataSourceA")
    private JdbcTemplate dataSourceA;
    
    @Autowired
    @Qualifier("dataSourceB")
    private JdbcTemplate dataSourceB;
    
    public String getData() {

         resultsA = dataSourceA.queryForObject("SELECT COUNT(*) FROM TABLE", String.class);
                
         resultsB = dataSourceB.queryForObject("SELECT COUNT(*) FROM TABLE", String.class);     
        
         return resultsA + resultsB;
    }
}

不使用@Qualifier时:数据源查询将成功,但 DataSourceB 将失败(表或视图不存在错误)- 因为它正在为两个实例化拉取@Primary Bean

当使用@ qualifier:应用程序错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which 
qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=dataSourceA)} 

我曾尝试向bean添加特定名称,如@bean(“dataSourceA”)、@bean(name=“dataSourceB”),而不是依赖函数名和其他一些语法更改,但没有结果。这里有人有什么见解吗?

注意:即使一个数据源bean注释o

共有2个答案

曹建华
2023-03-14

这是因为配置中定义的bean类型是数据源,而在@服务中,您注入了JdbcTemplate类型的bean。

要使它工作,您应该使用两个< code >数据源创建两个类型为< code>JdbcTemplate的beans,如下所示:

@Configuration
public class appDatabaseConfiguration {

 @Bean
 @Primary
 public DataSource dataSourceA() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate corpdwJndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBAProperties>");
 }
 
 @Bean
 public DataSource dataSourceB() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate jndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBBProperties>");
 }

 @Bean(name="jdbcTemplateA")
 public JdbcTemplate jdbcTemplateA() {
     return new JdbcTemplate(dataSourceA());
 }

 @Bean(name="jdbcTemplateB")
 public JdbcTemplate jdbcTemplateB() {
     return new JdbcTemplate(dataSourceB());
 }
}

然后,使用< code > @ Qualifier(" jdbcTemplateA ")或< code > @ Qualifier(" jdbcTemplateB ")注入您的beans

呼延臻
2023-03-14

您必须创建两个JdbcTemplates,每个模板都配置了不同的数据源,并使用模板的限定符自动连接,例如

@Bean
@Primary
public JdbcTemplate jdbcTemp1(@Qualifier("dataSourceA") DataSource ds) {
    return new JdbcTemplate(ds);
}

@Bean
public JdbcTemplate jdbcTemp2(@Qualifier("dataSourceB") DataSource ds) {
    return new JdbcTemplate(ds);
}

...

@Autowired
@Qualifier("jdbcTemp2")
private JdbcTemplate jdbcTemplate;
 类似资料:
  • 这是一个一般性的参考问题和答案,涵盖了许多永无止境的“如何访问JSON中的数据?”问题。它在这里处理在PHP中解码JSON和访问结果的广泛基础知识。 我有JSON: 如何在PHP中解码并访问结果数据?

  • 我试图生成一个项目列表(从一个名为“pm_project”的自定义表中)和在这些项目上工作的用户。 API:https://my-instance.service-now.com/api/now/table/pm_project?sysparm_fields=number%2cu_resource_list&sysparm_limit=1 数据: 使用一个单独的API(请参见下面的API和数据示例

  • 问题内容: 我有两个表: 这是表1: 这是表2: 现在,我想从这些表中获取数据。在两个表中都相同。 我想拿 和其他表。 请帮我做到这一点。 问题答案: 我假设您在第二个表中有一个命名字段(您没有列出它): 您应该查看有关的MySQL手册,因为这是编写SQL查询的非常基本的部分。您也可以考虑为product_id字段添加索引,以使查询运行更快。

  • 问题内容: 我有2个表:product和cart,我希望结合这2个表并根据特定条件以数组形式显示数据,如下所示: 应显示特定类别下的所有产品,如果特定用户购买了给定产品中的任何产品,则其详细信息也应显示在该产品的前面 我到目前为止所做的代码是 它给出了这样的数组 但是我想要代替上面的数组的最后一个数组是 产品表视图(如您所见,产品表中包含一个productid,在每个productid下最多可以有

  • 如何从df_raw中提取数据(标签)这是Mapstruct? 我正在使用Spark 1.6。我在Spark中通过hivesql从Hive获取数据,然后我得到了一个数据框,但数据框中的一列是Mapstruct,我试图从中提取数据,但失败了,非常希望stackoverflow能给我一些帮助,3Q。 从Hive获取数据后,我得到了一个名为df\u raw的数据帧,模式为: 和df\U raw。显示(3)

  • 我在MySQL中有两个表,如下所示: 表1如下: 表2如下: 现在,我想在一个查询中同时从表1和表2获取数据。 我想从两个表中检索值,其中Student ID等于某物。 学生姓名|年龄|教师姓名 您能告诉我如何从上面提到的两个表中查询值吗。我已经阅读了很多教程,但我不能正确地检索它。我是MySQL新手,请解释清楚。我应该使用什么表联接、并集、内部联接还是外部联接?