创建SqlSessionFactory; 首先读取配置文件,使用构造者模式创建SqlSessionFactory对象。
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
创建SqlSessionFactory 时序图。
核心:解析xml配置文件,解析的配置文件用Configuration对象保存。
解析配置文件核心步骤:
//从根节点解析
parseConfiguration(parser.evalNode("/configuration"));
private void parseConfiguration(XNode root) {try {//issue #117 read properties firstpropertiesElement(root.evalNode("properties"));//设置Properties settings = settingsAsProperties(root.evalNode("settings"));loadCustomVfs(settings);//自定义别名typeAliasesElement(root.evalNode("typeAliases"));//拦截器pluginElement(root.evalNode("plugins"));objectFactoryElement(root.evalNode("objectFactory"));objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));reflectorFactoryElement(root.evalNode("reflectorFactory"));settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631environmentsElement(root.evalNode("environments"));databaseIdProviderElement(root.evalNode("databaseIdProvider"));//自定义数据类型转换器typeHandlerElement(root.evalNode("typeHandlers"));//解析mapper 重要mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}}
解析顺序与配置文件中的设置信息完全一致。
Configuration对象几个主要的属性: