在Spring引导中配置数据源时出错

问题描述 投票:0回答:1

我想在一个弹簧启动应用程序中配置两个数据库&最初我正在尝试使用一个但是收到错误。


@Configuration
public class DatabaseConfiguration {
    @Bean(name = "user")
    @ConfigurationProperties(prefix = "spring.user")
    public DataSource createProductServiceDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcuser")
    @Autowired
    public JdbcTemplate createJdbcTemplate_ProductService(@Qualifier("user") DataSource productServiceDS) {
        return new JdbcTemplate(productServiceDS);
    }
}
dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
spring.jpa.hibernate.ddl-auto=none
spring.user.url=jdbc:mysql://ip address:port/testdb
spring.user.username=username
spring.user.password=password
server.port=port
spring.user.driver-class-name=com.mysql.jdbc.Driver
@RestController
@Qualifier("jdbcuser")
public class Failed_Status {


    @Autowired
        JdbcTemplate jdbcTemplate;
    @GetMapping("/select")
    public List<User> getList()
    {
        String sql="select * from Customerinfo where status like 'F%'";
        List<User> u=new ArrayList<User>();

        u= jdbcTemplate.query(sql,new UserRowMapper());
        System.out.println(u);
        return u;


    }

}
Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded data source could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

由于我是Spring新手,我无法找到如何使用多个数据库。请告诉我成功运行程序需要做哪些更改?

java spring spring-boot jdbctemplate multiple-databases
1个回答
0
投票

尝试使用@Primary标记主数据源,以便JDBC自动配置功能知道选择这个。当然,当您使用多个数据源时,您将需要自然地:

@Bean(name = "user")
@Primary
@ConfigurationProperties(prefix = "spring.user")
public DataSource createProductServiceDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "user2")
@ConfigurationProperties(prefix = "spring.user2")
public DataSource createProductServiceDataSource2() {
    return DataSourceBuilder.create().build();
}

推荐问答