spring - 使用 Spring Boot 连接到 MongoDb
问题描述
我想通过 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.xx 你应该使用它。我的 MongoDb 版本是 4.4.4
用户收藏:链接
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
它工作得很好。
谢谢!
解决方案
尝试删除依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
并使用配置
spring.data.mongodb.uri=mongodb://localhost:27017/springtest
spring.data.mongodb.username=mihai
spring.data.mongodb.password=mihai
添加依赖项时,它需要一个您未提供的数据源 url
推荐阅读
- objective-c - Xcode 权利“com.apple.security.inherit”似乎不起作用
- ios - 在一个视图中强制横向
- android - 删除片段中的溢出菜单 3 点
- docker - 在 docker 中配置 Traefik 以路由子文件夹
- spring-boot - Spring Security 5中与feign的服务到服务通信
- c# - 将 System.Int32[] 从 pythonnet.netstandard 转换为 python 脚本中的 numpy 数组
- json - 如何以特定的 json 格式获取 bigquery 的输出
- javascript - 带有 Formik editorState.getCurrentContent 的 Draft.js 不是函数
- python - matplotlib中y轴非常拥挤,如何隐藏y轴值?
- python - 使用获取密码组件后如何将Entry组件重置为普通文本?