安装依赖项
确保您的开发环境中安装了 JDK 版本 8 或更高版本。
本指南介绍如何在集成开发环境 (IDE) 中使用 Maven 或 Gradle 添加 MongoDB Java 驱动程序依赖项。我们建议您使用 IntelliJ IDEA 或 Eclipse IDE 等 IDE。IDE 可以更方便地配置 Maven 或 Gradle 以构建和运行项目。
如果您未使用 IDE,请参阅构建 Maven 或创建新的 Gradle 构建以了解有关如何设置项目的更多信息。请参阅 MongoDB Reactive Streams 驱动程序以直接从 Maven 下载驱动程序和依赖项。
安装 Project Reactor 库
本指南使用 Reactor 库中的方法,该库基于 Reactive Streams 规范。
在 IDE 中打开新的 Maven 或 Gradle项目。如果使用Maven,请将以下代码段添加到 pom.xml:
<dependencyManagement> <dependencies> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-bom</artifactId> <version>2023.0.7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
然后,将以下内容添加到pom.xml依赖项列表中:
<dependencies> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> </dependencies>
要使用Gradle安装 Reactor,请参阅 Reactor 项目文档中的“获取 Reactor”。
配置依赖项后,运行依赖项管理器并在 IDE 中刷新项目,确保它们可用于您的项目。
安装Java Reactive Streams驱动程序
在项目中,如果使用Maven,请将以下内容添加到 pom.xml 依赖项列表中:
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-reactivestreams</artifactId> <version>5.2.1</version> </dependency> </dependencies>
如果使用 Gradle,则将以下内容添加到 build.gradle 依赖项列表中:
dependencies { implementation 'org.mongodb:mongodb-driver-reactivestreams:5.2.1' }
配置依赖项后,运行依赖项管理器并在 IDE 中刷新项目,确保它们可用于您的项目。
完成这些步骤后,您就拥有了一个新项目并安装了驱动程序依赖项。
注意
如果您在此步骤中运行问题,请使用此页面右侧或右下角的 Rate this page标签页提交反馈。
您可以使用MongoDB Stack Overflow标签 或MongoDB Reddit Community 寻求一般问题的支持。