From 78ef98b35adb0ea0ee60adc5b514603265e7df67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 7 May 2023 17:41:56 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E4=BF=AE=E5=A4=8D=20entity=20?= =?UTF-8?q?=E6=9C=89=E9=85=8D=E7=BD=AE=20typeHandler=20=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20RowUtil.toEntity=20=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B=20close?= =?UTF-8?q?=20#I70XGX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/row/RowUtil.java | 15 ++++---- .../com/mybatisflex/core/table/TableInfo.java | 38 +++++++++++++++---- .../mybatisflex/core/util/ConvertUtil.java | 11 +++++- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java index 6b5cc31e..ec9a3bfb 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java @@ -31,8 +31,7 @@ public class RowUtil { static final String INDEX_SEPARATOR = "$"; - private static final Map, Map> classGettersMapping = new ConcurrentHashMap<>(); - + private static final Map, Map> classSettersCache = new ConcurrentHashMap<>(); public static T toObject(Row row, Class objectClass) { return toObject(row, objectClass, 0); @@ -41,15 +40,15 @@ public class RowUtil { public static T toObject(Row row, Class objectClass, int index) { T instance = ClassUtil.newInstance(objectClass); - Map setterMethods = getSetterMethods(objectClass); + Map classSetters = getSetterMethods(objectClass); Set rowKeys = row.keySet(); - setterMethods.forEach((property, setter) -> { + classSetters.forEach((property, setter) -> { try { if (index <= 0) { for (String rowKey : rowKeys) { if (property.equalsIgnoreCase(rowKey)) { Object rowValue = row.get(rowKey); - Object value = ConvertUtil.convert(rowValue, setter.getParameterTypes()[0]); + Object value = ConvertUtil.convert(rowValue, setter.getParameterTypes()[0], true); setter.invoke(instance, value); } } @@ -60,7 +59,7 @@ public class RowUtil { for (String rowKey : rowKeys) { if (newProperty.equalsIgnoreCase(rowKey)) { Object rowValue = row.get(rowKey); - Object value = ConvertUtil.convert(rowValue, setter.getParameterTypes()[0]); + Object value = ConvertUtil.convert(rowValue, setter.getParameterTypes()[0], true); setter.invoke(instance, value); fillValue = true; break; @@ -129,7 +128,7 @@ public class RowUtil { public static void registerMapping(Class clazz, Map columnSetterMapping) { - classGettersMapping.put(clazz, columnSetterMapping); + classSettersCache.put(clazz, columnSetterMapping); } @@ -204,7 +203,7 @@ public class RowUtil { private static Map getSetterMethods(Class aClass) { - return MapUtil.computeIfAbsent(classGettersMapping, aClass, aClass1 -> { + return MapUtil.computeIfAbsent(classSettersCache, aClass, aClass1 -> { Map columnSetterMapping = new HashMap<>(); List setters = ClassUtil.getAllMethods(aClass1, method -> method.getName().startsWith("set") diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 628ae344..3173af76 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -38,6 +38,9 @@ import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.util.MapUtil; +import java.lang.reflect.Proxy; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -693,10 +696,7 @@ public class TableInfo { if (index <= 0) { for (String rowKey : rowKeys) { if (column.equalsIgnoreCase(rowKey)) { - Object rowValue = row.get(rowKey); - Object value = ConvertUtil.convert(rowValue, metaObject.getSetterType(columnInfo.property)); - value = invokeOnSetListener(instance, columnInfo.getProperty(), value); - metaObject.setValue(columnInfo.property, value); + setInstancePropertyValue(row, instance, metaObject, columnInfo, rowKey); } } } else { @@ -705,10 +705,7 @@ public class TableInfo { boolean fillValue = false; for (String rowKey : rowKeys) { if (newColumn.equalsIgnoreCase(rowKey)) { - Object rowValue = row.get(rowKey); - Object value = ConvertUtil.convert(rowValue, metaObject.getSetterType(columnInfo.property)); - value = invokeOnSetListener(instance, columnInfo.getProperty(), value); - metaObject.setValue(columnInfo.property, value); + setInstancePropertyValue(row, instance, metaObject, columnInfo, rowKey); fillValue = true; break; } @@ -723,6 +720,31 @@ public class TableInfo { } + private void setInstancePropertyValue(Row row, Object instance, MetaObject metaObject, ColumnInfo columnInfo, String rowKey) { + Object rowValue = row.get(rowKey); + TypeHandler typeHandler = columnInfo.buildTypeHandler(); + if (typeHandler != null) { + try { + //通过 typeHandler 转换数据 + rowValue = typeHandler.getResult(getResultSet(rowValue), 0); + } catch (SQLException e) { + //ignore + } + } + if (rowValue != null && !metaObject.getSetterType(columnInfo.property).isAssignableFrom(rowValue.getClass())) { + rowValue = ConvertUtil.convert(rowValue, metaObject.getSetterType(columnInfo.property), true); + } + rowValue = invokeOnSetListener(instance, columnInfo.getProperty(), rowValue); + metaObject.setValue(columnInfo.property, rowValue); + } + + + private ResultSet getResultSet(Object value) { + return (ResultSet) Proxy.newProxyInstance(TableInfo.class.getClassLoader(), + new Class[]{ResultSet.class}, (proxy, method, args) -> value); + } + + /** * 初始化乐观锁版本号 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ConvertUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ConvertUtil.java index 2455015c..16203f36 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ConvertUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ConvertUtil.java @@ -34,6 +34,10 @@ public class ConvertUtil { public static Object convert(Object value, Class targetClass) { + return convert(value, targetClass, false); + } + + public static Object convert(Object value, Class targetClass, boolean ignoreConvertError) { if (value == null && targetClass.isPrimitive()) { return getPrimitiveDefaultValue(targetClass); } @@ -134,8 +138,11 @@ public class ConvertUtil { } } - - throw new IllegalArgumentException("\"" + targetClass.getName() + "\" can not be parsed."); + if (ignoreConvertError) { + return null; + } else { + throw new IllegalArgumentException("Can not convert \"" + value + "\" to type\"" + targetClass.getName() + "\"."); + } }