一、super调用父类的构造方法
1、MyBatis的BaseBuilder
/**
* @author Clinton Begin
*/
public abstract class BaseBuilder {
protected final Configuration configuration;
protected final TypeAliasRegistry typeAliasRegistry;
protected final TypeHandlerRegistry typeHandlerRegistry;
public BaseBuilder(Configuration configuration) {
this.configuration = configuration;
this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
}
}
2、MyBatis的SqlSourceBuilder,也就是BaseBuilder的子类
? ? ?super调用父类的构造方法初始化configuration,实质上configuration也是SqlSourceBuilder的成员变量,不过继承了父类的就不必重复写了,super调用父类构造方法也就是对自己的configuration成员变量初始化了。初始化后就可以在parse()方法里面引用configuration了。如果不初始化,parse()方法里面configuration调用isShrinkWhitespacesInSql()方法就报NULL异常!
/**
* @author Clinton Begin
*/
public class SqlSourceBuilder extends BaseBuilder {
private static final String PARAMETER_PROPERTIES = "javaType,jdbcType,mode," +
"numericScale,resultMap,typeHandler,jdbcTypeName";
public SqlSourceBuilder(Configuration configuration) {
// 调用父类的构造方法初始化configuration,实质上configuration也是SqlSourceBuilder的成员变量,
// 不过继承了父类的就不必重复写了,super调用父类构造方法也就是对自己的configuration成员变量初始化了
super(configuration);
}
public SqlSource parse(String originalSql,
Class<?> parameterType,
Map<String, Object> additionalParameters) {
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration
, parameterType, additionalParameters);
GenericTokenParser parser = new GenericTokenParser("#{"
, "}", handler);
String sql;
if (configuration.isShrinkWhitespacesInSql()) {
sql = parser.parse(removeExtraWhitespaces(originalSql));
} else {
sql = parser.parse(originalSql);
}
return new StaticSqlSource(configuration, sql, handler.getParameterMappings());
}
}
|