简体   繁体   English

如何在spring-data-mongo中自定义FieldNamingStrategy

[英]How to custmize FieldNamingStrategy in spring-data-mongo

In spring-data-mongo ,is this interface's function still can be extended? 在spring-data-mongo中,这个接口的功能还可以扩展吗?

What I want is saving CamelCaseToUnderline in mongodb and querying UnderlineToCamelCase. 我想要的是在mongodb中保存CamelCaseToUnderline并查询UnderlineToCamelCase。

I see the part source code, it has a final FieldNamingStrategy.if I wanna costomized it ,it must before instantiation, how? 我看到了部分源代码,它具有最终的FieldNamingStrategy。如果我想对它进行通用化,它必须在实例化之前,怎么做?

public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty<MongoPersistentProperty> implements MongoPersistentProperty {
    ....
    private final FieldNamingStrategy fieldNamingStrategy;

    public BasicMongoPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, MongoPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder, FieldNamingStrategy fieldNamingStrategy) {

      super(field, propertyDescriptor, owner, simpleTypeHolder);
      this.fieldNamingStrategy = fieldNamingStrategy == null ? PropertyNameFieldNamingStrategy.INSTANCE : fieldNamingStrategy;

      if (isIdProperty() && getFieldName() != ID_FIELD_NAME) {
        LOG.warn("Customizing field name for id property not allowed! Custom name will not be considered!");
      }
    }
   ...
}

Hi this is from the spring boot appendix 嗨,这是从Spring Boot附录中获得的

# MONGODB (MongoProperties)
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use

You've got the follow strategies out of the box 您可以立即使用以下策略

CamelCaseSplittingFieldNamingStrategy
CamelCaseAbbreviatingFieldNamingStrategy
SnakeCaseFieldNamingStrategy

If these dont suit your needs, feel free to extend them 如果这些都不满足您的需求,请随时扩展它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM