MyBatis源码阅读--配置文件解析XMLConfigBuilder

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011676300/article/details/82941942

MyBatis源码阅读-总索引

MyBatis源码阅读–配置文件解析XMLConfigBuilder

XML 映射配置文件说明

前言

org.apache.ibatis.builder.xml。XMLConfigBuilder继承自抽象类BaseBuilder,用于解析MyBatis的xml配置文件,并将解析结果存放到Configuration对象,用于创建SqlSessionFactory,或者在其他情况下使用。

public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        SqlSessionFactory var5;
        try {
           //parser.parse()解析配置文件
            XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
            var5 = this.build(parser.parse());
        } catch (Exception var14) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
        } finally {
            ErrorContext.instance().reset();
            try {
                reader.close();
            } catch (IOException var13) {               ;
            }        }
        return var5;
    }

私有属性

一共有四个私有属性。

    private boolean parsed;
    private final XPathParser parser;
    private String environment;
    private final ReflectorFactory localReflectorFactory;

1.parsed 是否解析标志,因为配置文件解析一次结果即可.可以看到,如果

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

2.parser是XPathParser类型, 是用来解析xml文件的 包括检测xml文件格式。
3.environment是当前环境,在配置文件节点environments中定义。
4.localReflectorFactory是ReflectorFactory类型。

构造器

从构造器可以看出,其使用XPathParser类来将配置文件输入流解析成XPathParser对象,方便后续操作。
所有的构造器最终都是调用该构造器 private XMLConfigBuilder(XPathParser parser, String environment, Properties props);
environment : 当前的环境id.

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

解析节点

configuration 配置
--properties 属性
--settings 设置
--typeAliases 类型别名
--typeHandlers 类型处理器
--objectFactory 对象工厂
--plugins 插件
--environments 环境
--environment 环境变量
--transactionManager 事务管理器
--dataSource 数据源
--databaseIdProvider 数据库厂商标识
--mappers 映射器
 public Configuration parse() {
        if (this.parsed) {
            throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        } else {
            this.parsed = true;
            this.parseConfiguration(this.parser.evalNode("/configuration"));
            return this.configuration;
        }
    }

    private void parseConfiguration(XNode root) {
        try {
        	//properties 属性
            this.propertiesElement(root.evalNode("properties"));
            //settings 设置
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            //typeAliases 类型别名
            this.typeAliasesElement(root.evalNode("typeAliases"));
            //plugins 插件
            this.pluginElement(root.evalNode("plugins"));
            //objectFactory 对象工厂
            this.objectFactoryElement(root.evalNode("objectFactory"));
            this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
            this.settingsElement(settings);
            //environments 环境
            this.environmentsElement(root.evalNode("environments"));
            //databaseIdProvider 数据库厂商标识
            this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            //typeHandlers 类型处理器
            this.typeHandlerElement(root.evalNode("typeHandlers"));
            //mappers 映射器
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

猜你喜欢

转载自blog.csdn.net/u011676300/article/details/82941942
今日推荐