Skip to content
广告❤️成为赞助商

Mapper

A mapper is used to map JavaBeans into types that MongoDB can recognize.

Note:

  • For example, mapping BigInteger to Long, since MongoDB does not support BigInteger, or mapping a custom type (e.g., User class) to a Document type.
  • When using a mapper, it needs to be registered as a Bean.

com.mongoplus.strategy.mapping.MappingStrategy

  • This interface defines the mapper. All mappers should implement this interface and its methods.
  • fieldValue: the value of the field to be mapped.

Example

First, create a class implementing the MappingStrategy interface, set the generic type to the mapped class, and register it as a bean (in the example below with @Component). Avoid setting proxy classes for mapper implementations.

java
// Register as a Spring Bean
@Component
public class BigIntegerMappingStrategy implements MappingStrategy<BigInteger> {}

Next, implement the mapping method of the MappingStrategy interface to define the mapping logic.

java
// Register as a Spring Bean
@Component
public class BigIntegerMappingStrategy implements MappingStrategy<BigInteger> {

    @Override
    public Object mapping(BigInteger fieldValue) throws IllegalAccessException {
        return fieldValue.longValue();
    }
}