org.springframework.r2dbc DatabaseClient类已移至
import org.springframework.r2dbc.core.DatabaseClient;
来自
import org.springframework.data.r2dbc.core.DatabaseClient;
Spring数据文档https://spring.io/projects/spring-data-r2dbc引用了一种简单的'as'方法来转换为对象
databaseClient
.sql("select * from reading")
.as(CrepsReading::class.java)
.fetch()
.all()
.asFlow()
它不起作用。 map(class)都没有。似乎只有映射类起作用。
val all: Flux<CrepsReading> = databaseClient
.sql("SELECT id, name FROM person")
.map(CrepsReading::class)
.fetch().all()
如何简单地使用spring-data-r2dbc(1.2.0)映射对象?是否有文档描述了spring-data-r2dbc的一部分DatabaseClient的使用?
答案 0 :(得分:1)
在Spring 5.3 / Spring Data R2dbc中,DatabaseClient
被重构并移至Spring框架的核心。
选中my example以查看如何处理生成的地图。
public static final BiFunction<Row, RowMetadata, Post> MAPPING_FUNCTION = (row, rowMetaData) -> Post.builder()
.id(row.get("id", UUID.class))
.title(row.get("title", String.class))
.content(row.get("content", String.class))
.status(row.get("status", Post.Status.class))
.metadata(row.get("metadata", Json.class))
.createdAt(row.get("created_at", LocalDateTime.class))
.build();
private final DatabaseClient databaseClient;
public Flux<Post> findByTitleContains(String name) {
return this.databaseClient
.sql("SELECT * FROM posts WHERE title LIKE :title")
.bind("title", "%" + name + "%")
.map(MAPPING_FUNCTION)
.all();
}