This is definitely a pain point when using MyBatis.
AFAIK there is no way to customize the framework from the outside to
do this.
I am running on a slightly modified version with one class modified to
get
auto-mapping for camel case Java properties.
In case you are interested:
org.apache.ibatis.reflection.Reflector:
New field (only for performance reasons):
private Map<String, String> propertyMap = new HashMap<String,
String>();
Then I replaced these line in the constructor
for (String propName : readablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH),
propName);
}
for (String propName : writeablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH),
propName);
}
by
putAll(readablePropertyNames);
putAll(writeablePropertyNames);
and added the putAll method:
private void putAll(String[] propertyNames) {
for (String propName : propertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(),
propName);
caseInsensitivePropertyMap.put(Stringz.camelCase2underScore(propName).toUpperCase(),
propName);
}
}
Then replaced findPropertyName with this
public String findPropertyName(String name) {
String propName = propertyMap.get(name);
if (propName == null) {
propName =
caseInsensitivePropertyMap.get(name.toUpperCase());
if (propName != null) {
propertyMap.put(name, propName);
}
}
return propName;
}
Finally the code to convert a string from camel case to underscore:
(currently in a utility class named Stringz):
/**
* <p>Converts a string in "camel case" to its underscored, lower-
case equivalent, e.g. "minimumFieldWidth" => "minimum_field_width".</
p>
* <p>The algorithm determines each character's type as one of
lower-case-character, upper-case-character, digit or other
(punctuation, ...).
* An underscore is inserted in between each change of character
type, except for the change from upper- to lower-case characters.</p>
* @param text a text in camel case form, e.g. "columnKey2value"
* @return the lower-cased, underscored form, e.g.
"column_key_2_value"
*/
@NotNull
public static String camelCase2underScore(@NotNull String text) {
StringBuilder builder = new StringBuilder(text.length() + 5);
CharType lastCharType = null;
for(int i = 0, count = text.length(); i < count; ++i) {
char c = text.charAt(i);
CharType charType = CharType.get(c);
//noinspection OverlyComplexBooleanExpression
if (lastCharType != null
&& lastCharType != charType
&& !(lastCharType == CharType.UPPER && charType ==
CharType.LOWER)) {
builder.append('_');
}
builder.append(Character.toLowerCase(c));
lastCharType = charType;
}
return builder.toString();
}
/**
* Represents the classification of a character into lower-case-
character, upper-case-character, digit or other.
* Uses the unicode character types (so that for example "ä"
counts as a lower-case-character).
*/
private enum CharType {
LOWER, UPPER, DIGIT, OTHER;
@NotNull
public static CharType get(char c) {
if(Character.isLowerCase(c)) {
return LOWER;
}
if (Character.isUpperCase(c)) {
return UPPER;
}
if (Character.isDigit(c)) {
return DIGIT;
}
return OTHER;