SQL数据库

Spring Framework 提供了广泛的支持来使用 SQL 数据库,从使用 JdbcClientJdbcTemplate 的直接 JDBC 访问,到完整的"对象关系映射"技术如 Hibernate。 Spring Data 提供了额外的功能层:直接从接口创建 Repository 实现,并使用约定从方法名生成查询。

Configure a DataSource

Java 的 DataSource 接口提供了处理数据库连接的标准方法。 传统上,DataSource 使用 URL 和一些凭据来建立数据库连接。

有关更高级的示例,请参阅"操作指南"中的 配置自定义 DataSource 部分,通常用于完全控制 DataSource 的配置。

Embedded Database Support

使用内存嵌入式数据库开发应用程序通常很方便。 显然,内存数据库不提供持久化存储。 您需要在应用程序启动时填充数据库,并在应用程序结束时准备丢弃数据。

"操作指南"部分包含一个关于 如何初始化数据库的部分。

Spring Boot 可以自动配置嵌入式 H2、https://hsqldb.org/[HSQL] 和 Derby 数据库。 您不需要提供任何连接 URL。 您只需要包含您想要使用的嵌入式数据库的构建依赖项。 如果类路径上有多个嵌入式数据库,请设置 spring.datasource.embedded-database-connection 配置属性来控制使用哪一个。 将该属性设置为 none 将禁用嵌入式数据库的自动配置。

如果您在测试中使用此功能,您可能会注意到,无论您使用多少个应用程序上下文,整个测试套件都会重用相同的数据库。 如果您想确保每个上下文都有单独的嵌入式数据库,您应该将 spring.datasource.generate-unique-name 设置为 true

例如,典型的 POM 依赖项如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>org.hsqldb</groupId>
	<artifactId>hsqldb</artifactId>
	<scope>runtime</scope>
</dependency>
您需要 spring-jdbc 依赖项才能自动配置嵌入式数据库。 在此示例中,它是通过 spring-boot-starter-data-jpa 传递引入的。
如果出于任何原因,您为嵌入式数据库配置了连接 URL,请注意确保禁用数据库的自动关闭。 如果您使用 H2,您应该使用 DB_CLOSE_ON_EXIT=FALSE 来做到这一点。 如果您使用 HSQLDB,您应该确保不使用 shutdown=true。 禁用数据库的自动关闭可以让 Spring Boot 控制数据库的关闭时间,从而确保在不再需要访问数据库时才关闭它。

Connection to a Production Database

生产数据库连接也可以通过使用池化的 DataSource 来自动配置。

DataSource Configuration

DataSource 配置由 spring.datasource.* 中的外部配置属性控制。 例如,您可能在 application.properties 中声明以下部分:

  • Properties

  • YAML

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring:
  datasource:
    url: "jdbc:mysql://localhost/test"
    username: "dbuser"
    password: "dbpass"
您应该至少通过设置 spring.datasource.url 属性来指定 URL。 否则,Spring Boot 会尝试自动配置嵌入式数据库。
Spring Boot 可以从 URL 推断大多数数据库的 JDBC 驱动程序类。 如果您需要指定特定的类,您可以使用 spring.datasource.driver-class-name 属性。
要创建池化的 DataSource,我们需要能够验证可用的 Driver 类,因此我们在做任何事情之前都会检查它。 换句话说,如果您设置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver,那么该类必须可加载。

有关更多支持的选项,请参阅 DataSourceProperties API 文档。 这些是标准选项,无论 实际实现如何都能工作。 也可以通过使用各自的前缀(spring.datasource.hikari.*spring.datasource.tomcat.*spring.datasource.dbcp2.*spring.datasource.oracleucp.*)来微调特定实现的设置。 有关更多详细信息,请参阅您使用的连接池实现的文档。

例如,如果您使用 Tomcat 连接池,您可以自定义许多其他设置,如下例所示:

  • Properties

  • YAML

spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.test-on-borrow=true
spring:
  datasource:
    tomcat:
      max-wait: 10000
      max-active: 50
      test-on-borrow: true

这将设置池在无连接可用时等待 10000ms 后抛出异常,将最大连接数限制为 50,并在从池中借用连接之前验证连接。

Supported Connection Pools

Spring Boot 使用以下算法选择特定实现:

  1. 我们更喜欢 HikariCP,因为它具有性能和并发性。 如果 HikariCP 可用,我们总是选择它。

  2. 否则,如果 Tomcat 池化的 DataSource 可用,我们使用它。

  3. 否则,如果 Commons DBCP2 可用,我们使用它。

  4. 如果 HikariCP、Tomcat 和 DBCP2 都不可用,并且如果 Oracle UCP 可用,我们使用它。

如果您使用 spring-boot-starter-jdbcspring-boot-starter-data-jpa starters,您会自动获得 HikariCP 依赖项。

您可以通过设置 spring.datasource.type 属性来完全绕过该算法并指定要使用的连接池。 这在 Tomcat 容器中运行应用程序时尤其重要,因为默认提供 tomcat-jdbc

始终可以使用 DataSourceBuilder 手动配置其他连接池。 如果您定义自己的 DataSource bean,则不会发生自动配置。 DataSourceBuilder 支持以下连接池:

Connection to a JNDI DataSource

如果您将 Spring Boot 应用程序部署到应用程序服务器,您可能希望使用应用程序服务器的内置功能来配置和管理 DataSource,并通过 JNDI 访问它。

spring.datasource.jndi-name 属性可以用作 spring.datasource.urlspring.datasource.usernamespring.datasource.password 属性的替代方案,以从特定 JNDI 位置访问 DataSource。 例如,application.properties 中的以下部分显示了如何访问 JBoss AS 定义的 DataSource

  • Properties

  • YAML

spring.datasource.jndi-name=java:jboss/datasources/customers
spring:
  datasource:
    jndi-name: "java:jboss/datasources/customers"

Using JdbcTemplate

Spring 的 JdbcTemplateNamedParameterJdbcTemplate 类是自动配置的,您可以将它们直接自动装配到您自己的 bean 中,如下例所示:

  • Java

  • Kotlin

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final JdbcTemplate jdbcTemplate;

	public MyBean(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void doSomething() {
		this.jdbcTemplate ...
	}

}
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component

@Component
class MyBean(private val jdbcTemplate: JdbcTemplate) {

	fun doSomething() {
		jdbcTemplate.execute("delete from customer")
	}

}

您可以使用 spring.jdbc.template.* 属性自定义模板的某些属性,如下例所示:

  • Properties

  • YAML

spring.jdbc.template.max-rows=500
spring:
  jdbc:
    template:
      max-rows: 500
NamedParameterJdbcTemplate 在后台重用相同的 JdbcTemplate 实例。 如果定义了多个 JdbcTemplate 且不存在主要候选者,则不会自动配置 NamedParameterJdbcTemplate

Using JdbcClient

Spring 的 JdbcClient 基于 NamedParameterJdbcTemplate 的存在而自动配置。 您也可以将其直接注入到您自己的 bean 中,如下例所示:

  • Java

  • Kotlin

import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final JdbcClient jdbcClient;

	public MyBean(JdbcClient jdbcClient) {
		this.jdbcClient = jdbcClient;
	}

	public void doSomething() {
		this.jdbcClient ...
	}

}
import org.springframework.jdbc.core.simple.JdbcClient
import org.springframework.stereotype.Component

@Component
class MyBean(private val jdbcClient: JdbcClient) {

	fun doSomething() {
		jdbcClient.sql("delete from customer").update()
	}

}

如果您依赖自动配置来创建底层的 JdbcTemplate,则使用 spring.jdbc.template.* 属性进行的任何自定义也会在客户端中生效。

