XMLConfigBuilder文件

1.package —— org.apache.ibatis.builder.xml

2.extends —— BaseBuilder

3.参数

    private boolean parsed; //判断是否已经解析过,使用于parse()
    private XPathParser parser;//xml配置文件Context内容
    private String environment;//环境参数
    private ReflectorFactory localReflectorFactory;

4.构造方法

    public XMLConfigBuilder(Reader reader) {
        this((Reader)reader, (String)null, (Properties)null);
    }

    public XMLConfigBuilder(Reader reader, String environment) {
        this((Reader)reader, environment, (Properties)null);
    }

    public XMLConfigBuilder(Reader reader, String environment, Properties props) {
        this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
    }
  
public XMLConfigBuilder(InputStream inputStream) { this((InputStream)inputStream, (String)null, (Properties)null); } public XMLConfigBuilder(InputStream inputStream, String environment) { this((InputStream)inputStream, environment, (Properties)null); } public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); } private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { super(new Configuration()); this.localReflectorFactory = new DefaultReflectorFactory(); ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; }

  6个public和1个private的构造方法,前6个public构造方法都是根据mybatis配置文件的文件流创建XPathParser对象,然后调用private的构造方法,显示调用了父类构造方法,然后给本地参数赋值。

5.成员方法

   public Configuration parse() {
        if (this.parsed) {//初始化时候赋值为false,判断是否解析过,Configuration是全局变量,只需要解析一次
            throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        } else {
            this.parsed = true;
        //调用下面的方法,parser.evalNode("/configuration")是解析xml配置的configuration节点的内容,得到XNode对象
this.parseConfiguration(this.parser.evalNode("/configuration")); return this.configuration; } }
  //解析xml配置的的根节点
private void parseConfiguration(XNode root) { try { this.propertiesElement(root.evalNode("properties")); Properties settings = this.settingsAsProperties(root.evalNode("settings")); this.loadCustomVfs(settings); this.typeAliasesElement(root.evalNode("typeAliases")); this.pluginElement(root.evalNode("plugins")); this.objectFactoryElement(root.evalNode("objectFactory")); this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); this.reflectorFactoryElement(root.evalNode("reflectorFactory")); this.settingsElement(settings); this.environmentsElement(root.evalNode("environments")); this.databaseIdProviderElement(root.evalNode("databaseIdProvider")); this.typeHandlerElement(root.evalNode("typeHandlers")); this.mapperElement(root.evalNode("mappers")); } catch (Exception var3) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3); } }

  parse的作用是解析mybatis-config.xml的configuration节点的内容,挨个赋值给configuration对象的属性节点,剩余方法都为私有方法,根据XNode对象来给全局变量configuration的属性进行赋值

原文地址:https://www.cnblogs.com/kongkongFabian/p/9601132.html