如何在r2dbc中联接表? [英] How to join tables in r2dbc?

查看:897
本文介绍了如何在r2dbc中联接表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java反应器中,r2dbc.我有两个表A,B.我也为它们定义了存储库. 如何获取由A连接B组成的数据?

In java reactor, r2dbc. I have two tables A, B. I also have repositories for them defined. How can i get data made up of A join B?

我只想出以下方法: 从A调用databaseClient.select,然后在循环调用中从B选择.

I only come up with the following approach: call databaseClient.select from A and consequently in a loop call select from B.

但是我想要更有效,更被动的方式.怎么做?

But i want more efficient and reactive way. How to do it?

推荐答案

TL; DR:使用SQL.

Spring Data的DatabaseClient是R2DBC的改进的,反应性的变体,而JdbcTemplate是JDBC的变体.它封装了各种执行模式,资源管理和异常转换.其流利的API选择/插入/更新/删除方法适用于简单而平坦的查询.超出提供的API的所有内容均受SQL使用限制.

Spring Data's DatabaseClient is an improved and reactive variant for R2DBC of what JdbcTemplate is for JDBC. It encapsulates various execution modes, resource management, and exception translation. Its fluent API select/insert/update/delete methods are suitable for simple and flat queries. Everything that goes beyond the provided API is subject to SQL usage.

话虽如此,您正在寻找的方法是DatabaseClient.execute(…):

That being said, the method you're looking for is DatabaseClient.execute(…):

DatabaseClient client = …;
client.execute("SELECT person.age, address.street FROM person INNER JOIN address ON person.address = address.id");

仓库@Query方法完全相同.

在结果处理期间调用数据库是锁定整个结果处理的好方法,因为结果是按流方式获取的.在未获取所有结果的情况下发出查询可能会耗尽128或256个项目的预取缓冲区,并导致结果流卡住.此外,您正在创建N + 1问题.

Calling the database during result processing is a good way to lock up the entire result processing as results are fetched stream-wise. Issuing a query while not all results are fetched yet can exhaust the prefetch buffer of 128 or 256 items and cause your result stream to stuck. Additionally, you're creating an N+1 problem.

这篇关于如何在r2dbc中联接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