【发布时间】:2021-03-31 17:10:41
【问题描述】:
我想通过 aplication.properties 文件将 Spring Boot REST Api 项目连接到 MongoDb。为什么?因为这对我来说似乎更容易。
我知道如何与 MySQL 数据库建立这种连接。我已经下载了 MongoDb Compass GUI。
application.properties 文件
spring.data.mongodb.uri=mongodb://localhost:27017/springtest
spring.data.mongodb.username=mihai
spring.data.mongodb.password=mihai
我使用 uri 因为我发现如果 MongoDb 版本 > 3.x.x 你应该使用它。我的 MongoDb 版本是 4.4.4
用户收藏:link
pom 文件:
<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-rest</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.8</version>
</dependency>
<!--##################################################-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.4</version>
</plugin>
</plugins>
</build>
</project>
用户存储库文件:
public interface UserRepository extends MongoRepository<Users, String> {
}
主应用程序文件:
@SpringBootApplication(exclude={SecurityAutoConfiguration.class})
public class AdServicesApplication {
public static void main(String[] args) {
SpringApplication.run(AdServicesApplication.class, args);
}
}
我得到以下错误恍惚:
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 0
根据我的研究,我发现问题可能是由于 application.properties 文件中提供的配置,但我真的不知道如何为 MongoDb 正确编写它。
例如,如果我将 application.properties 内容更改为:
spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory?useSSL=false
spring.datasource.username=mihai
spring.datasource.password=mihai
效果很好。
谢谢!
【问题讨论】:
标签: spring mongodb spring-boot