Search in sources :

Example 1 with FieldMapperAnnotation

use of indi.mybatis.flying.annotations.FieldMapperAnnotation in project mybatis.flying by limeng32.

the class SqlBuilder method buildConditionMapper.

private static void buildConditionMapper(ConditionMapper conditionMapper, ConditionMapperAnnotation conditionMapperAnnotation, Class<?> pojoClass, Field field) {
    conditionMapper.setFieldName(field.getName());
    conditionMapper.setDbFieldName(conditionMapperAnnotation.dbFieldName());
    conditionMapper.setConditionType(conditionMapperAnnotation.conditionType());
    conditionMapper.setSubTarget(conditionMapperAnnotation.subTarget());
    conditionMapper.setTypeHandlerPath(conditionMapperAnnotation.customTypeHandler());
    for (Field pojoField : pojoClass.getDeclaredFields()) {
        for (Annotation oan : pojoField.getDeclaredAnnotations()) {
            boolean b1 = oan instanceof FieldMapperAnnotation && ((FieldMapperAnnotation) oan).dbFieldName().equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b2 = oan instanceof Column && (FieldMapper.getColumnName((Column) oan, pojoField)).equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b3 = (conditionMapper.getSubTarget() != null) && (!Void.class.equals(conditionMapper.getSubTarget()));
            if (b1 || b2 || b3) {
                FieldMapper fieldMapper = new FieldMapper();
                if (b3) {
                    if (!tableMapperCache.containsKey(conditionMapper.getSubTarget())) {
                        buildTableMapper(conditionMapper.getSubTarget());
                    }
                    TableMapper tableMapper = tableMapperCache.get(conditionMapper.getSubTarget());
                    Map<String, FieldMapper> fieldMapperCache = tableMapper.getFieldMapperCache();
                    for (Map.Entry<String, FieldMapper> e : fieldMapperCache.entrySet()) {
                        if (conditionMapper.getDbFieldName().equalsIgnoreCase(e.getValue().getDbFieldName())) {
                            fieldMapper = e.getValue();
                            break;
                        }
                    }
                } else {
                    fieldMapper = new FieldMapper();
                    fieldMapper.buildMapper(pojoField);
                }
                conditionMapper.setFieldType(fieldMapper.getFieldType());
                conditionMapper.setJdbcType(fieldMapper.getJdbcType());
                conditionMapper.setCryptKeyColumn(fieldMapper.getCryptKeyColumn());
                conditionMapper.setCryptKeyAddition(fieldMapper.getCryptKeyAddition());
                conditionMapper.setDbFieldNameForJoin(fieldMapper.getDbFieldNameForJoin());
                if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
                    conditionMapper.setDbAssociationUniqueKey(fieldMapper.getDbAssociationUniqueKey());
                    conditionMapper.setForeignKey(true);
                }
                if (conditionMapper.isForeignKey() && (!ConditionType.NULL_OR_NOT.equals(conditionMapper.getConditionType())) && !fieldMapper.isDelegate()) {
                    if (!tableMapperCache.containsKey(pojoField.getType())) {
                        buildTableMapper(pojoField.getType());
                    }
                    TableMapper tm = tableMapperCache.get(pojoField.getType());
                    String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbAssociationUniqueKey()).getFieldName();
                    conditionMapper.setForeignFieldName(foreignFieldName);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Column(javax.persistence.Column) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation)

Example 2 with FieldMapperAnnotation

use of indi.mybatis.flying.annotations.FieldMapperAnnotation in project mybatis.flying by limeng32.

the class FieldMapper method buildMapper.

public boolean buildMapper(Field field) {
    Annotation[] fieldAnnotations = field.getDeclaredAnnotations();
    if (fieldAnnotations.length == 0) {
        return false;
    }
    boolean b = false;
    for (Annotation an1 : fieldAnnotations) {
        if (an1 instanceof Id) {
            setId((Id) an1);
        }
        if ((an1 instanceof FieldMapperAnnotation) || (an1 instanceof Column)) {
            b = true;
            setField(field);
            if (an1 instanceof FieldMapperAnnotation) {
                setFieldMapperAnnotation((FieldMapperAnnotation) an1);
            } else if (an1 instanceof Column) {
                setColumn((Column) an1);
            }
        }
    }
    if (b) {
        buildMapper();
    }
    return b;
}
Also used : Column(javax.persistence.Column) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Id(javax.persistence.Id) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation)

Aggregations

FieldMapperAnnotation (indi.mybatis.flying.annotations.FieldMapperAnnotation)2 Annotation (java.lang.annotation.Annotation)2 Column (javax.persistence.Column)2 ConditionMapperAnnotation (indi.mybatis.flying.annotations.ConditionMapperAnnotation)1 TableMapperAnnotation (indi.mybatis.flying.annotations.TableMapperAnnotation)1 FieldMapper (indi.mybatis.flying.models.FieldMapper)1 TableMapper (indi.mybatis.flying.models.TableMapper)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Id (javax.persistence.Id)1