<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring Boot MongoDB 反應式教程 > 原文: [http://zetcode.com/springboot/mongodbreactive/](http://zetcode.com/springboot/mongodbreactive/) Spring Boot MongoDB 反應式教程展示了如何在 Spring Boot 應用中使用 MongoDB 進行反應式編程。 ## MongoDB MongoDB 是 NoSQL 跨平臺的面向文檔的數據庫。 它是可用的最受歡迎的數據庫之一。 MongoDB 由 MongoDB Inc. 開發,并作為免費和開源軟件發布。 Spring Data MongoDB 項目提供了與 MongoDB 文檔數據庫的集成。 ## 反應式編程 反應式編程是一種編程范例,它是函數式的,基于事件的,非阻塞的,異步的,并且以數據流處理為中心。 術語反應式來自以下事實:我們對諸如鼠標單擊或 I/O 事件之類的更改做出反應。 當我們處理大量流數據時,響應式應用可以更好地擴展,并且效率更高。 反應性應用是非阻塞的; 他們沒有使用資源等待流程完成。 在構建反應式應用時,我們需要它一直到整個數據庫都是反應式的。 我們需要使用支持反應式編程的數據庫。 MongoDB 是具有響應式支持的數據庫。 響應式應用實現基于事件的模型,在該模型中將數據推送到使用者。 數據的使用者稱為訂閱者,因為它訂閱了發布者,后者發布異步數據流。 ## Spring 反應式 Spring 反應式是一個反應式庫,用于根據反應式流規范在 JVM 上構建非阻塞應用。 反應式項目提供兩種類型的發布者:`Mono`和`Flux`。 `Flux`是產生 0 到 N 個值的發布者。 返回多個元素的操作使用此類型。 `Mono`是產生 0 到 1 值的發布者。 它用于返回單個元素的操作。 ## Spring Boot MongoDB 反應式示例 在以下應用中,我們對 MongoDB 數據庫使用反應式編程。 > **注意**:默認情況下,沒有任何特定配置,Spring Boot 會嘗試使用`test`數據庫名稱連接到本地托管的 MongoDB 實例。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ │ MyRunner.java │ │ ├───model │ │ │ City.java │ │ ├───repository │ │ │ CityRepository.java │ │ └───service │ │ CityService.java │ │ ICityService.java │ └───resources │ application.properties └───test └───java ``` 這是 Spring 應用的項目結構。 `pom.xml` ```java <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springbootmongodbreactive</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven `pom.xml`文件。 `spring-boot-starter-data-mongodb-reactive`是用于使用 MongoDB 面向文檔的數據庫和 Spring Data MongoDB 反應式的 Spring Boot 入門程序。 `resources/application.properties` ```java spring.main.banner-mode=off ``` 在`application.properties`中,我們關閉 Spring Boot 橫幅并設置日志記錄屬性。 默認情況下,Spring Boot 會嘗試使用測試數據庫連接到 MongoDB 的本地托管實例。 ```java # mongodb spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=testdb ``` 如果要配置 MongoDB,可以設置相應的屬性。 `com/zetcode/model/City.java` ```java package com.zetcode.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import java.util.Objects; @Document(value="cities") public class City { @Id private String id; private String name; private int population; public City() { } public City(String name, int population) { this.name = name; this.population = population; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } @Override public int hashCode() { int hash = 7; hash = 79 * hash + Objects.hashCode(this.id); hash = 79 * hash + Objects.hashCode(this.name); hash = 79 * hash + this.population; return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final City other = (City) obj; if (this.population != other.population) { return false; } if (!Objects.equals(this.name, other.name)) { return false; } return Objects.equals(this.id, other.id); } @Override public String toString() { var builder = new StringBuilder(); builder.append("City{id=").append(id).append(", name=") .append(name).append(", population=") .append(population).append("}"); return builder.toString(); } } ``` 這是`City` bean,具有三個屬性:`id`,`name`和`population`。 ```java @Document(value="cities") public class City { ``` Bean 用可選的`@Document`注解修飾。 ```java @Id private String id; ``` `id`用`@Id`注解修飾。 Spring 會為一個新生成的城市對象自動生成一個新的 ID。 `com/zetcode/repository/CityRepository.java` ```java package com.zetcode.repository; import com.zetcode.model.City; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; @Configuration public interface CityRepository extends ReactiveMongoRepository<City, String> { } ``` 通過從`ReactiveMongoRepository`擴展,我們有了一個反應性 MongoDB 存儲庫。 `com/zetcode/service/ICityService.java` ```java package com.zetcode.service; import com.zetcode.model.City; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.List; public interface ICityService { Mono<City> insert(City city); Flux<City> saveAll(List<City> cities); Mono<City> findById(String id); Flux<City> findAll(); Mono<Void> deleteAll(); } ``` `ICityService`包含五種契約方法。 `com/zetcode/MyRunner.java` ```java package com.zetcode; import com.zetcode.model.City; import com.zetcode.service.CityService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.List; @Component public class MyRunner implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(MyRunner.class); @Autowired private CityService cityService; @Override public void run(String... args) throws Exception { logger.info("Creating cities"); var cities = List.of(new City("Bratislava", 432000), new City("Budapest", 1759000), new City("Prague", 1280000), new City("Warsaw", 1748000)); Mono<Void> one = cityService.deleteAll(); Flux<City> two = cityService.saveAll(cities); Flux<City> three = cityService.findAll(); three.subscribe(city -> logger.info("{}", city)); Mono<Void> all = Mono.when(one, two, three); all.block(); } } ``` 我們有一個命令行運行器。 在其`run()`方法中,我們使用反應式編程訪問 MongoDB。 ```java Mono<Void> one = cityService.deleteAll(); ``` 如果集合中有城市,我們將刪除所有城市。 ```java Flux<City> two = cityService.saveAll(cities); ``` 我們保存城市列表。 ```java Flux<City> three = cityService.findAll(); three.subscribe(System.out::println); ``` 我們從集合中找到所有城市。 我們使用`subscribe()`方法訂閱發布者,并將檢索到的城市打印到終端。 ```java Mono<Void> all = Mono.when(one, two, three); ``` 使用`Mono.when()`,我們將三個發布者匯總到一個新的`Mono`中,當所有來源完成后,這些`Mono`將實現。 ```java all.block(); ``` 使用`block()`,我們將觸發所有三個操作并等待完成。 由于我們具有控制臺應用,因此我們引入了阻塞操作,以便在終端上獲得結果。 `subscribe()`方法開始工作并立即返回。 我們不能保證在應用的其他部分運行操作完成。 `block()`是一項阻止操作:它觸發該操作并等待其完成。 > **注意**:通常,我們很少在應用中使用阻塞調用。 控制臺應用中的操作是少數例外之一。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 這段代碼設置了 Spring Boot 應用。 在本教程中,我們學習了如何在 Spring Boot 應用中使用反應式編程模型對 MongoDB 進行編程。 列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看