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;
}
Aggregations