gpt4 book ai didi

spring-webflux - Spring Data Reactive R2DBC 分页

转载 作者:行者123 更新时间:2023-12-04 12:58:11 25 4
gpt4 key购买 nike

Spring Data Reactive 不支持 Page 作为返回类型。我正在尝试使用来自服务层的两个不同的查询(一个用于获取结果列表,另一个用于获取总数)来实现这一点。
第一个查询给出的内容为 Flux<Person>第二个查询给出的计数为 Mono<Integer> .
我可以将这两者结合起来并返回 Mono<Page<Person>> ?如何?
请帮忙。

最佳答案

所以最后我将其编码如下。
我是整个响应式(Reactive)范式的新手。所以下面的代码可能不是最好的方法。最后,我希望 Spring Data R2DBC 增加对 Page 返回类型的支持。上述问题的答案在 CustomMentorRepositoryImpl 类中的搜索方法实现中。

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("mentors")
public class Mentor {

@Id
private Long id;
boolean hasId() {
return id != null;
}

@Version
@Column("version")
private Integer version;

@Column("name")
private String name;

@Column("age")
private Integer age;

}
存储库类
import com.sample.app.domain.Mentor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Repository
public interface MentorRepository extends
ReactiveCrudRepository<Mentor, String>, CustomMentorRepository {
}
//自定义仓库类
import com.sample.app.domain.Mentor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface CustomMentorRepository {
public Mono<Page<Mentor>> search(String name, Pageable page);
}
//CustomRepositoryImpl 类
import lombok.AllArgsConstructor;
import com.sample.app.domain.Mentor;
import com.sample.app.repository.CustomMentorRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.core.DatabaseClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@AllArgsConstructor
public class CustomMentorRepositoryImpl implements CustomMentorRepository {

DatabaseClient databaseClient;

@Override
public Mono<Page<Mentor>> search(String name, Pageable page) {
// Get the contents
Flux<Mentor> contents = databaseClient.execute("SELECT * FROM mentors where name like :name limit :limit offset :offset")
.bind("name", "%" + name + "%")
.bind("limit", page.getPageSize())
.bind("offset", page.getOffset())
.as(Mentor.class)
.fetch().all();
// Get the count
Mono<Long> count = databaseClient.execute("SELECT count(*) FROM mentors where name like :name")
.bind("name", "%" + name + "%")
.as(Long.class)
.fetch().first();
return contents.collectList().zipWith(count).flatMap(objects ->
Mono.just(new PageImpl<Mentor>(objects.getT1(), page, objects.getT2()))
);
}
}

关于spring-webflux - Spring Data Reactive R2DBC 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63324333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com