JPA and Spring Data JPA

Java Persistence API 是一种标准技术,允许您将对象"映射"到关系数据库。 spring-boot-starter-data-jpa POM 提供了一种快速入门的方式。 它提供以下关键依赖项:

  • Hibernate:最流行的 JPA 实现之一。

  • Spring Data JPA:帮助您实现基于 JPA 的存储库。

  • Spring ORM:来自 Spring Framework 的核心 ORM 支持。

我们在这里不会过多讨论 JPA 或 Spring Data。 您可以按照 spring.io使用 JPA 访问数据指南,并阅读 Spring Data JPAHibernate 参考文档。

Entity Classes

传统上,JPA "Entity" 类在 persistence.xml 文件中指定。 使用 Spring Boot,不需要此文件,而是使用"Entity Scanning"。 默认情况下,会扫描 自动配置包

任何使用 @Entity@Embeddable@MappedSuperclass 注解的类都会被考虑。 典型的实体类类似于以下示例:

  • Java

  • Kotlin

import java.io.Serializable;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

@Entity
public class City implements Serializable {

	@Id
	@GeneratedValue
	private Long id;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private String state;

	// ... additional members, often include @OneToMany mappings

	protected City() {
		// no-args constructor required by JPA spec
		// this one is protected since it should not be used directly
	}

	public City(String name, String state) {
		this.name = name;
		this.state = state;
	}

	public String getName() {
		return this.name;
	}

	public String getState() {
		return this.state;
	}

	// ... etc

}
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import java.io.Serializable

@Entity
class City : Serializable {

	@Id
	@GeneratedValue
	private val id: Long? = null

	@Column(nullable = false)
	var name: String? = null
		private set

	// ... etc
	@Column(nullable = false)
	var state: String? = null
		private set

	// ... additional members, often include @OneToMany mappings

	protected constructor() {
		// no-args constructor required by JPA spec
		// this one is protected since it should not be used directly
	}

	constructor(name: String?, state: String?) {
		this.name = name
		this.state = state
	}

}
您可以使用 @EntityScan 注解自定义实体扫描位置。 请参阅"操作指南"中的 将 @Entity 定义与 Spring 配置分离 部分。

Spring Data JPA Repositories

Spring Data JPA 存储库是您可以定义用于访问数据的接口。 JPA 查询会根据您的方法名自动创建。 例如,CityRepository 接口可能声明 findAllByState(String state) 方法来查找给定州的所有城市。

对于更复杂的查询,您可以使用 Spring Data 的 Query 注解来注解您的方法。

Spring Data 存储库通常从 RepositoryCrudRepository 接口扩展。 如果您使用自动配置,则会搜索 自动配置包中的存储库。

您可以使用 @EnableJpaRepositories 自定义查找存储库的位置。

以下示例显示了典型的 Spring Data 存储库接口定义:

  • Java

  • Kotlin

import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.City;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;

public interface CityRepository extends Repository<City, Long> {

	Page<City> findAll(Pageable pageable);

	City findByNameAndStateAllIgnoringCase(String name, String state);

}
import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.City
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.Repository

interface CityRepository : Repository<City?, Long?> {

	fun findAll(pageable: Pageable?): Page<City?>?

	fun findByNameAndStateAllIgnoringCase(name: String?, state: String?): City?

}

Spring Data JPA 存储库支持三种不同的引导模式:默认、延迟和惰性。 要启用延迟或惰性引导,请将 spring.data.jpa.repositories.bootstrap-mode 属性设置为 deferredlazy。 使用延迟或惰性引导时,自动配置的 EntityManagerFactoryBuilder 将使用上下文的 AsyncTaskExecutor(如果有)作为引导执行器。 如果存在多个,将使用名为 applicationTaskExecutor 的执行器。

使用延迟或惰性引导时,请确保将任何对 JPA 基础设施的访问推迟到应用程序上下文引导阶段之后。 您可以使用 SmartInitializingSingleton 来调用需要 JPA 基础设施的任何初始化。 对于作为 Spring bean 创建的 JPA 组件(如转换器),如果存在任何依赖项,请使用 ObjectProvider 延迟解析依赖项。

我们只是简单介绍了 Spring Data JPA。 有关完整详细信息,请参阅 Spring Data JPA 参考文档

Spring Data Envers Repositories

如果 Spring Data Envers 可用,JPA 存储库会自动配置以支持典型的 Envers 查询。

要使用 Spring Data Envers,请确保您的存储库从 RevisionRepository 扩展,如下例所示:

  • Java

  • Kotlin

import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.Country;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.history.RevisionRepository;

public interface CountryRepository extends RevisionRepository<Country, Long, Integer>, Repository<Country, Long> {

	Page<Country> findAll(Pageable pageable);

}
import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.Country
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.Repository
import org.springframework.data.repository.history.RevisionRepository

interface CountryRepository :
		RevisionRepository<Country?, Long?, Int>,
		Repository<Country?, Long?> {

	fun findAll(pageable: Pageable?): Page<Country?>?

}
有关更多详细信息,请查看 Spring Data Envers 参考文档

Creating and Dropping JPA Databases

默认情况下,仅当您使用嵌入式数据库(H2、HSQL 或 Derby)时,才会自动创建 JPA 数据库。 您可以使用 spring.jpa.* 属性显式配置 JPA 设置。 例如,要创建和删除表,您可以在 application.properties 中添加以下行:

  • Properties

  • YAML

spring.jpa.hibernate.ddl-auto=create-drop
spring:
  jpa:
    hibernate.ddl-auto: "create-drop"
Hibernate 自己的内部属性名称(如果您碰巧记得更好)是 hibernate.hbm2ddl.auto。 您可以使用 spring.jpa.properties.* 设置它以及其他 Hibernate 原生属性(在将它们添加到实体管理器之前会删除前缀)。 以下行显示了为 Hibernate 设置 JPA 属性的示例:
  • Properties

  • YAML

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring:
  jpa:
    properties:
      hibernate:
        "globally_quoted_identifiers": "true"

前面示例中的行将 hibernate.globally_quoted_identifiers 属性的值 true 传递给 Hibernate 实体管理器。

默认情况下,DDL 执行(或验证)会推迟到 ApplicationContext 启动后。

Open EntityManager in View

如果您运行 Web 应用程序,Spring Boot 默认注册 OpenEntityManagerInViewInterceptor 以应用"Open EntityManager in View"模式,以允许在 Web 视图中进行延迟加载。 如果您不希望这种行为,您应该在 application.properties 中将 spring.jpa.open-in-view 设置为 false

Spring Data JDBC

Spring Data 包括对 JDBC 的存储库支持,并将自动为 CrudRepository 上的方法生成 SQL。 对于更高级的查询,提供了 @Query 注解。

当必要的依赖项在类路径上时,Spring Boot 将自动配置 Spring Data 的 JDBC 存储库。 它们可以通过单个依赖项 spring-boot-starter-data-jdbc 添加到您的项目中。 如果需要,您可以通过添加 @EnableJdbcRepositories 注解或 AbstractJdbcConfiguration 子类到您的应用程序来控制 Spring Data JDBC 的配置。

有关 Spring Data JDBC 的完整详细信息,请参阅 参考文档

Using H2’s Web Console

H2 数据库 提供了 基于浏览器的控制台,Spring Boot 可以为您自动配置。 在满足以下条件时,控制台会自动配置:

如果您不使用 Spring Boot 的开发者工具但仍想使用 H2 的控制台,您可以将 spring.h2.console.enabled 属性配置为 true
H2 控制台仅用于开发,因此您应该确保在生产环境中不要将 spring.h2.console.enabled 设置为 true

Changing the H2 Console’s Path

默认情况下,控制台在 /h2-console 可用。 您可以使用 spring.h2.console.path 属性自定义控制台的路径。

Accessing the H2 Console in a Secured Application

H2 控制台使用框架,并且由于它仅用于开发,因此不实现 CSRF 保护措施。 如果您的应用程序使用 Spring Security,您需要配置它以

  • 禁用对控制台请求的 CSRF 保护,

  • 在来自控制台的响应上设置 X-Frame-Options 头为 SAMEORIGIN

有关 CSRFX-Frame-Options 头的更多信息可以在 Spring Security 参考指南中找到。

在简单设置中,可以使用如下所示的 SecurityFilterChain

  • Java

  • Kotlin

import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig;
import org.springframework.security.web.SecurityFilterChain;

@Profile("dev")
@Configuration(proxyBeanMethods = false)
public class DevProfileSecurityConfiguration {

	@Bean
	@Order(Ordered.HIGHEST_PRECEDENCE)
	SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http) throws Exception {
		http.securityMatcher(PathRequest.toH2Console());
		http.authorizeHttpRequests(yourCustomAuthorization());
		http.csrf(CsrfConfigurer::disable);
		http.headers((headers) -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
		return http.build();
	}


}
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.web.SecurityFilterChain

@Profile("dev")
@Configuration(proxyBeanMethods = false)
class DevProfileSecurityConfiguration {

	@Bean
	@Order(Ordered.HIGHEST_PRECEDENCE)
	fun h2ConsoleSecurityFilterChain(http: HttpSecurity): SecurityFilterChain {
		return http.authorizeHttpRequests(yourCustomAuthorization())
			.csrf { csrf -> csrf.disable() }
			.headers { headers -> headers.frameOptions { frameOptions -> frameOptions.sameOrigin() } }
			.build()
	}


}
H2 控制台仅用于开发。 在生产环境中,禁用 CSRF 保护或允许网站使用框架可能会造成严重的安全风险。
即使控制台的路径已自定义,PathRequest.toH2Console() 也会返回正确的请求匹配器。

Using jOOQ

jOOQ Object Oriented Querying (jOOQ) 是 Data Geekery 的一个流行产品,它从您的数据库生成 Java 代码,并让您通过其流畅的 API 构建类型安全的 SQL 查询。 商业版和开源版都可以与 Spring Boot 一起使用。

Code Generation

为了使用 jOOQ 类型安全的查询,您需要从数据库模式生成 Java 类。 您可以按照 jOOQ 用户手册中的说明进行操作。 如果您使用 jooq-codegen-maven 插件并且还使用 spring-boot-starter-parent "parent POM",您可以安全地省略插件的 <version> 标签。 您还可以使用 Spring Boot 定义的版本变量(如 h2.version)来声明插件的数据库依赖项。 以下列表显示了一个示例:

<plugin>
	<groupId>org.jooq</groupId>
	<artifactId>jooq-codegen-maven</artifactId>
	<executions>
		...
	</executions>
	<dependencies>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>${h2.version}</version>
		</dependency>
	</dependencies>
	<configuration>
		<jdbc>
			<driver>org.h2.Driver</driver>
			<url>jdbc:h2:~/yourdatabase</url>
		</jdbc>
		<generator>
			...
		</generator>
	</configuration>
</plugin>

Using DSLContext

jOOQ 提供的流畅 API 通过 DSLContext 接口启动。 Spring Boot 将 DSLContext 自动配置为 Spring Bean 并将其连接到您的应用程序 DataSource。 要使用 DSLContext,您可以注入它,如下例所示:

  • Java

  • Kotlin

import java.util.GregorianCalendar;
import java.util.List;

import org.jooq.DSLContext;

import org.springframework.stereotype.Component;

import static org.springframework.boot.docs.data.sql.jooq.dslcontext.Tables.AUTHOR;

@Component
public class MyBean {

	private final DSLContext create;

	public MyBean(DSLContext dslContext) {
		this.create = dslContext;
	}


}
import org.jooq.DSLContext
import org.springframework.stereotype.Component
import java.util.GregorianCalendar

@Component
class MyBean(private val create: DSLContext) {


}
jOOQ 手册倾向于使用名为 create 的变量来保存 DSLContext

然后,您可以使用 DSLContext 来构建您的查询,如下例所示:

  • Java

  • Kotlin

	public List<GregorianCalendar> authorsBornAfter1980() {
		return this.create.selectFrom(AUTHOR)
			.where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))
			.fetch(AUTHOR.DATE_OF_BIRTH);
	fun authorsBornAfter1980(): List<GregorianCalendar> {
		return create.selectFrom<Tables.TAuthorRecord>(Tables.AUTHOR)
			.where(Tables.AUTHOR?.DATE_OF_BIRTH?.greaterThan(GregorianCalendar(1980, 0, 1)))
			.fetch(Tables.AUTHOR?.DATE_OF_BIRTH)
	}

jOOQ SQL Dialect

除非已配置 spring.jooq.sql-dialect 属性,否则 Spring Boot 会确定用于您的数据源的 SQL 方言。 如果 Spring Boot 无法检测到方言,它将使用 DEFAULT

Spring Boot 只能自动配置 jOOQ 开源版本支持的方言。

Customizing jOOQ

可以通过定义您自己的 DefaultConfigurationCustomizer bean 来实现更高级的自定义,该 bean 将在创建 Configuration @Bean 之前被调用。 这优先于自动配置应用的任何内容。

如果您想完全控制 jOOQ 配置,您也可以创建自己的 Configuration @Bean

Using R2DBC

Reactive Relational Database Connectivity (R2DBC) 项目为关系数据库带来了响应式编程 API。 R2DBC 的 Connection 提供了一种处理非阻塞数据库连接的标准方法。 连接通过使用 ConnectionFactory 提供,类似于带有 jdbc 的 DataSource

ConnectionFactory 配置由 spring.r2dbc.* 中的外部配置属性控制。 例如,您可能在 application.properties 中声明以下部分:

  • Properties

  • YAML

spring.r2dbc.url=r2dbc:postgresql://localhost/test
spring.r2dbc.username=dbuser
spring.r2dbc.password=dbpass
spring:
  r2dbc:
    url: "r2dbc:postgresql://localhost/test"
    username: "dbuser"
    password: "dbpass"
您不需要指定驱动程序类名,因为 Spring Boot 从 R2DBC 的 Connection Factory 发现中获取驱动程序。
至少应该提供 url。 URL 中指定的信息优先于各个属性,即 nameusernamepassword 和池选项。
"操作指南"部分包含一个关于 如何初始化数据库的部分。

要自定义由 ConnectionFactory 创建的连接,即设置您不想(或不能)在中央数据库配置中配置的特定参数,您可以使用 ConnectionFactoryOptionsBuilderCustomizer @Bean。 以下示例显示了如何手动覆盖数据库端口,同时其余选项从应用程序配置中获取:

  • Java

  • Kotlin

import io.r2dbc.spi.ConnectionFactoryOptions;

import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyR2dbcConfiguration {

	@Bean
	public ConnectionFactoryOptionsBuilderCustomizer connectionFactoryPortCustomizer() {
		return (builder) -> builder.option(ConnectionFactoryOptions.PORT, 5432);
	}

}
import io.r2dbc.spi.ConnectionFactoryOptions
import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyR2dbcConfiguration {

	@Bean
	fun connectionFactoryPortCustomizer(): ConnectionFactoryOptionsBuilderCustomizer {
		return ConnectionFactoryOptionsBuilderCustomizer { builder ->
			builder.option(ConnectionFactoryOptions.PORT, 5432)
		}
	}

}

以下示例显示了如何设置一些 PostgreSQL 连接选项:

  • Java

  • Kotlin

import java.util.HashMap;
import java.util.Map;

import io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider;

import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyPostgresR2dbcConfiguration {

	@Bean
	public ConnectionFactoryOptionsBuilderCustomizer postgresCustomizer() {
		Map<String, String> options = new HashMap<>();
		options.put("lock_timeout", "30s");
		options.put("statement_timeout", "60s");
		return (builder) -> builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options);
	}

}
import io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider
import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyPostgresR2dbcConfiguration {

	@Bean
	fun postgresCustomizer(): ConnectionFactoryOptionsBuilderCustomizer {
		val options: MutableMap<String, String> = HashMap()
		options["lock_timeout"] = "30s"
		options["statement_timeout"] = "60s"
		return ConnectionFactoryOptionsBuilderCustomizer { builder ->
			builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options)
		}
	}

}

ConnectionFactory bean 可用时,常规 JDBC DataSource 自动配置会退出。 如果您想保留 JDBC DataSource 自动配置,并且愿意在响应式应用程序中使用阻塞 JDBC API 的风险,请在应用程序中的 @Configuration 类上添加 @Import(DataSourceAutoConfiguration.class) 以重新启用它。

Embedded Database Support

JDBC 支持类似,Spring Boot 可以自动配置嵌入式数据库用于响应式使用。 您不需要提供任何连接 URL。 您只需要包含您想要使用的嵌入式数据库的构建依赖项,如下例所示:

<dependency>
	<groupId>io.r2dbc</groupId>
	<artifactId>r2dbc-h2</artifactId>
	<scope>runtime</scope>
</dependency>

如果您在测试中使用此功能,您可能会注意到,无论您使用多少个应用程序上下文,整个测试套件都会重用相同的数据库。 如果您想确保每个上下文都有单独的嵌入式数据库,您应该将 spring.r2dbc.generate-unique-name 设置为 true

Using DatabaseClient

DatabaseClient bean 是自动配置的,您可以将它直接自动装配到您自己的 bean 中,如下例所示:

  • Java

  • Kotlin

import java.util.Map;

import reactor.core.publisher.Flux;

import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final DatabaseClient databaseClient;

	public MyBean(DatabaseClient databaseClient) {
		this.databaseClient = databaseClient;
	}

	// ...

	public Flux<Map<String, Object>> someMethod() {
		return this.databaseClient.sql("select * from user").fetch().all();
	}

}
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Component
import reactor.core.publisher.Flux

@Component
class MyBean(private val databaseClient: DatabaseClient) {

	// ...

	fun someMethod(): Flux<Map<String, Any>> {
		return databaseClient.sql("select * from user").fetch().all()
	}

}

Spring Data R2DBC Repositories

Spring Data R2DBC 存储库是您可以定义用于访问数据的接口。 查询会根据您的方法名自动创建。 例如,CityRepository 接口可能声明 findAllByState(String state) 方法来查找给定州的所有城市。

对于更复杂的查询,您可以使用 Spring Data 的 @Query 注解来注解您的方法。

Spring Data 存储库通常从 RepositoryCrudRepository 接口扩展。 如果您使用自动配置,则会搜索 自动配置包中的存储库。

以下示例显示了典型的 Spring Data 存储库接口定义:

  • Java

  • Kotlin

import reactor.core.publisher.Mono;

import org.springframework.data.repository.Repository;

public interface CityRepository extends Repository<City, Long> {

	Mono<City> findByNameAndStateAllIgnoringCase(String name, String state);

}
import org.springframework.data.repository.Repository
import reactor.core.publisher.Mono

interface CityRepository : Repository<City?, Long?> {

	fun findByNameAndStateAllIgnoringCase(name: String?, state: String?): Mono<City?>?

}
我们只是简单介绍了 Spring Data R2DBC。有关完整详细信息,请参阅 Spring Data R2DBC 参考文档