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 2173bf9d..64a54e99 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 @@ -25,6 +25,7 @@ import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.io.ResolverUtil; import org.apache.ibatis.reflection.Reflector; +import org.apache.ibatis.reflection.TypeParameterResolver; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; @@ -216,7 +217,7 @@ public class TableInfoFactory { ) { // 集合嵌套 if (Collection.class.isAssignableFrom(fieldType)) { - Type genericType = field.getGenericType(); + Type genericType = TypeParameterResolver.resolveFieldType(field, entityClass); if (genericType instanceof ParameterizedType){ Class actualTypeArgument = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0]; //需排除 List List 等场景 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java index abc5116c..ecf93717 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java @@ -19,8 +19,8 @@ import com.mybatisflex.annotation.Table; import java.util.Date; -@Table(value = "tb_account",onSet = AccountOnSetListener.class) -public class Account extends BaseEntity { +@Table(value = "tb_account", onSet = AccountOnSetListener.class) +public class Account extends BaseEntity { /*@Id(keyType = KeyType.Auto) private Long id;*/ @@ -77,6 +77,7 @@ public class Account extends BaseEntity { ", userName='" + userName + '\'' + ", age=" + age + ", birthday=" + birthday + + ", list=" + roles + '}'; } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java index 09b7a559..350c258c 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java @@ -16,13 +16,25 @@ package com.mybatisflex.test.model; +import java.util.List; + /** * @author 王帅 * @since 2.0 */ -public class BaseEntity extends IdEntity { +public class BaseEntity extends IdEntity { protected T userName; + protected List roles; + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + public T getUserName() { return userName; diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Gender.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Gender.java index 6d2e2738..16615e92 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Gender.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Gender.java @@ -16,14 +16,16 @@ package com.mybatisflex.test.model; +import com.mybatisflex.annotation.EnumValue; + /** * @author 王帅 * @since 2023-06-14 */ public enum Gender { - MALE, FEMALE; - /*MALE(1), FEMALE(0); + // MALE, FEMALE; + MALE(1), FEMALE(0); public int getCode() { return code; @@ -34,6 +36,6 @@ public enum Gender { Gender(int code) { this.code = code; - }*/ + } } \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java new file mode 100644 index 00000000..e1d3bea1 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ReflectTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.common; + +import com.mybatisflex.core.util.ClassUtil; +import com.mybatisflex.test.model.Account; +import org.apache.ibatis.reflection.TypeParameterResolver; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; + +/** + * @author 王帅 + * @since 2023-06-14 + */ +class ReflectTest { + + @Test + void test() { + Field field = ClassUtil.getAllFields(Account.class, f -> f.getName().equals("list")).get(0); + Type type = TypeParameterResolver.resolveFieldType(field, Account.class); + System.out.println(type); + } + +} \ No newline at end of file