From 60da8edb7a89479223bc54582217b6f280e37c7b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 2 Mar 2024 22:44:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B2=A1=E6=9C=89=E5=85=A8=E5=B1=80=20T?= =?UTF-8?q?ypeHandler=20=E6=97=B6=E5=86=8D=E5=8E=BB=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/table/TableInfoFactory.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 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 c71f7b9b..8cea6976 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 @@ -38,7 +38,6 @@ 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.TypeException; import org.apache.ibatis.type.TypeHandler; @@ -221,7 +220,7 @@ public class TableInfoFactory { Reflector reflector = Reflectors.of(entityClass); tableInfo.setReflector(reflector); - //初始化表名 + // 初始化表名 Table table = entityClass.getAnnotation(Table.class); if (table != null) { tableInfo.setSchema(table.schema()); @@ -256,12 +255,12 @@ public class TableInfoFactory { tableInfo.setDataSource(table.dataSource()); } } else { - //默认为类名转驼峰下划线 + // 默认为类名转驼峰下划线 String tableName = StringUtil.camelToUnderline(entityClass.getSimpleName()); tableInfo.setTableName(tableName); } - //初始化字段相关 + // 初始化字段相关 List columnInfoList = new ArrayList<>(); List idInfos = new ArrayList<>(); @@ -270,13 +269,13 @@ public class TableInfoFactory { String versionColumn = null; String tenantIdColumn = null; - //数据插入时,默认插入数据字段 + // 数据插入时,默认插入数据字段 Map onInsertColumns = new HashMap<>(); - //数据更新时,默认更新内容的字段 + // 数据更新时,默认更新内容的字段 Map onUpdateColumns = new HashMap<>(); - //大字段列 + // 大字段列 Set largeColumns = new LinkedHashSet<>(); // 默认查询列 @@ -286,11 +285,16 @@ public class TableInfoFactory { FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig(); + TypeHandlerRegistry typeHandlerRegistry = null; + if (config.getConfiguration() != null) { + typeHandlerRegistry = config.getConfiguration().getTypeHandlerRegistry(); + } + for (Field field : entityFields) { Class fieldType = reflector.getGetterType(field.getName()); - //移除默认的忽略字段 + // 移除默认的忽略字段 boolean isIgnoreField = false; for (Class ignoreColumnType : ignoreColumnTypes) { if (ignoreColumnType.isAssignableFrom(fieldType)) { @@ -305,11 +309,17 @@ public class TableInfoFactory { Column columnAnnotation = field.getAnnotation(Column.class); - - //满足以下 3 种情况,不支持该类型的属性自动映射为字段 - if ((columnAnnotation == null || columnAnnotation.typeHandler() == UnknownTypeHandler.class) // 未配置 typeHandler - && !fieldType.isEnum() // 类型不是枚举 - && !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型 + /* + * 满足以下 4 种情况,不支持该类型的属性自动映射为字段 + * 1、注解上未配置 TypeHandler + * 2、类型不是枚举 + * 3、默认的自动类型不包含该类型 + * 4、没有全局 TypeHandler + */ + if ((columnAnnotation == null || columnAnnotation.typeHandler() == UnknownTypeHandler.class) + && !fieldType.isEnum() + && !defaultSupportColumnTypes.contains(fieldType) + && (typeHandlerRegistry == null || !typeHandlerRegistry.hasTypeHandler(fieldType)) ) { // 忽略 集合 实体类 解析 if (columnAnnotation != null && columnAnnotation.ignore()) { @@ -334,10 +344,10 @@ public class TableInfoFactory { continue; } - //列名 + // 列名 String columnName = getColumnName(tableInfo.isCamelToUnderline(), field, columnAnnotation); - //逻辑删除字段 + // 逻辑删除字段 if ((columnAnnotation != null && columnAnnotation.isLogicDelete()) || columnName.equals(config.getLogicDeleteColumn())) { if (logicDeleteColumn == null) { @@ -347,7 +357,7 @@ public class TableInfoFactory { } } - //乐观锁版本字段 + // 乐观锁版本字段 if ((columnAnnotation != null && columnAnnotation.version()) || columnName.equals(config.getVersionColumn())) { if (versionColumn == null) { @@ -357,7 +367,7 @@ public class TableInfoFactory { } } - //租户ID 字段 + // 租户ID 字段 if ((columnAnnotation != null && columnAnnotation.tenantId()) || columnName.equals(config.getTenantColumn())) { if (tenantIdColumn == null) { @@ -382,7 +392,7 @@ public class TableInfoFactory { largeColumns.add(columnName); } - //主键配置 + // 主键配置 Id id = field.getAnnotation(Id.class); ColumnInfo columnInfo; if (id != null) { @@ -421,22 +431,20 @@ public class TableInfoFactory { } - //typeHandler 配置 + // typeHandler 配置 if (columnAnnotation != null && columnAnnotation.typeHandler() != UnknownTypeHandler.class) { TypeHandler typeHandler = null; - //集合类型,支持泛型 - //fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I7S2YE + // 集合类型,支持泛型 + // fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I7S2YE if (Collection.class.isAssignableFrom(fieldType)) { typeHandler = createCollectionTypeHandler(entityClass, field, columnAnnotation.typeHandler(), fieldType); } - //非集合类型 + // 非集合类型 else { Class typeHandlerClass = columnAnnotation.typeHandler(); - Configuration configuration = config.getConfiguration(); - if (configuration != null) { - TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); + if (typeHandlerRegistry != null) { Class propertyType = columnInfo.getPropertyType(); JdbcType jdbcType = columnAnnotation.jdbcType(); if (jdbcType != JdbcType.UNDEFINED) {