use of indi.mybatis.flying.annotations.Or 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;
}
Aggregations