簡體   English   中英

Spring 引導 2 - MySQL 數據源

[英]Spring Boot 2 - MySQL DataSource

我在 Spring 引導 2 應用程序中具有以下屬性。

spring.datasource:
   username: user
   password: pwd
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3306/test?enabledTLSProtocols=TLSv1.2&serverTimezone=UTC
   # Hikari CP
   type: com.zaxxer.hikari.HikariDataSource
   hikari.pool-name: ps-demo
   hikari.maximum-pool-size: 20
   hikari.max-lifetime: 1800000
   hikari.idle-timeout: 30000

Pom.xml,

由於某種原因,我需要 mysql 連接器 5.1.38。

<dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <optional>true</optional>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

無法自動接線,

@Named
public class MySqlDatasource {

    @Autowired
    private DataSource dataSource;

    public Connection getConnection() throws SQLException {
    
        return dataSource.getConnection();

    }
}

錯誤:-

"class": "org.springframework.beans.factory.NoSuchBeanDefinitionException",
    "msg": "No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}",
   

設置有什么問題嗎?

pom.xml中有spring-boot-starter-jdbc嗎?

這是依賴項

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

確保您也有MySQL驅動程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM