Reactive实战(二)Database


前面的 Reactive实战(一)WebFlux介绍了如何操作Mono和Flux,可以看出来这些操作都需要一个源头,那么这个源头是从哪儿来,就是DatabaseClient了

pom.xml

如果是使用了springboot的初始化项目工具,应该很容易得到下面配置,注意postgresql的驱动

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

application.yml

使用下面方式配置数据库链接相关信息

spring:
  cloud:
    config:
      allowOverride: true
  r2dbc:
    url: r2dbc:postgresql://localhost:5432/qbit
    username: postgres
    password: mima

ConnectionFactory


import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static io.r2dbc.spi.ConnectionFactoryOptions.*;

/**
 * @author Qbit
 */
@Configuration
public class DatabaseConfiguration {
    private final String url;
    private final String user;
    private final String password;

    public DatabaseConfiguration(
            @Value("${spring.r2dbc.url}") String url,
            @Value("${spring.r2dbc.username}") String username,
            @Value("${spring.r2dbc.password}") String password) {
        this.url = url;
        this.user = username;
        this.password = password;
    }

    @Bean
    public ConnectionFactory connectionFactory(){
        var endpoint=StringUtils.substringAfterLast(StringUtils.substringBeforeLast(this.url,"/"),"/");
        ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(DRIVER, StringUtils.substringAfterLast(
                        StringUtils.substringBefore(this.url,"://"),":"
                ))
                .option(HOST, StringUtils.substringBefore(endpoint,":"))
                .option(PORT, Integer.parseInt(StringUtils.substringAfter(endpoint,":")))  // optional, defaults to 5432
                .option(USER, user)
                .option(PASSWORD, password)
                .option(DATABASE, StringUtils.substringAfterLast(this.url,"/"))  // optionall
                .build());
        return connectionFactory;
    }
}

使用DatabaseClient

execute

这个是整个Publisher的开始,它通过一个Sql来得到执行结果。另外也提供了select,不过貌似只能单表查询,感觉像是玩具。

map

一般在执行完execute后会使用map来讲结果集转换为java对象。虽然也有as方法,但是没法用。map要求传入一个BiFunction<Row, RowMetadata, T>,这里可能需要自行完成一些类型转换工作。

all & first

all这一方法将数据流转化为Flux来做其他处理
如果数据流只有一条记录或者只需要第一条记录,那么也可以使用first来获取Mono

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值