Spring Data Jpa 无法配置DataSource:未指定“url”属性,无法为MongoDb配置嵌入式目录

vsdwdz23  于 2023-11-19  发布在  Spring
关注(0)|答案(1)|浏览(118)

我正在使用Sping Boot 2.7.15,JDK 17
在pom.xml中添加Spring Data JPA,mongo-db依赖项用于数据库通信
我看到了一些解决方案,但我没有一个应用程序.yml文件来更改'url'

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.7.15)

2023-08-25 09:00:35.880  INFO 10812 --- [           main] c.lcwd.rating.RatingServiceApplication   : Starting RatingServiceApplication using Java 17.0.8 on pushpendra-ThinkPad-T460 with PID 10812 (/home/pushpendra/Documents/Workspace/Intellij/Microservice/RatingService/target/classes started by pushpendra in /home/pushpendra/Documents/Workspace/Intellij/Microservice/UserService)
2023-08-25 09:00:35.886  INFO 10812 --- [           main] c.lcwd.rating.RatingServiceApplication   : No active profile set, falling back to 1 default profile: "default"
2023-08-25 09:00:37.529  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-08-25 09:00:37.532  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-08-25 09:00:37.559  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 JPA repository interfaces.
2023-08-25 09:00:37.644  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-08-25 09:00:37.644  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2023-08-25 09:00:37.646  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 MongoDB repository interfaces.
2023-08-25 09:00:38.671  INFO 10812 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8083 (http)
2023-08-25 09:00:38.687  INFO 10812 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-08-25 09:00:38.688  INFO 10812 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.79]
2023-08-25 09:00:38.841  INFO 10812 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-08-25 09:00:38.842  INFO 10812 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2835 ms
2023-08-25 09:00:38.964  WARN 10812 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2023-08-25 09:00:38.974  INFO 10812 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-08-25 09:00:38.997  INFO 10812 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-25 09:00:39.020 ERROR 10812 --- [           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).

Process finished with exit code 1
This is my 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.7.15</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lcwd.rating</groupId>
    <artifactId>RatingService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RatingService</name>
    <description>This is Rating Service</description>
    <properties>
       <java.version>17</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-data-mongodb</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
       </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>
             <configuration>
                <excludes>
                   <exclude>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                   </exclude>
                </excludes>
             </configuration>
          </plugin>
       </plugins>
    </build>

</project>

这是我的应用程序。yml

spring:
  data:
    mongodb:
      uri: mongodb://userAdmin:admin@localhost:27017/?authMechanism=DEFAULT
      database: microservices

server:
  port: 8083
package com.lcwd.rating;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RatingServiceApplication {

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

}

的字符串
到目前为止,只有数据库配置添加在项目中,但当我运行项目时,我得到标题中提到的错误。
也可以尝试使用uri,如下所示

mongodb://userAdmin:admin@localhost:27017


也可以尝试使用application.properties而不是application.yml
仔细检查用户名、密码、主机、端口号和数据库是否存在。
重新启动Intellij并再次运行。
检查stackoverflow问题,在谷歌上搜索,参考文章,也尝试使用chatGPT。

jtjikinw

jtjikinw1#

说明:使用mongdb数据库时,不需要spring-boot-starter-data-jpa依赖,因为JPA(Java Persistence API)是管理关系型数据库的规范,MongoDB是一种NoSQL数据库。MongoDB使用与传统关系型数据库不同的数据模型和查询语言。
**解决方案:**只要从pom.xml中删除下面的依赖项,它就会正常工作。

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

字符串

相关问题