Search in sources :

Example 1 with QueryMapper

use of indi.mybatis.flying.models.QueryMapper in project mybatis.flying by limeng32.

the class SqlBuilder method dealMapperAnnotationIterationForCount.

private static void dealMapperAnnotationIterationForCount(Object object, StringBuffer fromSql, StringBuffer whereSql, TableName originTableName, Mapperable originFieldMapper, String fieldPerfix, AtomicInteger index, TableName lastTableName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    QueryMapper queryMapper = buildQueryMapper(object.getClass(), getTableMappedClass(object.getClass()));
    TableName tableName = new TableName(tableMapper, index.getAndIncrement(), lastTableName.getMap());
    /*
		 * 在第一次遍历中,处理好fromSql。 如果originFieldMapper为null则可认为是第一次遍历
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
    }
    /*
		 * 在非第一次遍历中,处理fieldPerfix和fromSql。
		 * 如果originFieldMapper和originTableName均不为null则可认为是非第一次遍历
		 */
    String temp = null;
    if (originFieldMapper != null && originTableName != null) {
        /* 处理fieldPerfix */
        temp = originFieldMapper.getFieldName();
        if (fieldPerfix != null) {
            temp = fieldPerfix + DOT + temp;
        }
        /* 处理fromSql */
        fromSql.append(_LEFT_JOIN_).append(tableName.sqlSelect()).append(_ON_).append(originTableName.sqlWhere()).append(originFieldMapper.getDbFieldName()).append(_EQUAL_).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
    }
    /* 处理fieldMapper中的条件 */
    for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            continue;
        }
        /* 此处当value拥有TableMapper或QueryMapper标注时,开始进行迭代 */
        if (fieldMapper.isForeignKey()) {
            dealMapperAnnotationIterationForCount(value, fromSql, whereSql, tableName, fieldMapper, temp, index, tableName);
        } else {
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /* 处理queryMapper中的“且”条件 */
    for (ConditionMapper conditionMapper : queryMapper.getConditionMapperCache().values()) {
        Object value = dtoFieldMap.get(conditionMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionMapper(conditionMapper, value, whereSql, tableName, temp, false, 0);
    }
    /* 处理queryMapper中的“或”条件 */
    for (OrMapper orMapper : queryMapper.getOrMapperCache().values()) {
        Object value = dtoFieldMap.get(orMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionOrMapper(orMapper, value, whereSql, tableName, temp);
    }
}
Also used : TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) TableMapper(indi.mybatis.flying.models.TableMapper) Mapperable(indi.mybatis.flying.models.Mapperable) QueryMapper(indi.mybatis.flying.models.QueryMapper)

Example 2 with QueryMapper

use of indi.mybatis.flying.models.QueryMapper in project mybatis.flying by limeng32.

the class SqlBuilder method dealMapperAnnotationIterationForSelectAll.

private static void dealMapperAnnotationIterationForSelectAll(Object object, StringBuffer selectSql, StringBuffer fromSql, StringBuffer whereSql, TableName originTableName, Mapperable originFieldMapper, String fieldPerfix, AtomicInteger index, TableName lastTableName, String ignoreTag) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    QueryMapper queryMapper = buildQueryMapper(object.getClass(), getTableMappedClass(object.getClass()));
    TableName tableName = null;
    if (lastTableName == null) {
        tableName = new TableName(tableMapper, index.getAndIncrement(), null);
    } else {
        tableName = new TableName(tableMapper, index.getAndIncrement(), lastTableName.getMap());
    }
    /*
		 * 在第一次遍历中,处理好selectSql和fromSql。 如果originFieldMapper为null则可认为是第一次遍历
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
        for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
            if ((!fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
                selectSql.append(tableName.sqlWhere()).append(fieldMapper.getDbFieldName()).append(COMMA);
            }
        }
    }
    /*
		 * 在非第一次遍历中,处理fieldPerfix和fromSql。
		 * 如果originFieldMapper和originTableName均不为null则可认为是非第一次遍历
		 */
    String temp = null;
    if (originFieldMapper != null && originTableName != null) {
        /* 处理fieldPerfix */
        temp = originFieldMapper.getFieldName();
        if (fieldPerfix != null) {
            temp = fieldPerfix + DOT + temp;
        }
        /* 处理fromSql */
        fromSql.append(_LEFT_JOIN_).append(tableName.sqlSelect()).append(_ON_).append(originTableName.sqlWhere()).append(originFieldMapper.getDbFieldName()).append(_EQUAL_).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
    }
    /* 处理fieldMapper中的条件 */
    for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            continue;
        }
        /* 此处当value拥有TableMapper或QueryMapper标注时,开始进行迭代 */
        if (fieldMapper.isForeignKey()) {
            dealMapperAnnotationIterationForSelectAll(value, selectSql, fromSql, whereSql, tableName, fieldMapper, temp, index, tableName, null);
        } else {
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /* 处理queryMapper中的“且”条件 */
    for (ConditionMapper conditionMapper : queryMapper.getConditionMapperCache().values()) {
        Object value = dtoFieldMap.get(conditionMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionMapper(conditionMapper, value, whereSql, tableName, temp, false, 0);
    }
    /* 处理queryMapper中的“或”条件 */
    for (OrMapper orMapper : queryMapper.getOrMapperCache().values()) {
        Object value = dtoFieldMap.get(orMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionOrMapper(orMapper, value, whereSql, tableName, temp);
    }
}
Also used : TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) TableMapper(indi.mybatis.flying.models.TableMapper) Mapperable(indi.mybatis.flying.models.Mapperable) QueryMapper(indi.mybatis.flying.models.QueryMapper)

Example 3 with QueryMapper

use of indi.mybatis.flying.models.QueryMapper 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

ConditionMapper (indi.mybatis.flying.models.ConditionMapper)3 OrMapper (indi.mybatis.flying.models.OrMapper)3 QueryMapper (indi.mybatis.flying.models.QueryMapper)3 Mapperable (indi.mybatis.flying.models.Mapperable)2 TableMapper (indi.mybatis.flying.models.TableMapper)2 TableName (indi.mybatis.flying.models.TableName)2 ConditionMapperAnnotation (indi.mybatis.flying.annotations.ConditionMapperAnnotation)1 FieldMapperAnnotation (indi.mybatis.flying.annotations.FieldMapperAnnotation)1 Or (indi.mybatis.flying.annotations.Or)1 TableMapperAnnotation (indi.mybatis.flying.annotations.TableMapperAnnotation)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 WeakHashMap (java.util.WeakHashMap)1