Search in sources :

Example 1 with TableMapperAnnotation

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

the class SqlBuilder method buildTableMapper.

/**
 * 由传入的dto对象的class构建TableMapper对象,构建好的对象存入缓存中,以后使用时直接从缓存中获取
 *
 * @param dtoClass
 * @return TableMapper
 */
private static TableMapper buildTableMapper(Class<?> dtoClass) {
    Map<String, FieldMapper> fieldMapperCache = null;
    Field[] fields = dtoClass.getDeclaredFields();
    FieldMapper fieldMapper = null;
    TableMapper tableMapper = null;
    tableMapper = tableMapperCache.get(dtoClass);
    if (tableMapper != null) {
        return tableMapper;
    }
    tableMapper = new TableMapper();
    tableMapper.setClazz(dtoClass);
    List<FieldMapper> uniqueKeyList = new ArrayList<FieldMapper>();
    List<FieldMapper> opVersionLockList = new ArrayList<FieldMapper>();
    Annotation[] classAnnotations = dtoClass.getDeclaredAnnotations();
    for (Annotation an : classAnnotations) {
        if (an instanceof TableMapperAnnotation) {
            tableMapper.setTableMapperAnnotation((TableMapperAnnotation) an);
        } else if (an instanceof Table) {
            tableMapper.setTable((Table) an);
        }
    }
    fieldMapperCache = new WeakHashMap<String, FieldMapper>(16);
    for (Field field : fields) {
        fieldMapper = new FieldMapper();
        boolean b = fieldMapper.buildMapper(field);
        if (!b) {
            continue;
        }
        switch(fieldMapper.getOpLockType()) {
            case Version:
                fieldMapper.setOpVersionLock(true);
                break;
            default:
                break;
        }
        if (fieldMapper.isUniqueKey()) {
            uniqueKeyList.add(fieldMapper);
        }
        if (fieldMapper.getIgnoreTag().length > 0) {
            for (String t : fieldMapper.getIgnoreTag()) {
                fieldMapper.getIgnoreTagSet().add(t);
            }
        }
        if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
            fieldMapper.setForeignKey(true);
        }
        if (fieldMapper.isForeignKey()) {
            if (!tableMapperCache.containsKey(field.getType())) {
                buildTableMapper(field.getType());
            }
            TableMapper tm = tableMapperCache.get(field.getType());
            String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbAssociationUniqueKey()).getFieldName();
            fieldMapper.setForeignFieldName(foreignFieldName);
        }
        if (!"".equals(fieldMapper.getDbCrossedAssociationUniqueKey())) {
            fieldMapper.setCrossDbForeignKey(true);
        }
        if (fieldMapper.isCrossDbForeignKey()) {
            if (!tableMapperCache.containsKey(field.getType())) {
                buildTableMapper(field.getType());
            }
            TableMapper tm = tableMapperCache.get(field.getType());
            String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbCrossedAssociationUniqueKey()).getFieldName();
            fieldMapper.setForeignFieldName(foreignFieldName);
        }
        if (fieldMapper.isOpVersionLock()) {
            opVersionLockList.add(fieldMapper);
        }
        fieldMapperCache.put(field.getName(), fieldMapper);
    }
    tableMapper.setFieldMapperCache(fieldMapperCache);
    tableMapper.setUniqueKeyNames(uniqueKeyList.toArray(new FieldMapper[uniqueKeyList.size()]));
    tableMapper.setOpVersionLocks(opVersionLockList.toArray(new FieldMapper[opVersionLockList.size()]));
    tableMapper.buildTableName();
    tableMapperCache.put(dtoClass, tableMapper);
    return tableMapper;
}
Also used : Table(javax.persistence.Table) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ArrayList(java.util.ArrayList) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation) Field(java.lang.reflect.Field) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Aggregations

ConditionMapperAnnotation (indi.mybatis.flying.annotations.ConditionMapperAnnotation)1 FieldMapperAnnotation (indi.mybatis.flying.annotations.FieldMapperAnnotation)1 TableMapperAnnotation (indi.mybatis.flying.annotations.TableMapperAnnotation)1 FieldMapper (indi.mybatis.flying.models.FieldMapper)1 TableMapper (indi.mybatis.flying.models.TableMapper)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Table (javax.persistence.Table)1