SoFunction
Updated on 2025-03-08

MyBatis Map result key is converted to camel

In the MyBatis configuration file, the following configurations are supported:

properties, settings, typeAliases, typeHandlers,
objectFactory, objectWrapperFactory, reflectorFactory,
plugins, environments, databaseIdProvider, mappers

We useobjectWrapperFactoryTo solve this problem.

When configuring this property, you must follow the order of the above attribute configuration (inobjectFactoryLater, inreflectorFactory) Otherwise, there will be an error.

The objectWrapperFactory interface is as follows:

public interface ObjectWrapperFactory {
 boolean hasWrapperFor(Object object);
 ObjectWrapper getWrapperFor(MetaObject metaObject, Object object);
}

By implementing this interface, it can be judged thatobjectyesMapWhen type, returntrue, and then belowgetWrapperForReturn a Wrapper class that can handle key as camel.

First, refer to the existing onesMapWrapperRealize your ownMyWrapper

package ;
import ;
import ;
import ;
/**
  * Hump treatment
  */
public class MyMapWrapper extends MapWrapper {
  public MyMapWrapper(MetaObject metaObject, Map<String, Object> map) {
    super(metaObject, map);
  }
  @Override
  public String findProperty(String name, boolean useCamelCaseMapping) {
    if (useCamelCaseMapping
        && (((0) >= 'A' && (0) <= 'Z')
           || ("_") >= 0)) {
      return underlineToCamelhump(name);
    }
    return name;
  }
  /**
    * Replace the underlined style with the hump style
    * @param inputString
    * @return
    */
  public String underlineToCamelhump(String inputString) {
    StringBuilder sb = new StringBuilder();
    boolean nextUpperCase = false;
    for (int i = 0; i < (); i++) {
      char c = (i);
      if (c == '_') {
        if (() > 0) {
          nextUpperCase = true;
        }
      } else {
        if (nextUpperCase) {
          ((c));
          nextUpperCase = false;
        } else {
          ((c));
        }
      }
    }
    return ();
  }
}

This method is easy to implement. Then provideObjectWrapperFactoryImplementation class:

package ;
import ;
import ;
import ;
import ;
/**
  * Map type result turns into camel
  */
public class MapWrapperFactory implements ObjectWrapperFactory {
  @Override
  public boolean hasWrapperFor(Object object) {
    return object != null && object instanceof Map;
  }
  @Override
  public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
    return new MyMapWrapper(metaObject, (Map) object);
  }
}

Still very simple, configure it in the MyBatis configuration fileobjectWrapperFactory

<objectWrapperFactory type=""/>

Because it's judged in the codeuseCamelCaseMappingSo if you want to be able to use this function, you still need tosettingsAdded the following configurations:

&lt;settings&gt;
  &lt;setting name="mapUnderscoreToCamelCase" value="true"/&gt;
  &lt;!-- Other configurations --&gt;
&lt;/settings&gt;

This method does not require the use of an interceptor, and it is directly from the sourceMap The key is processed without any additional consumption, if you set the return value frequentlyresultTypeAs a map, you can try this method.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links