From b4a134e924ec3dc468f1c24ee12990c8c1f53050 Mon Sep 17 00:00:00 2001 From: Faputa <2238932729@qq.com> Date: Thu, 23 Nov 2023 14:21:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E5=A5=BD=E7=9A=84getEntityClass?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使mapper可以支持多继承、继承泛型接口等场景 --- .../core/table/TableInfoFactory.java | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index 152d983b..dc7fe35e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -115,20 +115,62 @@ public class TableInfoFactory { private static Class getEntityClass(Class mapperClass) { - if (mapperClass == null || mapperClass == Object.class) { - return null; - } + return getEntityClass(mapperClass, null); + } + + private static Class getEntityClass(Class mapperClass, Type[] actualTypeArguments) { + // 检查基接口 Type[] genericInterfaces = mapperClass.getGenericInterfaces(); - if (genericInterfaces.length == 1) { - Type type = genericInterfaces[0]; + for (Type type : genericInterfaces) { if (type instanceof ParameterizedType) { - Type actualTypeArgument = ((ParameterizedType) type).getActualTypeArguments()[0]; - return actualTypeArgument instanceof Class ? (Class) actualTypeArgument : null; + // 泛型基接口 + ParameterizedType parameterizedType = (ParameterizedType) type; + Type rawType = parameterizedType.getRawType(); + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + adjustTypeArguments(mapperClass, actualTypeArguments, typeArguments); + if (rawType == BaseMapper.class) { + // 找到了 + if (typeArguments[0] instanceof Class) { + return (Class) typeArguments[0]; + } + } else if (rawType instanceof Class) { + // 其他泛型基接口 + Class entityClass = getEntityClass((Class) rawType, typeArguments); + if (entityClass != null) { + return entityClass; + } + } } else if (type instanceof Class) { - return getEntityClass((Class) type); + // 其他基接口 + Class entityClass = getEntityClass((Class) type); + if (entityClass != null) { + return entityClass; + } + } + } + // 检查基类 + Class superclass = mapperClass.getSuperclass(); + if (superclass == null || superclass == Object.class) { + return null; + } + Type[] typeArguments = superclass.getTypeParameters(); + adjustTypeArguments(mapperClass, actualTypeArguments, typeArguments); + return getEntityClass(superclass, typeArguments); + } + + private static void adjustTypeArguments(Class subclass, Type[] subclassTypeArguments, Type[] typeArguments) { + for (int i = 0; i < typeArguments.length; i++) { + if (typeArguments[i] instanceof TypeVariable) { + TypeVariable typeVariable = (TypeVariable) typeArguments[i]; + TypeVariable[] typeParameters = subclass.getTypeParameters(); + for (int j = 0; j < typeParameters.length; j++) { + if (Objects.equals(typeVariable.getName(), typeParameters[j].getName())) { + typeArguments[i] = subclassTypeArguments[j]; + break; + } + } } } - return getEntityClass(mapperClass.getSuperclass()); } From d5d3de74e7f8a5a4b34be1a599561334117eb85d Mon Sep 17 00:00:00 2001 From: Faputa <2238932729@qq.com> Date: Thu, 23 Nov 2023 14:28:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mybatisflex/core/table/TableInfoFactory.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index dc7fe35e..61712630 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -115,6 +115,9 @@ public class TableInfoFactory { private static Class getEntityClass(Class mapperClass) { + if (mapperClass == null || mapperClass == Object.class) { + return null; + } return getEntityClass(mapperClass, null); }