所以最后我编码如下。
我对整个反应式范式很陌生。所以下面的代码可能不是最好的方法。最终,我希望 Spring Data R2DBC 增加对 Page 返回类型的支持。上述问题的答案在 CustomMentorRepositoryImpl 类中的 search 方法实现中。
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 {
}
// CustomRepository 类
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()))
);
}
}