feat: 增加填充器
This commit is contained in:
parent
1d67d20416
commit
9ab6918432
@ -5,6 +5,7 @@ import com.flyfish.framework.query.Queries;
|
||||
import com.flyfish.framework.query.Query;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.utils.CopyUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@ -33,6 +34,9 @@ public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
|
||||
protected List<String> fields;
|
||||
|
||||
@Getter
|
||||
protected boolean fetchRef;
|
||||
|
||||
public Qo<T> accept(List<T> result, Pageable pageable) {
|
||||
this.pageable = pageable;
|
||||
this.result = result;
|
||||
|
@ -94,8 +94,8 @@ public interface ReactiveQueryModelExecutor<T> {
|
||||
/**
|
||||
* 删除全部
|
||||
*
|
||||
* @param qo 查询实体
|
||||
* @param query 查询实体
|
||||
* @return 结果
|
||||
*/
|
||||
Mono<Void> deleteAll(Qo<T> qo);
|
||||
Mono<Void> deleteAll(Qo<T> query);
|
||||
}
|
||||
|
@ -83,6 +83,14 @@ public interface Qo<T> {
|
||||
@JsonIgnore
|
||||
List<String> getFields();
|
||||
|
||||
/**
|
||||
* 是否拉取关联查询
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@JsonIgnore
|
||||
boolean isFetchRef();
|
||||
|
||||
/**
|
||||
* 让值全部包含在Pojo里
|
||||
*
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.flyfish.framework.r2dbc.config;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.r2dbc.repository.decorator.DomainEntityOperations;
|
||||
import com.flyfish.framework.r2dbc.operations.R2dbcReactiveEntityOperations;
|
||||
import com.flyfish.framework.r2dbc.repository.factory.DefaultReactiveRepositoryFactoryBean;
|
||||
import com.flyfish.framework.r2dbc.repository.impl.DefaultReactiveRepositoryImpl;
|
||||
@ -9,10 +10,12 @@ import io.r2dbc.spi.ConnectionFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.data.mapping.callback.EntityCallback;
|
||||
import org.springframework.data.r2dbc.config.EnableR2dbcAuditing;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
|
||||
import org.springframework.r2dbc.connection.init.CompositeDatabasePopulator;
|
||||
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer;
|
||||
@ -40,6 +43,12 @@ public class R2dbcDataConfig {
|
||||
return new R2dbcReferenceEntityCallback();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public R2dbcEntityTemplate r2dbcEntityTemplate(ConnectionFactory connectionFactory) {
|
||||
return new DomainEntityOperations(connectionFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveEntityOperations r2dbcReactiveEntityOperations(R2dbcEntityOperations r2dbcEntityOperations) {
|
||||
return new R2dbcReactiveEntityOperations(r2dbcEntityOperations);
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.flyfish.framework.r2dbc.query.fill;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* r2dbc引用填充
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface R2dbcReferenceFiller {
|
||||
|
||||
<T extends Domain> Flux<T> fill(Flux<T> beans);
|
||||
|
||||
<T extends Domain> Mono<T> fill(Mono<T> bean);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.flyfish.framework.r2dbc.repository.fill;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import org.springframework.data.r2dbc.convert.R2dbcConverter;
|
||||
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* r2dbc引用填充
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface R2dbcReferenceFiller<T extends Domain> {
|
||||
|
||||
/**
|
||||
* 创建新的填充器
|
||||
*
|
||||
* @param <T> 泛型
|
||||
* @return 结果
|
||||
*/
|
||||
static <T extends Domain> R2dbcReferenceFiller<T> newFiller(RelationalEntityInformation<T, String> entityInformation, R2dbcConverter converter) {
|
||||
return new SimpleR2dbcReferenceFiller<>(entityInformation, converter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充附加值
|
||||
*
|
||||
* @param bean 实体信息
|
||||
* @return 结果
|
||||
*/
|
||||
Mono<T> fill(T bean);
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.flyfish.framework.r2dbc.repository.fill;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import org.springframework.data.r2dbc.convert.R2dbcConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public class SimpleR2dbcReferenceFiller<T extends Domain> implements R2dbcReferenceFiller<T> {
|
||||
|
||||
private final Lazy<RelationalPersistentEntity<T>> persistentEntity;
|
||||
|
||||
public SimpleR2dbcReferenceFiller(RelationalEntityInformation<T, String> entity, R2dbcConverter converter) {
|
||||
this.persistentEntity = Lazy.of(() -> (RelationalPersistentEntity<T>) converter
|
||||
.getMappingContext()
|
||||
.getRequiredPersistentEntity(entity.getJavaType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充附加值
|
||||
*
|
||||
* @param bean 实体信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public Mono<T> fill(T bean) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package com.flyfish.framework.r2dbc.repository.impl;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.domain.base.Qo;
|
||||
import com.flyfish.framework.r2dbc.repository.fill.R2dbcReferenceFiller;
|
||||
import com.flyfish.framework.repository.DefaultReactiveRepository;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -13,12 +14,10 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.r2dbc.convert.R2dbcConverter;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
|
||||
import org.springframework.data.r2dbc.repository.support.SimpleR2dbcRepository;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.core.query.Criteria;
|
||||
import org.springframework.data.relational.core.query.Query;
|
||||
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.util.Assert;
|
||||
import reactor.core.publisher.Flux;
|
||||
@ -40,7 +39,7 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
|
||||
private final R2dbcEntityOperations entityOperations;
|
||||
|
||||
private final Lazy<RelationalPersistentEntity<T>> persistentEntity;
|
||||
private final R2dbcReferenceFiller<T> filler;
|
||||
|
||||
/**
|
||||
* 构造新的默认仓库
|
||||
@ -53,9 +52,7 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
super(entity, entityOperations, converter);
|
||||
this.entity = entity;
|
||||
this.entityOperations = entityOperations;
|
||||
this.persistentEntity = Lazy.of(() -> (RelationalPersistentEntity<T>) converter
|
||||
.getMappingContext()
|
||||
.getRequiredPersistentEntity(this.entity.getJavaType()));
|
||||
this.filler = R2dbcReferenceFiller.newFiller(entity, converter);
|
||||
}
|
||||
|
||||
|
||||
@ -68,7 +65,9 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
@Override
|
||||
public Mono<T> findByName(String name) {
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
return entityOperations.selectOne(Query.query(Criteria.where("name").is(name)), entity.getJavaType());
|
||||
return entityOperations
|
||||
.selectOne(Query.query(Criteria.where("name").is(name)), entity.getJavaType())
|
||||
.flatMap(this::afterSelect);
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
@ -83,8 +82,9 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Mono<T> findOne(Qo<T> query) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
.flatMap(querying -> entityOperations.selectOne(querying, entity.getJavaType()));
|
||||
return getQuery(query)
|
||||
.flatMap(querying -> entityOperations.selectOne(querying, entity.getJavaType()))
|
||||
.flatMap(t -> this.afterSelect(query, t));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,9 +96,9 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Flux<T> findAll(Qo<T> query) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
return getQuery(query)
|
||||
.flatMapMany(querying -> entityOperations.select(querying, entity.getJavaType())
|
||||
.doOnNext(t -> t.setCurrentUser(query.getUser())));
|
||||
.flatMap(t -> this.afterSelect(query, t)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,9 +113,9 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Flux<T> findAll(Qo<T> query, Sort sort) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
.flatMapMany(querying -> entityOperations.select(querying.sort(sort),
|
||||
entity.getJavaType()));
|
||||
return getQuery(query)
|
||||
.flatMapMany(querying -> entityOperations.select(querying.sort(sort), entity.getJavaType())
|
||||
.flatMap(t -> this.afterSelect(query, t)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,10 +128,10 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Mono<Page<T>> findAll(Qo<T> query, Pageable pageable) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
return getQuery(query)
|
||||
.flatMap(querying -> entityOperations.select(querying.with(pageable),
|
||||
entity.getJavaType())
|
||||
.doOnNext(t -> t.setCurrentUser(query.getUser()))
|
||||
.flatMap(t -> this.afterSelect(query, t))
|
||||
.collectList()
|
||||
.flatMap(list -> ReactivePageableExecutionUtils.getPage(list, pageable, this.count(query))))
|
||||
.defaultIfEmpty(Page.empty());
|
||||
@ -145,7 +145,7 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Mono<Long> count(Qo<T> query) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
return getQuery(query)
|
||||
.flatMap(querying -> this.entityOperations.count(querying, entity.getJavaType()))
|
||||
.defaultIfEmpty(0L);
|
||||
}
|
||||
@ -161,7 +161,7 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
public Flux<T> findAllByValues(String key, List<?> values) {
|
||||
Criteria criteria = Criteria.where(key).in(values);
|
||||
Query query = Query.query(criteria);
|
||||
return entityOperations.select(query, entity.getJavaType());
|
||||
return entityOperations.select(query, entity.getJavaType()).flatMap(this::afterSelect);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,7 +172,7 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
*/
|
||||
@Override
|
||||
public Mono<Boolean> exists(Qo<T> query) {
|
||||
return Mono.justOrEmpty(getQuery(query))
|
||||
return getQuery(query)
|
||||
.flatMap(querying -> entityOperations.exists(querying, entity.getJavaType()))
|
||||
.defaultIfEmpty(false);
|
||||
}
|
||||
@ -180,12 +180,12 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
/**
|
||||
* 删除全部
|
||||
*
|
||||
* @param qo 查询实体
|
||||
* @param query 查询实体
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> deleteAll(Qo<T> qo) {
|
||||
return Mono.justOrEmpty(getQuery(qo))
|
||||
public Mono<Void> deleteAll(Qo<T> query) {
|
||||
return getQuery(query)
|
||||
.flatMap(querying -> entityOperations.delete(querying, entity.getJavaType()))
|
||||
.then(Mono.empty());
|
||||
}
|
||||
@ -235,7 +235,19 @@ public class DefaultReactiveRepositoryImpl<T extends Domain> extends SimpleR2dbc
|
||||
return entity;
|
||||
}
|
||||
|
||||
private Optional<Query> getQuery(Qo<T> qo) {
|
||||
return qo.getQuery(entity);
|
||||
private Mono<Query> getQuery(Qo<T> qo) {
|
||||
return Mono.justOrEmpty(qo.getQuery(entity));
|
||||
}
|
||||
|
||||
private Mono<T> afterSelect(T entity) {
|
||||
return filler.fill(entity);
|
||||
}
|
||||
|
||||
private Mono<T> afterSelect(Qo<T> qo, T entity) {
|
||||
entity.setCurrentUser(qo.getUser());
|
||||
if (qo.isFetchRef()) {
|
||||
return filler.fill(entity);
|
||||
}
|
||||
return Mono.just(entity);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user