ResultMappingResolver类介绍
一个ResultMap元素对应一个ResultMap对象
resultMap元素中的idArg/result/association/collection元素对应一个ResultMapping对象,所有resultMap会有多个ResultMapping对象
association/collection元素对应一个内映射的ResultMap
不管是ResultMap对象还是内映射的ResultMap对象都被会加入到Configuration.resultMap属性
ration.resultMap属性
x
1
2
一个ResultMap元素对应一个ResultMap对象
3
resultMap元素中的idArg/result/association/collection元素对应一个ResultMapping对象,所有resultMap会有多个ResultMapping对象
4
association/collection元素对应一个内映射的ResultMap
5
不管是ResultMap对象还是内映射的ResultMap对象都被会加入到Configuration.resultMap属性
6
7
ResultMappingResolver类分析
public class ResultMapResolver {
private final MapperBuilderAssistant assistant;
private final String id;
private final Class<?> type;
private final String extend;
private final Discriminator discriminator;
private final List<ResultMapping> resultMappings;
private final Boolean autoMapping;
public ResultMap resolve() {
return assistant.addResultMap(this.id, this.type, this.extend, this.discriminator, this.resultMappings, this.autoMapping);
}
}
public class MapperBuilderAssistant extends BaseBuilder {
...
public ResultMap addResultMap(String id,Class<?> type,String extend,Discriminator discriminator,List<ResultMapping> resultMappings,
Boolean autoMapping) {
id = applyCurrentNamespace(id, false);
extend = applyCurrentNamespace(extend, true);
if (extend != null) {
if (!configuration.hasResultMap(extend)) {
throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
}
ResultMap resultMap = configuration.getResultMap(extend);
List<ResultMapping> extendedResultMappings = new ArrayList<ResultMapping>(resultMap.getResultMappings());
extendedResultMappings.removeAll(resultMappings);
// Remove parent constructor if this resultMap declares a constructor.
boolean declaresConstructor = false;
for (ResultMapping resultMapping : resultMappings) {
if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
declaresConstructor = true;
break;
}
}
if (declaresConstructor) {
Iterator<ResultMapping> extendedResultMappingsIter = extendedResultMappings.iterator();
while (extendedResultMappingsIter.hasNext()) {
if (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) {
extendedResultMappingsIter.remove();
}
}
}
resultMappings.addAll(extendedResultMappings);
}
// 交由ResultMap.Builder来创建ResultMap对象
ResultMap resultMap =
new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping).discriminator(discriminator).build();
// 将ResultMap加载在Configuration中
configuration.addResultMap(resultMap);
return resultMap;
}
}
x
1
public class ResultMapResolver {
2
private final MapperBuilderAssistant assistant;
3
private final String id;
4
private final Class<?> type;
5
private final String extend;
6
private final Discriminator discriminator;
7
private final List<ResultMapping> resultMappings;
8
private final Boolean autoMapping;
9
10
public ResultMap resolve() {
11
return assistant.addResultMap(this.id, this.type, this.extend, this.discriminator, this.resultMappings, this.autoMapping);
12
}
13
14
}
15
16
public class MapperBuilderAssistant extends BaseBuilder {
17
...
18
public ResultMap addResultMap(String id,Class<?> type,String extend,Discriminator discriminator,List<ResultMapping> resultMappings,
19
Boolean autoMapping) {
20
id = applyCurrentNamespace(id, false);
21
extend = applyCurrentNamespace(extend, true);
22
if (extend != null) {
23
if (!configuration.hasResultMap(extend)) {
24
throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
25
}
26
ResultMap resultMap = configuration.getResultMap(extend);
27
List<ResultMapping> extendedResultMappings = new ArrayList<ResultMapping>(resultMap.getResultMappings());
28
extendedResultMappings.removeAll(resultMappings);
29
// Remove parent constructor if this resultMap declares a constructor.
30
boolean declaresConstructor = false;
31
for (ResultMapping resultMapping : resultMappings) {
32
if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
33
declaresConstructor = true;
34
break;
35
}
36
}
37
if (declaresConstructor) {
38
Iterator<ResultMapping> extendedResultMappingsIter = extendedResultMappings.iterator();
39
while (extendedResultMappingsIter.hasNext()) {
40
if (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) {
41
extendedResultMappingsIter.remove();
42
}
43
}
44
}
45
resultMappings.addAll(extendedResultMappings);
46
}
47
// 交由ResultMap.Builder来创建ResultMap对象
48
ResultMap resultMap =
49
new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping).discriminator(discriminator).build();
50
// 将ResultMap加载在Configuration中
51
configuration.addResultMap(resultMap);
52
return resultMap;
53
}
54
55
}