java 如何解决“配置数据源失败:未指定“url”属性,无法配置嵌入式数据源”

0md85ypi  于 2022-12-25  发布在  Java
关注(0)|答案(3)|浏览(304)

我正在尝试实现从currrencyexchange模块到currencyconversion的开放式调用,但我被这个错误卡住了。有人能帮我吗?我已经检查了其他答案,并试图添加#spring.datasource.driver-class-name=com.mysql.jdbc.Driver,但没有工作。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-09-22 21:07:32.787 ERROR [currency-conversion-service,,] 261173 --- [           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 (the profiles dev are currently active).

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.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hashedin</groupId>
    <artifactId>currency-conversion-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>currency-conversion-service</name>
    <description>Currency Exchange Service template for HU SDE 22.3.2 Advanced Java track</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.1</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

应用特性

spring.application.name=currency-conversion-service
    #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.profiles.active=dev
    
server.port=8081
spring.cloud.config.enabled=false
eureka.client.serviceUrl.defaultZone=https://hu-22-java-adv-mastereureka-server-urtjok3rza-wl.a.run.app/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=https://hu-22-java-adv-mastereureka-server-urtjok3rza-wl.a.run.app

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
yeotifhr

yeotifhr1#

如果你想使用mysql,你必须添加mysql驱动程序:

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

zfycwa2u2#

spring-boot-starter-data-jpa工件可以与H2嵌入式数据库集成,在POM.xml中添加以下依赖项:

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

然后,在src/main/resources文件夹中添加两个脚本文件:

  • schema.sql,用于创建数据库
  • data.sql,用于填充数据库

注意,使用springdataJPA,您可以获得嵌入式数据库的默认配置,而不需要将数据库配置添加到application.properties文件中。

pengsaosao

pengsaosao3#

Add this version of derby in pom.xml :

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>runtime</scope>
</dependency>

After making changes in pom.xml, force update the maven project

And add the following in applications.properties file :

#JPA
  spring.jpa.generate-ddl=true
  spring.jpa.hibernate.ddl-auto=update
  spring.jpa.database=default
  spring.jpa.show-sql=true
#DATASOURCE
 spring.datasource.continue-on-error=false
 spring.datasource.generate-unique-name=false
 spring.datasource.username=app

Maybe problem with the earlier default derby jar dependency

It worked after making the above changes

相关问题