MyBatis源码分析,一、XmlConfigBulider

读取配置文件

在这里插入图片描述

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

创建SqlSessionFactory

将读取的配置文件流传给SqlSessionFactoryBulider,
SqlSessionFactoryBulider里面内置一个XmlConfigBulider对象,
XmlConfigBulider里内置了一个XPathParse对象,

XPathParse
XPathParse对象它调用一个createDocument方法,将mybatis-config.xml配置文件的流读取成document对象。

 private Document createDocument(InputSource inputSource) {
    // important: this must only be called AFTER common constructor
    try {
      //创建工厂对象
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
      factory.setValidating(validation);
	  
      factory.setNamespaceAware(false);
      factory.setIgnoringComments(true);
      factory.setIgnoringElementContentWhitespace(false);
      factory.setCoalescing(false);
      factory.setExpandEntityReferences(true);
	  //创建bulider对象
      DocumentBuilder builder = factory.newDocumentBuilder();
      builder.setEntityResolver(entityResolver);
      builder.setErrorHandler(new ErrorHandler() {
        @Override
        public void error(SAXParseException exception) throws SAXException {
          throw exception;
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
          throw exception;
        }

        @Override
        public void warning(SAXParseException exception) throws SAXException {
          // NOP
        }
      });
      return builder.parse(inputSource);
    } catch (Exception e) {
      throw new BuilderException("Error creating document instance.  Cause: " + e, e);
    }
  }

最后将生成的document传给XPathParse的内置对象doucument
在这里插入图片描述

XmlConfigBulider
此时XmlConfigBulider拥有了一个parser(XPathParse)对象,parser对象有了一个document(Doucument)对象,document就是Mybatis配置文件信息的对象化

然后调用XmlConfigBulider的parse()方法,创建Configuration对象

最后传给XmlConfigBulider的内置configuration属性

  public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    } 
    parsed = true;
    //从配置文件的</configuration>开始解析,
    //evalNode方法获取一个XNode对象,从XNode对象可以取出每个标签的某个属性
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值