XMLConfigBuilder 用来解析mybatis-config.xml文件
public Configuration parse() {
if (parsed) { //每个XMLConfigBuilder只能使用一次
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
//XmlPathPaser.evalNode("/configuration")
//evalNode(),将要解析的标签名称传进去可以获得和xml文件中一样的内容
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
private void parseConfiguration(XNode root) {
try {
propertiesElement(root.evalNode("properties"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
//xml定义: <properties resource="jdbc.properties"></properties>
//context参数值: <properties resource="jdbc.properties"/>
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
//获取<properties>子节点, 结果为 {} --空集合
Properties defaults = context.getChildrenAsProperties();
//获取resource属性值, 结果为: jdbc.properties
String resource = context.getStringAttribute("resource");
//获取url, 未定义, 结果为: null
String url = context.getStringAttribute("url");
//<properties> url 和 resource 属性不能同时使用
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
//本次执行1位置
if (resource != null) {//1
//读取jdbc.properties, 通过putAll()方法添加进defaults,前面用来放子节点的, 结果如下:
//{user=root, url=jdbc:oracle:thin:@localhost:orcl, password=root, wdriverClass=com.mysql.jdbc.Driver}
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {//2
defaults.putAll(Resources.getUrlAsProperties(url));
}
//获取Configration的配置的参数,我这里为: null
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
//将解析书来的变量设置到两个对象属性中
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
XMLConfigBuilder parse()方法解析_properties标签内容
最新推荐文章于 2022-12-03 10:53:10 发布