• new SqlSessionFactoryBuilder().build(inputStream, properties)


    SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream, properties);

      public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
        this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
      }


    this中的处理逻辑:

    new XPathParser(inputStream, true, props, new XMLMapperEntityResolver())

    org.apache.ibatis.parsing.XPathParser

      public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
        commonConstructor(validation, variables, entityResolver);
        this.document = createDocument(new InputSource(inputStream));
      }
      private void commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {
        this.validation = validation;
        this.entityResolver = entityResolver;
        this.variables = variables;
        XPathFactory factory = XPathFactory.newInstance();
        this.xpath = factory.newXPath();
      }

       返回一个org.w3c.dom.Document

      private Document createDocument(InputSource inputSource) {
        // important: this must only be called AFTER common constructor
        try {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setValidating(validation);
    
          factory.setNamespaceAware(false);
          factory.setIgnoringComments(true);
          factory.setIgnoringElementContentWhitespace(false);
          factory.setCoalescing(false);
          factory.setExpandEntityReferences(true);
    
          DocumentBuilder builder = factory.newDocumentBuilder();
          builder.setEntityResolver(entityResolver);
          builder.setErrorHandler(new ErrorHandler() {
            public void error(SAXParseException exception) throws SAXException {
              throw exception;
            }
    
            public void fatalError(SAXParseException exception) throws SAXException {
              throw exception;
            }
    
            public void warning(SAXParseException exception) throws SAXException {
            }
          });
          return builder.parse(inputSource);
        } catch (Exception e) {
          throw new BuilderException("Error creating document instance.  Cause: " + e, e);
        }
      }

    com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl

        public Document parse(InputSource is) throws SAXException, IOException {
            if (is == null) {
                throw new IllegalArgumentException(
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
                    "jaxp-null-input-source", null));
            }
            if (fSchemaValidator != null) {
                if (fSchemaValidationManager != null) {
                    fSchemaValidationManager.reset();
                    fUnparsedEntityHandler.reset();
                }
                resetSchemaValidator();
            }
            domParser.parse(is);
            Document doc = domParser.getDocument();
            domParser.dropDocumentReferences();
            return doc;
        }

    继续org.apache.ibatis.builder.xml.XMLConfigBuilder中的处理:

      private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
        super(new Configuration());
        ErrorContext.instance().resource("SQL Mapper Configuration");
        this.configuration.setVariables(props);
        this.parsed = false;
        this.environment = environment;
        this.parser = parser;
      }
    parser.evalNode("/configuration"):

    org.apache.ibatis.parsing.XPathParser
    public XNode evalNode(String expression)

      public Configuration parse() {
        if (parsed) {
          throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        }
        parsed = true;
        parseConfiguration(parser.evalNode("/configuration"));//
        return configuration;
      }

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <setting name="logImpl" value="LOG4J"/>
        </settings>
        <typeAliases>
            <package name="com.tangcheng.mybatis.introduction.domain"/>
        </typeAliases>
    
        <environments default="dev">
            <environment id="dev">
                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="UNPOOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                    <property name="username" value="root"/>
                    <property name="password" value=""/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="mapper"/> //更改成 <mapper resource="mapper/CountryMapper.xml"/> 错误即可消失
        </mappers>
    
    </configuration>
    Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:259)
    	at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:125)
    	at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:78)
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:339)
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:113)
    	... 23 more
    Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    	at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
    	at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    	at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:994)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
    	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    	at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
    	at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:257)
    	... 27 more

    参考:
    http://blog.csdn.net/Yhboom/article/details/75041618

  • 相关阅读:
    Istio Ambient Mesh七层服务治理图文详解
    云数据库时代,DBA将走向何方?
    云数据库 GaussDB(for Influx) 解密第十一期:让智能电网中时序数据处理更高效
    ModelBox姿态匹配:抖抖手动动脚勤做深呼吸
    持续优化,欣欣向云 | RocketMQ Operator 0.3.0 正式发布
    Nacos 多语言体系再添新员:首个 Rust 语言版本正式发布!
    实战指南 | Serverless 架构下的应用开发
    大数据生态中的 RocketMQ 5.0
    Ubuntu不同于CentOS的命令
    orchestrator/raft一致性集群
  • 原文地址:https://www.cnblogs.com/softidea/p/5773191.html
Copyright © 2020-2023  润新知