XMLConfigBuilder parse()方法解析_properties标签内容

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

}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值