【发布时间】:2022-08-03 20:52:15
【问题描述】:
我有一个名为客户的文档列表,我使用 mongotemplate 检索了这些文档,下面是一些文档:
{\"customer\": {\"entityPerimeter\": \"abp\", \"name\": \"ZERZER\", \"siren\": \"6154645\", \"enterpriseId\": \"546456\", \"ic01\": \"\", \"marketingOffer\": \"qlksdjf\", \"irType\": \"Router\", \"offerSpecificationOfferLabel\": \"2Mb\"}}
{\"customer\": {\"entityPerimeter\": \"sdf\", \"name\": \"qazer\", \"siren\": \"156\", \"enterpriseId\": \"546456\", \"ic01\": \"\", \"marketingOffer\": \"qlksdjddddsqf\", \"irType\": \"Ruter\", \"offerSpecificationOfferLabel\": \"2Mb\"}}
{\"customer\": {\"entityPerimeter\": \"zer\", \"name\": \"fazdsdfsdgg\", \"siren\": \"sdfs\", \"enterpriseId\": \"1111\", \"ic01\": \"\", \"marketingOffer\": \"qsdfqsd\", \"irType\": \"Router\", \"offerSpecificationOfferLabel\": \"2Mb\"}}
我在 mongodb 中所做的就是得到这个结果:
public List<DBObject> findAllCustomersByExtractionDateMongo(LocalDate extractionDate) {
Aggregation aggregation = newAggregation(
match(Criteria.where(EXTRACTION_DATE).is(extractionDate)),
project(CUSTOMER).andExclude(\"_id\"),
group().addToSet(\"$customer\").as(\"distinct_customers\"),
unwind(\"distinct_customers\"),
project().andExclude(\"_id\").and(\"distinct_customers\").as(\"customer\"),
project().andExclude(\"distinct_customers\")
);
return template
.aggregate(aggregation, COLLECTION, DBObject.class)
.getMappedResults();
}
现在我真正想要的是将这些文档映射到一个名为 Customer 的类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Customer {
private String entityPerimeter;
private String name;
private String siren;
private String enterpriseId;
private String ic01;
private String marketingOffer;
private String product;
private String irType;
}
我试图通过创建一个 DTO 接口来做到这一点:
public interface DocumentToCustomerMapper {
String NULL = \"null\";
static Customer getFilter(DBObject document) {
var customer = new Customer();
customer.setSiren(Optional.ofNullable((String) document.get(CustomerAttributes.SIREN.value())).orElse(NULL));
customer.setEnterpriseId(Optional.ofNullable((String) document.get(CustomerAttributes.ENTERPRISE_ID.value())).orElse(NULL));
customer.setEntityPerimeter(Optional.ofNullable((String) document.get(CustomerAttributes.ENTITY_PERIMETER.value())).orElse(NULL));
customer.setName(Optional.ofNullable((String) document.get(CustomerAttributes.NAME.value())).orElse(NULL));
customer.setIc01(Optional.ofNullable((String) document.get(CustomerAttributes.IC_01.value())).orElse(NULL));
customer.setMarketingOffer(Optional.ofNullable((String) document.get(CustomerAttributes.MARKETING_OFFER.value())).orElse(NULL));
customer.setProduct(Optional.ofNullable((String) document.get(CustomerAttributes.PRODUCT.value())).orElse(NULL));
customer.setIrType(Optional.ofNullable((String) document.get(CustomerAttributes.IR_TYPE.value())).orElse(NULL));
return customer;
}
}
然后在findAllCystomersByExtractionDateMongo() 我正在这样做:
public List<Customer> findAllCustomersByExtractionDateMongo(LocalDate extractionDate) {
Aggregation aggregation = newAggregation(
match(Criteria.where(EXTRACTION_DATE).is(extractionDate)),
project(CUSTOMER).andExclude(\"_id\"),
group().addToSet(\"$customer\").as(\"distinct_customers\"),
unwind(\"distinct_customers\"),
project().andExclude(\"_id\").and(\"distinct_customers\").as(\"customer\"),
project().andExclude(\"distinct_customers\")
);
final Converter<DBObject, Customer> converter = DocumentToCustomerMapper::getFilter;
MongoCustomConversions cc = new MongoCustomConversions(List.of(converter));
((MappingMongoConverter) template.getConverter()).setCustomConversions(cc);
return template
.aggregate(aggregation, COLLECTION, Customer.class)
.getMappedResults();
}
但不幸的是,它给了我一个例外:
Couldn\'t resolve type arguments for class com.obs.dqsc.api.repository.mongo_template.CustomerRepositoryImpl$$Lambda$1333/0x00000008012869a8!
我试图删除此代码:
final Converter<DBObject, Customer> converter = DocumentToCustomerMapper::getFilter;
MongoCustomConversions cc = new MongoCustomConversions(List.of(converter));
((MappingMongoConverter) template.getConverter()).setCustomConversions(cc);
然后我得到的只是客户对象中的一些空值:
Customer(entityPerimeter=null, name=null, siren=null, enterpriseId=null, ic01=null, marketingOffer=null, product=null, irType=null)
Customer(entityPerimeter=null, name=null, siren=null, enterpriseId=null, ic01=null, marketingOffer=null, product=null, irType=null)
Customer(entityPerimeter=null, name=null, siren=null, enterpriseId=null, ic01=null, marketingOffer=null, product=null, irType=null)
注意:对于性能问题,我不想在 java 端做任何映射,也不想在我的 mongo 配置中使用全局转换器。
标签: java mongodb spring-boot mapping mongotemplate