Search in sources :

Example 1 with ConditionMapperAnnotation

use of indi.mybatis.flying.annotations.ConditionMapperAnnotation 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.dbAssociationTypeHandler());
    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());
                if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
                    conditionMapper.setDbAssociationUniqueKey(fieldMapper.getDbAssociationUniqueKey());
                    conditionMapper.setForeignKey(true);
                }
                if (conditionMapper.isForeignKey() && (!ConditionType.NullOrNot.equals(conditionMapper.getConditionType()))) {
                    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);
                }
                if (!"".equals(fieldMapper.getDbCrossedAssociationUniqueKey())) {
                    conditionMapper.setDbCrossedAssociationUniqueKey(fieldMapper.getDbCrossedAssociationUniqueKey());
                    fieldMapper.setCrossDbForeignKey(true);
                }
                if (fieldMapper.isCrossDbForeignKey()) {
                    if (!tableMapperCache.containsKey(pojoField.getType())) {
                        buildTableMapper(pojoField.getType());
                    }
                    TableMapper tm = tableMapperCache.get(pojoField.getType());
                    String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbCrossedAssociationUniqueKey()).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) 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 ConditionMapperAnnotation

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

the class SqlBuilder method buildQueryMapper.

/**
 * 由传入的dto对象的class构建TableMapper对象,构建好的对象存入缓存中,以后使用时直接从缓存中获取
 *
 * @param dtoClass
 * @param pojoClass
 * @return QueryMapper
 */
private static QueryMapper buildQueryMapper(Class<?> dtoClass, Class<?> pojoClass) {
    QueryMapper queryMapper = queryMapperCache.get(dtoClass);
    if (queryMapper != null) {
        return queryMapper;
    }
    Map<String, ConditionMapper> conditionMapperCache = new WeakHashMap<>(16);
    Map<String, OrMapper> orMapperCache = new WeakHashMap<>(4);
    Field[] fields = null;
    ConditionMapperAnnotation conditionMapperAnnotation = null;
    ConditionMapper conditionMapper = null;
    Or or = null;
    OrMapper orMapper = null;
    queryMapper = new QueryMapper();
    fields = dtoClass.getDeclaredFields();
    Annotation[] conditionAnnotations = null;
    for (Field field : fields) {
        conditionAnnotations = field.getDeclaredAnnotations();
        if (conditionAnnotations.length == 0) {
            continue;
        }
        for (Annotation an : conditionAnnotations) {
            if (an instanceof ConditionMapperAnnotation) {
                conditionMapperAnnotation = (ConditionMapperAnnotation) an;
                conditionMapper = new ConditionMapper();
                buildConditionMapper(conditionMapper, conditionMapperAnnotation, pojoClass, field);
                conditionMapperCache.put(field.getName(), conditionMapper);
            } else if (an instanceof Or) {
                or = (Or) an;
                orMapper = new OrMapper();
                orMapper.setFieldName(field.getName());
                ConditionMapper[] conditionMappers = new ConditionMapper[or.value().length];
                int i = 0;
                for (ConditionMapperAnnotation cma : or.value()) {
                    conditionMappers[i] = new ConditionMapper();
                    buildConditionMapper(conditionMappers[i], cma, pojoClass, field);
                    i++;
                }
                orMapper.setConditionMappers(conditionMappers);
                orMapperCache.put(field.getName(), orMapper);
            }
        }
    }
    queryMapper.setConditionMapperCache(conditionMapperCache);
    queryMapper.setOrMapperCache(orMapperCache);
    queryMapperCache.put(dtoClass, queryMapper);
    return queryMapper;
}
Also used : Or(indi.mybatis.flying.annotations.Or) 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) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation) OrMapper(indi.mybatis.flying.models.OrMapper) QueryMapper(indi.mybatis.flying.models.QueryMapper) WeakHashMap(java.util.WeakHashMap)

Aggregations

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