use of org.apache.ibatis.type.EnumOrdinalTypeHandler in project qiuyj-code by qiuyuanjun.
the class EnumTypeConditionChecker method doCheck.
@Override
@SuppressWarnings("unchecked")
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
Enumerated enumerated = AnnotationUtils.findAnnotation(field, Enumerated.class);
if (Objects.isNull(enumerated)) {
if (Objects.isNull(preRv.fieldMethod)) {
try {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
} catch (Exception e) {
// ignore
}
}
if (Objects.nonNull(preRv.fieldMethod)) {
enumerated = AnnotationUtils.findAnnotation(preRv.fieldMethod, Enumerated.class);
}
}
if (Objects.nonNull(enumerated)) {
Class<?> type = getFieldJavaType(field);
if (type.isEnum()) {
PropertyColumnMapping enumMapping = sqlInfo.getPropertyColumnMappingByPropertyName(field.getName());
TypeHandler enumTypeHandlerType = enumerated.type() == Enumerated.ValueType.ORDINAL ? new EnumOrdinalTypeHandler(type) : new EnumTypeHandler(type);
enumMapping.setTypeHandler(enumTypeHandlerType);
enumMapping.setJdbcType(enumerated.jdbcType());
sqlInfo.setHasEnumField();
}
}
preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
return preRv;
}
use of org.apache.ibatis.type.EnumOrdinalTypeHandler in project littlefisher-system by littlefishercoder.
the class MyBatisConfig method customize.
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
String typeEnumsPackage = littleFisherProperties.getMybatis().getTypeEnumsPackage();
final Set<Class> classes = Sets.newHashSet();
if (StringUtil.isNotBlank(typeEnumsPackage)) {
String[] typeEnumsPackageArray = StringUtils.tokenizeToStringArray(typeEnumsPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
Arrays.stream(typeEnumsPackageArray).forEach(typeEnumsPackageStr -> classes.addAll(PackageUtil.scanTypePackage(typeEnumsPackage)));
// 取得类型转换注册器
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
try {
for (Class cls : classes) {
if (cls.isEnum()) {
if (IEnum.class.isAssignableFrom(cls)) {
typeHandlerRegistry.register(cls.getName(), EnumTypeHandler.class.getName());
} else {
// 使用原生 EnumOrdinalTypeHandler
typeHandlerRegistry.register(cls.getName(), EnumOrdinalTypeHandler.class.getName());
}
}
}
} catch (ClassNotFoundException e) {
throw new BaseAppException("CORE-000007", null, e);
}
}
}
Aggregations