fix: 没有全局 TypeHandler 时再去处理属性。

This commit is contained in:
Suomm 2024-03-02 22:44:43 +08:00
parent 85c410f8ce
commit 60da8edb7a

View File

@ -38,7 +38,6 @@ import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.io.ResolverUtil; import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.reflection.Reflector; import org.apache.ibatis.reflection.Reflector;
import org.apache.ibatis.reflection.TypeParameterResolver; import org.apache.ibatis.reflection.TypeParameterResolver;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeException; import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandler;
@ -221,7 +220,7 @@ public class TableInfoFactory {
Reflector reflector = Reflectors.of(entityClass); Reflector reflector = Reflectors.of(entityClass);
tableInfo.setReflector(reflector); tableInfo.setReflector(reflector);
//初始化表名 // 初始化表名
Table table = entityClass.getAnnotation(Table.class); Table table = entityClass.getAnnotation(Table.class);
if (table != null) { if (table != null) {
tableInfo.setSchema(table.schema()); tableInfo.setSchema(table.schema());
@ -256,12 +255,12 @@ public class TableInfoFactory {
tableInfo.setDataSource(table.dataSource()); tableInfo.setDataSource(table.dataSource());
} }
} else { } else {
//默认为类名转驼峰下划线 // 默认为类名转驼峰下划线
String tableName = StringUtil.camelToUnderline(entityClass.getSimpleName()); String tableName = StringUtil.camelToUnderline(entityClass.getSimpleName());
tableInfo.setTableName(tableName); tableInfo.setTableName(tableName);
} }
//初始化字段相关 // 初始化字段相关
List<ColumnInfo> columnInfoList = new ArrayList<>(); List<ColumnInfo> columnInfoList = new ArrayList<>();
List<IdInfo> idInfos = new ArrayList<>(); List<IdInfo> idInfos = new ArrayList<>();
@ -270,13 +269,13 @@ public class TableInfoFactory {
String versionColumn = null; String versionColumn = null;
String tenantIdColumn = null; String tenantIdColumn = null;
//数据插入时默认插入数据字段 // 数据插入时默认插入数据字段
Map<String, String> onInsertColumns = new HashMap<>(); Map<String, String> onInsertColumns = new HashMap<>();
//数据更新时默认更新内容的字段 // 数据更新时默认更新内容的字段
Map<String, String> onUpdateColumns = new HashMap<>(); Map<String, String> onUpdateColumns = new HashMap<>();
//大字段列 // 大字段列
Set<String> largeColumns = new LinkedHashSet<>(); Set<String> largeColumns = new LinkedHashSet<>();
// 默认查询列 // 默认查询列
@ -286,11 +285,16 @@ public class TableInfoFactory {
FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig(); FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig();
TypeHandlerRegistry typeHandlerRegistry = null;
if (config.getConfiguration() != null) {
typeHandlerRegistry = config.getConfiguration().getTypeHandlerRegistry();
}
for (Field field : entityFields) { for (Field field : entityFields) {
Class<?> fieldType = reflector.getGetterType(field.getName()); Class<?> fieldType = reflector.getGetterType(field.getName());
//移除默认的忽略字段 // 移除默认的忽略字段
boolean isIgnoreField = false; boolean isIgnoreField = false;
for (Class<?> ignoreColumnType : ignoreColumnTypes) { for (Class<?> ignoreColumnType : ignoreColumnTypes) {
if (ignoreColumnType.isAssignableFrom(fieldType)) { if (ignoreColumnType.isAssignableFrom(fieldType)) {
@ -305,11 +309,17 @@ public class TableInfoFactory {
Column columnAnnotation = field.getAnnotation(Column.class); Column columnAnnotation = field.getAnnotation(Column.class);
/*
//满足以下 3 种情况不支持该类型的属性自动映射为字段 * 满足以下 4 种情况不支持该类型的属性自动映射为字段
if ((columnAnnotation == null || columnAnnotation.typeHandler() == UnknownTypeHandler.class) // 未配置 typeHandler * 1注解上未配置 TypeHandler
&& !fieldType.isEnum() // 类型不是枚举 * 2类型不是枚举
&& !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型 * 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()) { if (columnAnnotation != null && columnAnnotation.ignore()) {
@ -334,10 +344,10 @@ public class TableInfoFactory {
continue; continue;
} }
//列名 // 列名
String columnName = getColumnName(tableInfo.isCamelToUnderline(), field, columnAnnotation); String columnName = getColumnName(tableInfo.isCamelToUnderline(), field, columnAnnotation);
//逻辑删除字段 // 逻辑删除字段
if ((columnAnnotation != null && columnAnnotation.isLogicDelete()) if ((columnAnnotation != null && columnAnnotation.isLogicDelete())
|| columnName.equals(config.getLogicDeleteColumn())) { || columnName.equals(config.getLogicDeleteColumn())) {
if (logicDeleteColumn == null) { if (logicDeleteColumn == null) {
@ -347,7 +357,7 @@ public class TableInfoFactory {
} }
} }
//乐观锁版本字段 // 乐观锁版本字段
if ((columnAnnotation != null && columnAnnotation.version()) if ((columnAnnotation != null && columnAnnotation.version())
|| columnName.equals(config.getVersionColumn())) { || columnName.equals(config.getVersionColumn())) {
if (versionColumn == null) { if (versionColumn == null) {
@ -357,7 +367,7 @@ public class TableInfoFactory {
} }
} }
//租户ID 字段 // 租户ID 字段
if ((columnAnnotation != null && columnAnnotation.tenantId()) if ((columnAnnotation != null && columnAnnotation.tenantId())
|| columnName.equals(config.getTenantColumn())) { || columnName.equals(config.getTenantColumn())) {
if (tenantIdColumn == null) { if (tenantIdColumn == null) {
@ -382,7 +392,7 @@ public class TableInfoFactory {
largeColumns.add(columnName); largeColumns.add(columnName);
} }
//主键配置 // 主键配置
Id id = field.getAnnotation(Id.class); Id id = field.getAnnotation(Id.class);
ColumnInfo columnInfo; ColumnInfo columnInfo;
if (id != null) { if (id != null) {
@ -421,22 +431,20 @@ public class TableInfoFactory {
} }
//typeHandler 配置 // typeHandler 配置
if (columnAnnotation != null && columnAnnotation.typeHandler() != UnknownTypeHandler.class) { if (columnAnnotation != null && columnAnnotation.typeHandler() != UnknownTypeHandler.class) {
TypeHandler<?> typeHandler = null; 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)) { if (Collection.class.isAssignableFrom(fieldType)) {
typeHandler = createCollectionTypeHandler(entityClass, field, columnAnnotation.typeHandler(), fieldType); typeHandler = createCollectionTypeHandler(entityClass, field, columnAnnotation.typeHandler(), fieldType);
} }
//非集合类型 // 非集合类型
else { else {
Class<?> typeHandlerClass = columnAnnotation.typeHandler(); Class<?> typeHandlerClass = columnAnnotation.typeHandler();
Configuration configuration = config.getConfiguration(); if (typeHandlerRegistry != null) {
if (configuration != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
Class<?> propertyType = columnInfo.getPropertyType(); Class<?> propertyType = columnInfo.getPropertyType();
JdbcType jdbcType = columnAnnotation.jdbcType(); JdbcType jdbcType = columnAnnotation.jdbcType();
if (jdbcType != JdbcType.UNDEFINED) { if (jdbcType != JdbcType.UNDEFINED) {