1.从SqlSessionFactoryBuilder().build进入
@Test public void test1() { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); CommonMapper mapper = sqlSession.getMapper(CommonMapper.class); System.out.println(mapper.queryAreaByAreaCode(new HashMap())); }
2.把传进来的inputStream转换成Document对象
public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) { commonConstructor(validation, variables, entityResolver); this.document = createDocument(new InputSource(inputStream)); }
3.转换完成后开始解析文件
/** * 解析配置文件的入口 * @return Configuration 对象 */ public Configuration parse() { // 不允许重复解析 if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; // 从根节点开始解析 parseConfiguration(parser.evalNode("/configuration")); return configuration; }
4.首先解析properties,以保证在解析其他节点时便可以生效;将db.properties内容存入到Configuration大对象中,Properties variables 属性
/** * 从根节点configuration开始解析下层节点 * @param root 根节点configuration节点 */ private void parseConfiguration(XNode root) { try { // 解析信息放入Configuration // 1.首先解析properties,以保证在解析其他节点时便可以生效 // 将db.properties内容存入到Configuration大对象中,Properties variables 属性 propertiesElement(root.evalNode("properties")); //2.settings标签解析 Properties settings = settingsAsProperties(root.evalNode("settings")); //3.加载自定义类扫描器 loadCustomVfs(settings); //4.log解析 loadCustomLogImpl(settings); //5.别名扫描注册 ----- 重点 typeAliasesElement(root.evalNode("typeAliases")); //6.解析<plugins>节点,组成拦截器链 pluginElement(root.evalNode("plugins")); //7.解析Pojo对象工厂类 objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); reflectorFactoryElement(root.evalNode("reflectorFactory")); //8.解析settings标签 settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 //9.解析环境标签 ----- 重点 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); //10.解析类型转换器 ----- 重点 typeHandlerElement(root.evalNode("typeHandlers")); //11.解析mappers ----- 重点 mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
//将db.properties内容存入到Configuration大对象中,Properties variables 属性 private void propertiesElement(XNode context) throws Exception { if (context != null) { Properties defaults = context.getChildrenAsProperties(); String resource = context.getStringAttribute("resource"); String url = context.getStringAttribute("url"); if (resource != null && url != null) { throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other."); } if (resource != null) { defaults.putAll(Resources.getResourceAsProperties(resource)); } else if (url != null) { defaults.putAll(Resources.getUrlAsProperties(url)); } Properties vars = configuration.getVariables(); if (vars != null) { defaults.putAll(vars); } parser.setVariables(defaults); configuration.setVariables(defaults); } }
5.settings标签解析,加载自定义类扫描器(静态内部类,线程互斥,用于下面别名的扫描),log解析。
6.typeAliasesElement(root.evalNode("typeAliases"));别名扫描注册
private void typeAliasesElement(XNode parent) { if (parent != null) { for (XNode child : parent.getChildren()) { if ("package".equals(child.getName())) { String typeAliasPackage = child.getStringAttribute("name"); configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage); } else { String alias = child.getStringAttribute("alias"); String type = child.getStringAttr