入門

啟動工作環境的一種簡單方法是在 Spring Tools 中或從 Spring Initializr 建立一個基於 Spring 的專案。

首先,您需要設定一個正在執行的資料庫伺服器。請參閱您的供應商文件,瞭解如何配置您的資料庫以進行 R2DBC 訪問。

要求

Spring Data R2DBC 需要 Spring Framework 7.0.0 及更高版本。

在資料庫方面,Spring Data R2DBC 需要一個 驅動程式 來抽象特定於供應商的 SQL 功能。Spring Data R2DBC 直接支援以下資料庫:

如果您使用不同的資料庫,您的應用程式將無法啟動。方言 部分包含有關在這種情況下如何操作的更多詳細資訊。

Hello World

在 STS 中建立 Spring 專案

  1. 轉到檔案 → 新建 → Spring 模板專案 → 簡單 Spring 實用工具專案,並在提示時按“是”。然後輸入專案和包名,例如 org.spring.r2dbc.example

  2. 將以下內容新增到 pom.xml 檔案的 dependencies 元素中

  3. 將以下內容新增到 pom.xml 檔案的 dependencies 元素中

    <dependencies>
    
        <!-- other dependency elements omitted -->
    
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
            <version>4.0.0</version>
        </dependency>
    
        <!-- a R2DBC driver -->
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-h2</artifactId>
            <version>x.y.z</version>
        </dependency>
    
    </dependencies>
  4. 將 pom.xml 中 Spring 的版本更改為

    <spring.version>7.0.0</spring.version>
  5. 將以下 Spring Milestone 倉庫位置新增到您的 pom.xml 中,使其與您的 <dependencies/> 元素處於同一級別

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Maven MILESTONE Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

該倉庫也可以在此處瀏覽

您可能還希望將日誌級別設定為 DEBUG 以檢視一些額外資訊。為此,請編輯 application.properties 檔案,使其包含以下內容:

logging.level.org.springframework.r2dbc=DEBUG

然後,您可以,例如,建立一個 Person 類進行持久化,如下所示:

public class Person {

	private final String id;
	private final String name;
	private final int age;

	public Person(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

接下來,您需要在資料庫中建立表結構,如下所示:

CREATE TABLE person(
    id VARCHAR(255) PRIMARY KEY,
    name VARCHAR(255),
    age INT
);

您還需要一個主應用程式來執行,如下所示:

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import reactor.test.StepVerifier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;

public class R2dbcApp {

  private static final Log log = LogFactory.getLog(R2dbcApp.class);

  public static void main(String[] args) {

    ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");

    R2dbcEntityTemplate template = new R2dbcEntityTemplate(connectionFactory);

    template.getDatabaseClient().sql("CREATE TABLE person" +
        "(id VARCHAR(255) PRIMARY KEY," +
        "name VARCHAR(255)," +
        "age INT)")
      .fetch()
      .rowsUpdated()
      .as(StepVerifier::create)
      .expectNextCount(1)
      .verifyComplete();

    template.insert(Person.class)
      .using(new Person("joe", "Joe", 34))
      .as(StepVerifier::create)
      .expectNextCount(1)
      .verifyComplete();

    template.select(Person.class)
      .first()
      .doOnNext(it -> log.info(it))
      .as(StepVerifier::create)
      .expectNextCount(1)
      .verifyComplete();
  }
}

當您執行主程式時,前面的示例會產生類似於以下內容的輸出:

2018-11-28 10:47:03,893 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 310 - Executing SQL statement [CREATE TABLE person(
     id VARCHAR(255) PRIMARY KEY,
     name VARCHAR(255),
     age INT
 )]
2018-11-28 10:47:04,074 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 908 - Executing SQL statement [INSERT INTO person (id, name, age) VALUES($1, $2, $3)]
2018-11-28 10:47:04,092 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 575 - Executing SQL statement [SELECT id, name, age FROM person]
2018-11-28 10:47:04,436  INFO        org.spring.r2dbc.example.R2dbcApp:  43 - Person [id='joe', name='Joe', age=34]

即使在這個簡單的例子中,也有一些值得注意的地方:

  • 您可以使用標準的 io.r2dbc.spi.ConnectionFactory 物件建立 Spring Data R2DBC 中核心輔助類 (R2dbcEntityTemplate) 的例項。

  • 對映器針對標準 POJO 物件工作,無需任何額外的元資料(儘管您可以選擇提供該資訊 — 請參閱此處)。

  • 對映約定可以使用欄位訪問。請注意,Person 類只有 getter 方法。

  • 如果建構函式引數名稱與儲存行的列名稱匹配,則它們用於例項化物件。

示例倉庫

有一個 GitHub 倉庫,其中包含幾個示例,您可以下載並試用以瞭解該庫的工作原理。

使用 Spring 連線到關係資料庫

使用關係資料庫和 Spring 時,首要任務之一是使用 IoC 容器建立一個 io.r2dbc.spi.ConnectionFactory 物件。請確保使用 支援的資料庫和驅動程式

使用 Java 配置註冊 ConnectionFactory 例項

以下示例展示瞭如何使用基於 Java 的 bean 元資料來註冊 io.r2dbc.spi.ConnectionFactory 例項:

使用 Java 配置註冊 io.r2dbc.spi.ConnectionFactory 物件
@Configuration
public class ApplicationConfiguration extends AbstractR2dbcConfiguration {

    @Override
    @Bean
    public ConnectionFactory connectionFactory() {
        return …
    }
}

這種方法允許您使用標準的 io.r2dbc.spi.ConnectionFactory 例項,容器使用 Spring 的 AbstractR2dbcConfiguration。與直接註冊 ConnectionFactory 例項相比,配置支援的額外優勢還在於為容器提供了一個 ExceptionTranslator 實現,該實現將 R2DBC 異常轉換為 Spring DataAccessException 層次結構中的異常,用於使用 @Repository 註解的資料訪問類。該層次結構和 @Repository 的使用在 Spring 的 DAO 支援功能 中有描述。

AbstractR2dbcConfiguration 還註冊了 DatabaseClient,這是資料庫互動和 Repository 實現所必需的。

方言

Spring Data R2DBC 使用 R2dbcDialect 來封裝特定於資料庫或其驅動程式的行為。Spring Data R2DBC 透過檢查 ConnectionFactory 並相應地選擇適當的資料庫方言來響應資料庫的特定性。如果您使用的資料庫沒有可用的方言,那麼您的應用程式將無法啟動。在這種情況下,您必須要求您的供應商提供 R2dbcDialect 實現。或者,您可以實現自己的 R2dbcDialect

方言由 DialectResolverConnectionFactory 解析,通常透過檢查 ConnectionFactoryMetadata。+ 您可以透過在 META-INF/spring.factories 中註冊實現 org.springframework.data.r2dbc.dialect.DialectResolver$R2dbcDialectProvider 的類來讓 Spring 自動發現您的 R2dbcDialectDialectResolver 使用 Spring 的 SpringFactoriesLoader 從類路徑中發現方言提供程式實現。為此

  1. 實現您自己的 Dialect

  2. 實現一個返回 DialectR2dbcDialectProvider

  3. 透過在 META-INF 下建立一個 spring.factories 資源並新增一行來完成註冊:
    org.springframework.data.r2dbc.dialect.DialectResolver$R2dbcDialectProvider=<您的 R2dbcDialectProvider 的完全限定名>

© . This site is unofficial and not affiliated with VMware.