首页 > 其他 > 详细

Mybatis跟踪源码的那些事(二)

时间:2021-05-08 16:31:55      阅读:25      评论:0      收藏:0      [点我收藏+]

1.我们先看下程序入口

public static void main(String[] args) {
    
    try {
     //核心配置文件 String resource
= "configuration.xml";
     //将文件写入流 InputStream inputStream
= Resources.getResourceAsStream(resource);
     //我们跟进下build() SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class); List<SysUser> list = mapper.getAll(); System.out.println(list); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

SqlSessionFactoryBuilder

  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
    //XMLConfigBuilder 读取配置文件parse(); XMLConfigBuilder parser
= new XMLConfigBuilder(inputStream, environment, properties); return build(parser.parse()); } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { inputStream.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } }

跟进parse()方法

 

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

 

跟进parseConfiguration方法,解析配置,传入的参数就是我们xml配置文件的configuration根节点。解析我们的配置文件就需要分别解析其中的各个节点。

 private void parseConfiguration(XNode root) {
    try {
      propertiesElement(root.evalNode("properties")); //issue #117 read properties first
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      settingsElement(root.evalNode("settings"));
      environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

 

Mybatis跟踪源码的那些事(二)

原文:https://www.cnblogs.com/liudingwei/p/14744979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!