diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 0f069af4..396e8040 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.mybatis; import com.mybatisflex.core.FlexConsts; +import com.mybatisflex.core.handler.CompositeEnumTypeHandler; import com.mybatisflex.core.keygen.MultiEntityKeyGenerator; import com.mybatisflex.core.keygen.MultiRowKeyGenerator; import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil; @@ -59,6 +60,7 @@ public class FlexConfiguration extends Configuration { public FlexConfiguration() { setObjectWrapperFactory(new FlexWrapperFactory()); + setDefaultEnumTypeHandler(CompositeEnumTypeHandler.class); initDefaultMappers(); } @@ -66,6 +68,7 @@ public class FlexConfiguration extends Configuration { public FlexConfiguration(Environment environment) { super(environment); setObjectWrapperFactory(new FlexWrapperFactory()); + setDefaultEnumTypeHandler(CompositeEnumTypeHandler.class); initDefaultMappers(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java index 8fa7371f..41d3ae02 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java @@ -15,9 +15,10 @@ */ package com.mybatisflex.core.table; -import com.mybatisflex.core.handler.CompositeEnumTypeHandler; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.mask.MaskTypeHandler; import com.mybatisflex.core.util.StringUtil; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; @@ -104,7 +105,7 @@ public class ColumnInfo { this.jdbcType = jdbcType; } - public TypeHandler buildTypeHandler() { + public TypeHandler buildTypeHandler(Configuration configuration) { //优先使用自定义的 typeHandler if (typeHandler != null) { @@ -112,7 +113,10 @@ public class ColumnInfo { } //枚举 else if (propertyType.isEnum()) { - typeHandler = new CompositeEnumTypeHandler(propertyType); + if (configuration == null){ + configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration(); + } + this.typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType); } //若用户未定义 typeHandler,而配置了数据脱敏,则使用脱敏的 handler 处理 else if (StringUtil.isNotBlank(maskType)) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 64304af4..ed87d6e8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -645,7 +645,7 @@ public class TableInfo { if (value != null) { ColumnInfo columnInfo = columnInfoMapping.get(column); if (columnInfo != null) { - TypeHandler typeHandler = columnInfo.buildTypeHandler(); + TypeHandler typeHandler = columnInfo.buildTypeHandler(null); if (typeHandler != null) { value = new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType()); } @@ -1083,7 +1083,7 @@ public class TableInfo { , columnInfo.propertyType) .jdbcType(columnInfo.getJdbcType()) .flags(flags) - .typeHandler(columnInfo.buildTypeHandler()) + .typeHandler(columnInfo.buildTypeHandler(configuration)) .build(); resultMappings.add(mapping); existMappingColumns.add(mapping.getColumn()); @@ -1100,7 +1100,7 @@ public class TableInfo { , columnInfo.propertyType) .jdbcType(columnInfo.getJdbcType()) .flags(flags) - .typeHandler(columnInfo.buildTypeHandler()) + .typeHandler(columnInfo.buildTypeHandler(configuration)) .build(); resultMappings.add(mapping); existMappingColumns.add(mapping.getColumn()); @@ -1115,7 +1115,7 @@ public class TableInfo { Object value = getPropertyValue(metaObject, columnInfo.property); if (value != null) { - TypeHandler typeHandler = columnInfo.buildTypeHandler(); + TypeHandler typeHandler = columnInfo.buildTypeHandler(null); if (typeHandler != null) { return new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType()); } @@ -1180,7 +1180,7 @@ public class TableInfo { private void setInstancePropertyValue(Row row, Object instance, MetaObject metaObject, ColumnInfo columnInfo, String rowKey) { Object rowValue = row.get(rowKey); - TypeHandler typeHandler = columnInfo.buildTypeHandler(); + TypeHandler typeHandler = columnInfo.buildTypeHandler(null); if (typeHandler != null) { try { //通过 typeHandler 转换数据 diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java index f19009a0..7a93c893 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -43,6 +43,8 @@ public class Account extends BaseEntity implements Serializable, AgeAware { @ColumnAlias("my_age") private int age; + private SexEnum sex; + @NotBlank private Date birthday; @@ -84,6 +86,14 @@ public class Account extends BaseEntity implements Serializable, AgeAware { this.age = age; } + public SexEnum getSex() { + return sex; + } + + public void setSex(SexEnum sex) { + this.sex = sex; + } + public Date getBirthday() { return birthday; } @@ -131,18 +141,19 @@ public class Account extends BaseEntity implements Serializable, AgeAware { this.title = title; } + @Override public String toString() { return "Account{" + "id=" + id + ", userName='" + userName + '\'' + ", age=" + age + + ", sex=" + sex + ", birthday=" + birthday + ", options=" + options + ", isDelete=" + isDelete + ", articles=" + articles + - ", title=" + title + + ", title='" + title + '\'' + '}'; } - } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java index d10e9928..adf8f126 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java @@ -83,7 +83,7 @@ public class AccountTester { @Test public void testExecutor() { - DbChain.create() + DbChain.table("tb_account") .select(ACCOUNT.ALL_COLUMNS) .from(ACCOUNT) .where(ACCOUNT.ID.ge(1)) diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/SexEnum.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/SexEnum.java index cb7cc661..b205283e 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/SexEnum.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/SexEnum.java @@ -50,4 +50,12 @@ public enum SexEnum { public void setDesc(String desc) { this.desc = desc; } + + @Override + public String toString() { + return "SexEnum{" + + "code=" + code + + ", desc='" + desc + '\'' + + '}'; + } }