问题遇到的现象和发生背景
之前用的mybatis-plus,很方便,现在要改成r2dbc,研究了一下,单表很好写,但是多表查询要怎么样来搞呢?
问题相关代码,请勿粘贴截图
public interface IAlVerifyCodeRepository extends ReactiveCrudRepository<AlVerifyCode, Long>
{
@Query("select c.id, c.mobile, c.code ,b.name,b.id from t1 c ,t2 b" +
"where c.mobile = :mobile and c.code = " +
":code and " +
"c.state = 0 and b.mobile = c.mobile")
Flux<AlVerifyCode> verifyCodeByMobile(String mobile, String code);
}
这样写是明显不行的,因为sql返回来的明显不是一个<AlVerifyCode>对象
那么,这种多表查询要怎么写?
看到有的文章是这样写
List<FluxToMono> initData = new ArrayList<>();
return tableMappingRepository.findAllBySearchEquals(1)
.reduce(initData, (rd, next) -> {
rd.add(new FluxToMono(next.getTableType(), next.getMappingId()));
return rd;
})
.flatMap(fMap -> {
FluxToMono aFilter = fMap.stream().filter(f -> DeviceTypeEnum.A.getCode().equals(f.getTableType())).findFirst().orElse(FluxToMono.builder().build());
Mono<TableA> a = tableARepository.findFirstById(aFilter.getMappingId());
FluxToMono bFilter = fMap.stream().filter(f -> DeviceTypeEnum.B.getCode().equals(f.getTableType())).findFirst().orElse(FluxToMono.builder().build());
Mono<NoiseMessageRecord> b = tableBRepository.findFirstById(bFiltert.getMappingId());
return Mono.zip(
a.defaultIfEmpty(TableA.builder().build()),
b.defaultIfEmpty(TableB.builder().build()),
(lambdaA, lambdaB) ->
ShowDataVo.builder()
.name(lambdaA.getName())
.sex(lambdaA.getSex())
.school(lambdaB.getSchool())
.grade(lambdaB.getgrade())
.build());
});
感觉太麻烦,也太不好辨识了
这玩意儿到底要怎么用?