SpringBoot2 启动报错 Failed to auto-configure a DataSource

控制台打印输出

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource 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).
 

解决方法(一)

需要在启动类@SpringBootApplication中添加exclude= {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。这是因为添加了数据库组件,所以autoconfig会去读取数据源配置,而新建的项目还没有配置数据源,所以会导致异常出现。

解决方法(二)

在pom.xml文件中去掉相应的JDBC的导包配置

        <!--<dependency>-->
            <!--<groupId>org.mybatis.spring.boot</groupId>-->
            <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>1.3.2</version>-->
        <!--</dependency>-->

        <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
            <!--<scope>runtime</scope>-->
        <!--</dependency>-->

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

解决方法(三)

在application.yml中添加相应的数据库配置变量

猜你喜欢

转载自blog.csdn.net/qq_42221135/article/details/83927773