error:If you want an embedded database (H2, HSQL or Derby)

Posted 劭兮劭兮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了error:If you want an embedded database (H2, HSQL or Derby)相关的知识,希望对你有一定的参考价值。

在这里插入图片描述

错误代码

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-05-30 15:00:35.740 ERROR 17800 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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).

错误原因:

触发了数据源的自动化配置,然而当前项目其实并不需要数据源。究其根源是依赖方提供的API依赖中(本项目创建时导入了web,mysql,jpa 等依赖),多余的依赖触发了该自动化配置的加载。

很多人说是数据库连接的问题,但是我的IDEA连接数据库成功,在这里插入图片描述

错误解决办法

//默认程序入口
@SpringBootApplication
public class BlogApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }

}

//修改为,就是在注解后面加上(exclude = DataSourceAutoConfiguration.class)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class BlogApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }

}

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)作用:

排除自动注入数据源的配置(取消数据库配置),即禁止 springboot 自动注入数据源配置;
DataSourceAutoConfiguration.class 会自动查找 application.yml 或者 properties 文件里的 spring.datasource.* 相关属性并自动配置单数据源;而往往在项目中我们需要配置多数据源,也就是需要自己手动配置数据源;关于单数据源和多数据源的区别参考:单数据源与多数据源(本人小白一个,有点不太懂)

错误介绍

用springboot 写项目时,导入依赖如下:在这里插入图片描述

导入依赖后的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


未导入依赖的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

对比发现:

在这里插入图片描述
触发了数据源的自动化配置,然而当前项目其实并不需要数据源。究其根源是依赖方提供的API依赖中(本项目创建时导入了web,mysql,jpa 等依赖),多余的依赖触发了该自动化配置的加载。

小白一个,有不对的地方,还请指教!

以上是关于error:If you want an embedded database (H2, HSQL or Derby)的主要内容,如果未能解决你的问题,请参考以下文章

Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the

Spring boot 启动错误处理:Action: Consider the following: If you want an embedded database (H2, HSQL or Der

If you want to allow applications containing errors to be published on the server

If you want something ,go gei it,period.

iOSCGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTE(代