R2DBC-响应式数据库

2024-01-21 13:44
文章标签 数据库 响应 r2dbc

本文主要是介绍R2DBC-响应式数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简单查询

基于全异步,响应式,消息驱动
用法:
1.导入驱动:导入连接池(r2dbc-pool),导入驱动(r2dbc-mysql)
2. 使用驱动提供的api操作
pom.xml

<properties><r2dbc-mysql.version>1.0.5</r2dbc-mysql.version>
</properties><dependencies><dependency><groupId>io.asyncer</groupId><artifactId>r2dbc-mysql</artifactId><version>${r2dbc-mysql.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

单元测试

    @Testpublic void testGetConnection() throws Exception{//1.获取连接工厂MySqlConnectionConfiguration config = MySqlConnectionConfiguration.builder().host("123.57.132.54").username("root").password("zyl000419").database("index_demo").build();MySqlConnectionFactory factory = MySqlConnectionFactory.from(config);//2.获取到连接,发送sqlMono.from(factory.create()).flatMapMany(connection ->connection.createStatement("SELECT * FROM t_author WHERE id = ?id").bind("id",1L).execute())//每一个连接会产生很多数据(result).flatMap(result -> {return result.map(readable -> {Long id = readable.get("id", Long.class);String name = readable.get("name", String.class);return new Author(id,name);});}).subscribe(System.out::println);System.in.read();}

参数赋值
在这里插入图片描述

spring data r2dbc-整合与自动配置

SpringBoot对r2dbc自动配置
R2dbcAutoConfiguration:主要配置连接工厂,连接池
R2dbcDataAutoConfiguration:
r2dbcEntityTemplate:操作数据库的响应式客户端,提供crud Api数据类型映射关系,转换器
自定义R2dbcCustomConversions转换器组件
数据类型 int -> integer; varchar->string
R2dbcRepositoriesAutoConfiguration:开启springboot声明式接口方式的crud
spring data 提供了基础的crud接口,不用写任何实现的情况下,可以直接具有crud功能
R2dbcTransactionManager:事物管理

导入相关依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-r2dbc</artifactId></dependency>

编写application.yml配置
调整日志级别,打印sql语句

spring:r2dbc:url: r2dbc:mysql://your_host:3306username: rootpassword: your_passwordname: your_database
logging:level:org.springframework.r2dbc: debug

database client & r2dbcEntityTemplate api

创建数据库映射实体

@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("t_author")
public class Author {private Long id;private String name;}

R2dbcEntityTemplate: crudApi,join操作不好做

    @Autowiredprivate R2dbcEntityTemplate template;@Testpublic void testR2dbcEntityTemplate() throws Exception{//1.构造查询条件Criteria criteria = Criteria.empty().and("id").is(1L).and("name").is("zyl");//2.封装为查询对象Query query = Query.query(criteria);template.select(query, Author.class).subscribe(System.out::println);System.in.read();}

DatabaseClient:数据库客户端,贴近底层,join操作好做

    @Autowiredprivate DatabaseClient databaseClient;@Testpublic void testJoin() throws IOException {databaseClient.sql("SELECT  * FROM t_author WHERE id = ?id").bind("id",1L).fetch().all().map(map -> {String id = String.valueOf(map.get("id"));String name = String.valueOf(map.get("name"));return new Author(Long.valueOf(id), name);}).subscribe(System.out::println);System.in.read();}

spring data r2dbc

开启r2dbc仓库功能,jpa

@EnableR2dbcRepositories
@Configuration
public class R2dbcConfig {}

1.写Repositories接口,默认继承一些crud方法
QBC: Query By Ctiteric
QBE: Query By Example

@Repository
public interface AuthorRepositories extends R2dbcRepository<Author,Long> {}

测试:
复杂调价查询:
1.QBE Api(不推荐)
2.自定义方法
3.自定义sql
repositeries起名有提示,按sql起名

@Repository
public interface AuthorRepositories extends R2dbcRepository<Author,Long> {/*** where id in ? and name like ?*/Flux<Author> findAllByIdInAndNameLike(Collection<Long> ids, String name);
}

测试复杂查询

    @Testpublic void testRepositories() throws IOException {authorRepositories.findAll().subscribe(System.out::println);authorRepositories.findAllByIdInAndNameLike(List.of(1L),"z%").subscribe(System.out::println);System.in.read();}

控制台打印sql

SELECT t_author.id, t_author.name 
FROM t_author 
WHERE t_author.id IN (?) AND (t_author.name LIKE ?)

缺点:仅限单表crud
测试多表复杂查询
自定义注解@Query(),指定sql语句
1-1查询:一个图书有一个作者
1-n查询:一个作者写了多本图书
实体类Book

@Data
@Table("t_book")
public class Book {@Idprivate Long id;private String title;private Long authorId;private LocalDateTime publishTime;}

repositorues

@Repository
public interface BookRepositories extends R2dbcRepository<Book,Long> {@Query("SELECT book.title,author.name " +"FROM index_demo.t_book book " +"LEFT JOIN index_demo.t_author author " +"ON book.author_id = author.id " +"WHERE book.id = :bookId")Mono<Book> findBookAndAuthor(Long bookId);
}

绑定查询参数:
在这里插入图片描述
自定义结果转换器

@ReadingConverter//读取数据库数据时,把row->book
public class BookConverter implements Converter<Row, Book> {@Overridepublic Book convert(Row source) {if (ObjectUtils.isEmpty(source)) {return new Book();}String title = source.get("title", String.class);String authorName = source.get("name", String.class);Book book = new Book();Author author = new Author();author.setName(authorName);book.setAuthor(author);book.setTitle(title);return book;}
}

配置自定义类型转换器

@EnableR2dbcRepositories
@Configuration
public class R2dbcConfig {/*** 将自己定义的转换器加入进去*/@Bean@ConditionalOnMissingBeanpublic R2dbcCustomConversions conversions () {return R2dbcCustomConversions.of(MySqlDialect.INSTANCE,new BookConverter());}}

测试

    @Testpublic void testQueryMulti() throws Exception{bookRepositories.findBookAndAuthor(1L).subscribe(System.out::println);System.in.read();}

总结:
1.spring data R2DBC 基础的CRUD用R2dbcRepository 提供好了
2.自定义复杂的sql(单表):@Query()
3.多表查询复杂结果集合:DatabaseClient自定义sql,自定义结果封装
@Query+自定义converter实现结果封装
自定义转换器问题:对以前crud产生影响
Converter<Row,Book>:把数据库每一行row,转换成book
工作时机:spring data发现方法签名只要是返回Book,利用自定义转换器工作
所有对Book结果封装都使用转换器,包括单表查询
解决方法1:新VO+新的Repositories+自定义类型转换器
BookauthorVO

@Data
public class BookAuthorVO {private Long id;private String title;private Long authorId;private LocalDateTime publishTime;private Author author;//每一本书有唯一作者
}

自定义BookAuthorRepositories

@Repository
public interface BookAuthorRepositories extends R2dbcRepository<BookAuthorVO,Long> {@Query("SELECT book.title,author.name " +"FROM index_demo.t_book book " +"LEFT JOIN index_demo.t_author author " +"ON book.author_id = author.id " +"WHERE book.id = :bookId")Mono<Book> findBookAndAuthor(@Param("bookId")Long bookId);
}

自定义BookAuthor转换器

@ReadingConverter//读取数据库数据时,把row->book
public class BookAuthorConverter implements Converter<Row, BookAuthorVO> {@Overridepublic BookAuthorVO convert(Row source) {if (ObjectUtils.isEmpty(source)) {return new BookAuthorVO();}String title = source.get("title", String.class);String authorName = source.get("name", String.class);BookAuthorVO book = new BookAuthorVO();Author author = new Author();author.setName(authorName);book.setAuthor(author);book.setTitle(title);return book;}
}

解决方法2:自定义转换器中增加判断
source.getMetaData.contains(“”)
让converter兼容更多表结构(推荐!!!)

@ReadingConverter//读取数据库数据时,把row->book
public class BookAuthorConverter implements Converter<Row, BookAuthorVO> {@Overridepublic BookAuthorVO convert(Row source) {if (ObjectUtils.isEmpty(source)) {return new BookAuthorVO();}String title = source.get("title", String.class);BookAuthorVO book = new BookAuthorVO();book.setTitle(title);if (source.getMetadata().contains("name")) {String authorName = source.get("name", String.class);Author author = new Author();author.setName(authorName);book.setAuthor(author);}return book;}
}

经验:
1-1/1-n都需要自定义结果集
spring data R2dbc:自定义converter指定结果封装
mybatis:自定义resultMap标签来封装

BufferUntilChanged操作

如果下一个判定值,比起上一个发生了变化,就开一个新buffer保存
如果没有变化,就保存到原buffer中
前提:数据已经提前排好序
groupBy:允许乱序
作者有很多图书. 1:n
sql

SELECT author.name,author.id,book.title
FROM index_demo.t_author author
LEFT JOIN index_demo.t_book book
ON author.id = book.author_id
WHERE author.id = 1;

测试

    @Testpublic void testAuthorBookTest() throws Exception {databaseClient.sql("SELECT author.name,author.id,book.title " +"FROM index_demo.t_author author " +"LEFT JOIN index_demo.t_book book " +"ON author.id = book.author_id " +"WHERE author.id = ?id").bind("id", 1L).fetch().all().bufferUntilChanged(rowMap -> Long.parseLong(String.valueOf(rowMap.get("id"))))//id发生变化,重新分组,若是对象比较,需重写equals()方法.map(list -> {if (CollectionUtils.isEmpty(list)) {return Collections.emptyList();}List<Book> bookList = list.stream().map(item -> {String title = String.valueOf(item.get("title"));return Book.builder().title(title).build();}).toList();return Author.builder().id(Long.valueOf(String.valueOf(list.get(0).get("id")))).name(String.valueOf(list.get(0).get("name"))).bookList(bookList);}).subscribe(System.out::println);System.in.read();}

这篇关于R2DBC-响应式数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/629739

相关文章

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 数据库表与查询操作实战案例

《MySQL数据库表与查询操作实战案例》本文将通过实际案例,详细介绍MySQL中数据库表的设计、数据插入以及常用的查询操作,帮助初学者快速上手,感兴趣的朋友跟随小编一起看看吧... 目录mysql 数据库表操作与查询实战案例项目一:产品相关数据库设计与创建一、数据库及表结构设计二、数据库与表的创建项目二:员

MybatisPlus中removeById删除数据库未变解决方案

《MybatisPlus中removeById删除数据库未变解决方案》MyBatisPlus中,removeById需实体类标注@TableId注解以识别数据库主键,若字段名不一致,应通过value属... 目录MyBATisPlus中removeBypythonId删除数据库未变removeById(Se

在 Spring Boot 中连接 MySQL 数据库的详细步骤

《在SpringBoot中连接MySQL数据库的详细步骤》本文介绍了SpringBoot连接MySQL数据库的流程,添加依赖、配置连接信息、创建实体类与仓库接口,通过自动配置实现数据库操作,... 目录一、添加依赖二、配置数据库连接三、创建实体类四、创建仓库接口五、创建服务类六、创建控制器七、运行应用程序八

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp