fix: 修复无法配置自定义 EnumTypeHandler 的问题;close #I7WNQQ

This commit is contained in:
开源海哥 2023-08-29 10:29:50 +08:00
parent bc9085786d
commit c34834bedf
6 changed files with 37 additions and 11 deletions

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core.mybatis; package com.mybatisflex.core.mybatis;
import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.handler.CompositeEnumTypeHandler;
import com.mybatisflex.core.keygen.MultiEntityKeyGenerator; import com.mybatisflex.core.keygen.MultiEntityKeyGenerator;
import com.mybatisflex.core.keygen.MultiRowKeyGenerator; import com.mybatisflex.core.keygen.MultiRowKeyGenerator;
import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil; import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil;
@ -59,6 +60,7 @@ public class FlexConfiguration extends Configuration {
public FlexConfiguration() { public FlexConfiguration() {
setObjectWrapperFactory(new FlexWrapperFactory()); setObjectWrapperFactory(new FlexWrapperFactory());
setDefaultEnumTypeHandler(CompositeEnumTypeHandler.class);
initDefaultMappers(); initDefaultMappers();
} }
@ -66,6 +68,7 @@ public class FlexConfiguration extends Configuration {
public FlexConfiguration(Environment environment) { public FlexConfiguration(Environment environment) {
super(environment); super(environment);
setObjectWrapperFactory(new FlexWrapperFactory()); setObjectWrapperFactory(new FlexWrapperFactory());
setDefaultEnumTypeHandler(CompositeEnumTypeHandler.class);
initDefaultMappers(); initDefaultMappers();
} }

View File

@ -15,9 +15,10 @@
*/ */
package com.mybatisflex.core.table; 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.mask.MaskTypeHandler;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandler;
@ -104,7 +105,7 @@ public class ColumnInfo {
this.jdbcType = jdbcType; this.jdbcType = jdbcType;
} }
public TypeHandler buildTypeHandler() { public TypeHandler buildTypeHandler(Configuration configuration) {
//优先使用自定义的 typeHandler //优先使用自定义的 typeHandler
if (typeHandler != null) { if (typeHandler != null) {
@ -112,7 +113,10 @@ public class ColumnInfo {
} }
//枚举 //枚举
else if (propertyType.isEnum()) { else if (propertyType.isEnum()) {
typeHandler = new CompositeEnumTypeHandler(propertyType); if (configuration == null){
configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
}
this.typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
} }
//若用户未定义 typeHandler而配置了数据脱敏则使用脱敏的 handler 处理 //若用户未定义 typeHandler而配置了数据脱敏则使用脱敏的 handler 处理
else if (StringUtil.isNotBlank(maskType)) { else if (StringUtil.isNotBlank(maskType)) {

View File

@ -645,7 +645,7 @@ public class TableInfo {
if (value != null) { if (value != null) {
ColumnInfo columnInfo = columnInfoMapping.get(column); ColumnInfo columnInfo = columnInfoMapping.get(column);
if (columnInfo != null) { if (columnInfo != null) {
TypeHandler typeHandler = columnInfo.buildTypeHandler(); TypeHandler typeHandler = columnInfo.buildTypeHandler(null);
if (typeHandler != null) { if (typeHandler != null) {
value = new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType()); value = new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType());
} }
@ -1083,7 +1083,7 @@ public class TableInfo {
, columnInfo.propertyType) , columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType()) .jdbcType(columnInfo.getJdbcType())
.flags(flags) .flags(flags)
.typeHandler(columnInfo.buildTypeHandler()) .typeHandler(columnInfo.buildTypeHandler(configuration))
.build(); .build();
resultMappings.add(mapping); resultMappings.add(mapping);
existMappingColumns.add(mapping.getColumn()); existMappingColumns.add(mapping.getColumn());
@ -1100,7 +1100,7 @@ public class TableInfo {
, columnInfo.propertyType) , columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType()) .jdbcType(columnInfo.getJdbcType())
.flags(flags) .flags(flags)
.typeHandler(columnInfo.buildTypeHandler()) .typeHandler(columnInfo.buildTypeHandler(configuration))
.build(); .build();
resultMappings.add(mapping); resultMappings.add(mapping);
existMappingColumns.add(mapping.getColumn()); existMappingColumns.add(mapping.getColumn());
@ -1115,7 +1115,7 @@ public class TableInfo {
Object value = getPropertyValue(metaObject, columnInfo.property); Object value = getPropertyValue(metaObject, columnInfo.property);
if (value != null) { if (value != null) {
TypeHandler typeHandler = columnInfo.buildTypeHandler(); TypeHandler typeHandler = columnInfo.buildTypeHandler(null);
if (typeHandler != null) { if (typeHandler != null) {
return new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType()); 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) { private void setInstancePropertyValue(Row row, Object instance, MetaObject metaObject, ColumnInfo columnInfo, String rowKey) {
Object rowValue = row.get(rowKey); Object rowValue = row.get(rowKey);
TypeHandler<?> typeHandler = columnInfo.buildTypeHandler(); TypeHandler<?> typeHandler = columnInfo.buildTypeHandler(null);
if (typeHandler != null) { if (typeHandler != null) {
try { try {
//通过 typeHandler 转换数据 //通过 typeHandler 转换数据

View File

@ -43,6 +43,8 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
@ColumnAlias("my_age") @ColumnAlias("my_age")
private int age; private int age;
private SexEnum sex;
@NotBlank @NotBlank
private Date birthday; private Date birthday;
@ -84,6 +86,14 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
this.age = age; this.age = age;
} }
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
public Date getBirthday() { public Date getBirthday() {
return birthday; return birthday;
} }
@ -131,18 +141,19 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
this.title = title; this.title = title;
} }
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{" +
"id=" + id + "id=" + id +
", userName='" + userName + '\'' + ", userName='" + userName + '\'' +
", age=" + age + ", age=" + age +
", sex=" + sex +
", birthday=" + birthday + ", birthday=" + birthday +
", options=" + options + ", options=" + options +
", isDelete=" + isDelete + ", isDelete=" + isDelete +
", articles=" + articles + ", articles=" + articles +
", title=" + title + ", title='" + title + '\'' +
'}'; '}';
} }
} }

View File

@ -83,7 +83,7 @@ public class AccountTester {
@Test @Test
public void testExecutor() { public void testExecutor() {
DbChain.create() DbChain.table("tb_account")
.select(ACCOUNT.ALL_COLUMNS) .select(ACCOUNT.ALL_COLUMNS)
.from(ACCOUNT) .from(ACCOUNT)
.where(ACCOUNT.ID.ge(1)) .where(ACCOUNT.ID.ge(1))

View File

@ -50,4 +50,12 @@ public enum SexEnum {
public void setDesc(String desc) { public void setDesc(String desc) {
this.desc = desc; this.desc = desc;
} }
@Override
public String toString() {
return "SexEnum{" +
"code=" + code +
", desc='" + desc + '\'' +
'}';
}
} }