mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 没有全局 TypeHandler 时再去处理属性。
This commit is contained in:
parent
85c410f8ce
commit
60da8edb7a
@ -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<ColumnInfo> columnInfoList = new ArrayList<>();
|
||||
List<IdInfo> idInfos = new ArrayList<>();
|
||||
|
||||
@ -270,13 +269,13 @@ public class TableInfoFactory {
|
||||
String versionColumn = null;
|
||||
String tenantIdColumn = null;
|
||||
|
||||
//数据插入时,默认插入数据字段
|
||||
// 数据插入时,默认插入数据字段
|
||||
Map<String, String> onInsertColumns = new HashMap<>();
|
||||
|
||||
//数据更新时,默认更新内容的字段
|
||||
// 数据更新时,默认更新内容的字段
|
||||
Map<String, String> onUpdateColumns = new HashMap<>();
|
||||
|
||||
//大字段列
|
||||
// 大字段列
|
||||
Set<String> 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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user