In the MyBatis configuration file, the following configurations are supported:
properties, settings, typeAliases, typeHandlers,
objectFactory, objectWrapperFactory, reflectorFactory,
plugins, environments, databaseIdProvider, mappers
We useobjectWrapperFactory
To solve this problem.
When configuring this property, you must follow the order of the above attribute configuration (inobjectFactory
Later, 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 thatobject
yesMap
When type, returntrue
, and then belowgetWrapperFor
Return a Wrapper class that can handle key as camel.
First, refer to the existing onesMapWrapper
Realize 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 provideObjectWrapperFactory
Implementation 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 codeuseCamelCaseMapping
So if you want to be able to use this function, you still need tosettings
Added the following configurations:
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- Other configurations --> </settings>
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 frequentlyresultType
As 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