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;
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();
}

View File

@ -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)) {

View File

@ -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 转换数据

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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))

View File

@ -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 + '\'' +
'}';
}
}