Mybatis源码解析之:mybatis-config.xml源码解析(一)

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值