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

运行Spring Boot应用程序时出现DataSource错误

白通
2023-03-14

如果我使用第一种方法,即使用@enableAutoConfiguration(exclude={datasourceAutoConfiguration.class})注释我的主应用程序类,则会出现以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

如果我使用第二种方法which,我仍然会得到另一个错误:

Binding to target [Bindable@7c551ad4 type = com.zaxxer.hikari.HikariDataSource, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: driverclassname
    Value: com.mysql.jdbc.Driver
    Origin: "driverClassName" from property source "source"
    Reason: Unable to set value for property driver-class-name

我还尝试了安迪·威尔金森的建议,并补充道

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb

我还尝试提供用户名和pwd(不确定这是否是必需的,因为我不想访问我的数据库),但对我不起作用。如果需要,我也可以提供我的pom配置。

共有1个答案

焦博实
2023-03-14

下面的配置对我来说很好-

应用程序.属性-

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rolb
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.initialize=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

pom.xml-

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

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


        <!-- <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency> -->

         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


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

    </dependencies>
 类似资料: