修复:当构造函数字段顺序与数据库字段定义顺序不一致时可能出现的映射失败的问题

This commit is contained in:
natsufumij 2023-11-10 10:41:19 +08:00
parent 60a0fae0a0
commit 087ddd3442

View File

@ -15,6 +15,7 @@
*/
package com.mybatisflex.core.mybatis;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.annotations.AutomapConstructor;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
@ -763,53 +764,12 @@ public class FlexDefaultResultSetHandler extends DefaultResultSetHandler {
? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
}
/**
* <h3>将驼峰转为下划线</h3>
* <pre>{@code
* var word = underlineByhump("helloWorld")
* //word == "hello_world"
* }</pre>
* @author natsufumij
*/
public static String underlineByhump(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大写
boolean preCharIsUpperCase = true;
// 当前字符是否大写
boolean curreCharIsUpperCase = true;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append("_");
} else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
sb.append("_");
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw, List<Class<?>> constructorArgTypes,
List<Object> constructorArgs, Constructor<?> constructor, boolean foundValues) throws SQLException {
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
Class<?> parameterType = constructor.getParameterTypes()[i];
Parameter parameter = constructor.getParameters()[i];
String columnName = underlineByhump(parameter.getName());
String columnName = StringUtil.camelToUnderline(parameter.getName());
TypeHandler<?> typeHandler = rsw.getTypeHandler(parameterType, columnName);
Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
constructorArgTypes.add(parameterType);