Search in sources :

Example 1 with EnumOrdinalTypeHandler

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;
}
Also used : Enumerated(com.qiuyj.mybatis.annotation.Enumerated) PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler) TypeHandler(org.apache.ibatis.type.TypeHandler) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler)

Example 2 with EnumOrdinalTypeHandler

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);
        }
    }
}
Also used : BaseAppException(com.littlefisher.core.exception.BaseAppException) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumTypeHandler(com.littlefisher.core.mybatis.EnumTypeHandler)

Aggregations

EnumOrdinalTypeHandler (org.apache.ibatis.type.EnumOrdinalTypeHandler)2 BaseAppException (com.littlefisher.core.exception.BaseAppException)1 EnumTypeHandler (com.littlefisher.core.mybatis.EnumTypeHandler)1 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)1 Enumerated (com.qiuyj.mybatis.annotation.Enumerated)1 EnumTypeHandler (org.apache.ibatis.type.EnumTypeHandler)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1