Mybatis原理及源码分析

原文来自:三不猴子

Mybatis原理及源码分析

作为Java程序员Mybatis应该是一个必会框架了,其源码体量只有Spring 的1/5,也是Hibernate的1/5 ,相比于其他流行框架Mybatis源码无疑是学习成本最低的,当做年轻人看的第一个框架源码,无疑是非常好的。

整体架构

对于一个陌生的事物切勿一头扎进细节里,我们先要观其大概看看架构脉络,MyBatis 分为三层架构,分别是基础支撑层、核心处理层和接口层。

Mybatis 整体架构

基础支撑层

基础支撑层是这个Mybatis框架的基建,为整个Mybatis框架提供非常基础的功能。(篇幅有限下面我们只对部分模块做简单的分析)

  1. 类型转换模块,我们在Mybatis中使用< typeAliase >标签定义一个别名就是使用类型转换模块实现的。类型转换模块最重要的功能还是实现了Mybatis中JDBC类型和Java类型之间的转换。主要体现在:

    1. 在SQL模板绑定用户参入实参的场景中,将Java类型转换成JDBC类型

    2. 在结果集中,将JDBC类型转换成Java类型。

      Mybatis类型转换
  2. 日志模块,产生日志,定位异常。

  3. 反射工具,对原生的Java反射作了一些封装。

  4. Binding模块,我们在执行Mybatis中的方法时都是通过SqlSession来获Mapper接口中的代理,这个代理将Mapper.xml文件中SQL进行关联就是通过Binding模块来实现的。值得注意点是这个过程是发生在编译期。可以将错误提前到编译期。

  5. 数据源模块,数据源对于一个ORM来说算是非常核心的组件之一了。Mybatis默认的数据源是非常出色的,Mybatis同时也支持集成第三方的数据源。

  6. 缓存模块,缓存模块的好坏直接影响这个ORM的性能。Mybatis提供了两级缓存,同时也支持第三方缓存中间件集成。

    Mybatis缓存
  7. 解析器模块,主要是config.xml配置文件解析,和mapper.xml文件解析。

  8. 事务管理模块,事务模块对数据库的事务机制进行控制,对数据库事务进行了抽象,同时也对spring框架进行了整合。详细的处理逻辑下文会结合源码详细解析。

核心处理层

核心处理层是我们在学习Mybatis原理的时候需要花80%时间的地方。核心处理层是 MyBatis 核心实现所在,其中涉及 MyBatis 的初始化以及执行一条 SQL 语句的全流程。

配置解析

MyBatis 的初始化以及执行一条 SQL 语句的全流程中也包含了配置解析,我们在现实开发中一般都是使用spring boot starter的自动配置。我们一项目启动为起点一层一层剥开Mybatis的流程。先打开org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration首先明确一点就是MybatisAutoConfiguration的目的就是要得到一个SqlSessionFactory。


  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    Configuration configuration = this.properties.getConfiguration();
    if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
      configuration = new Configuration();
    }
    if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
      for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
        customizer.customize(configuration);
      }
    }
    factory.setConfiguration(configuration);
    if (this.properties.getConfigurationProperties() != null) {
      factory.setConfigurationProperties(this.properties.getConfigurationProperties());
    }
    if (!ObjectUtils.isEmpty(this.interceptors)) {
      factory.setPlugins(this.interceptors);
    }
    if (this.databaseIdProvider != null) {
      factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
      factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
      factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      factory.setMapperLocations(this.properties.resolveMapperLocations());
    }

    return factory.getObject();
  }

这里是通过MybatisProperties里面的配置并放入到SqlSessionFactoryBean中,再由SqlSessionFactoryBean得到SqlSessionFactory。看到最后一行return factory.getObject();我们进去看看这个factory.getObject()的逻辑是如何得到一个SqlSessionFactory。

@Override
public SqlSessionFactory getObject() throws Exception {
  if (this.sqlSessionFactory == null) {
    afterPropertiesSet();
  }

  return this.sqlSessionFactory;
}

这一步没什么好说的,看看afterPropertiesSet()方法

@Override
public void afterPropertiesSet() throws Exception {
  notNull(dataSource, "Property 'dataSource' is required");
  notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
  state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
            "Property 'configuration' and 'configLocation' can not specified with together");

  this.sqlSessionFactory = buildSqlSessionFactory();
}

重点来了,看看这个buildSqlSessionFactory()方法这里的核心目的就是将configurationProperties解析到Configuration对象中。代码太长了我就不贴出来了, buildSqlSessionFactory()的逻辑我画了个图,有兴趣的小伙伴自行看一下。

Mybatis配置解析1

我们不要陷入细节之中,我们看看中点看看buildSqlSessionFactory() 方法的最后一行this.sqlSessionFactoryBuilder.build(configuration)点进去

public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

通过buildSqlSessionFactory()解析得到的Configuration对象创建一个DefaultSqlSessionFactory(config),到此我们就得到了SqlSessionFactory同时被配置成一个bean了。

我们最终操作都是SqlSession,什么时候会通过SqlSessionFactory得到一个SqlSession呢?

要解决这个问题我们回到最开始的MybatisAutoConfigurationsqlSessionTemplate(SqlSessionFactory sqlSessionFactory)这个方法,点开SqlSessionTemplate发现它是一个实现了SqlSession到这里我们猜测就是在这里SqlSessionFactory会构建一个SqlSession出来。我们进入new SqlSessionTemplate(sqlSessionFactory)看看源码。

  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
  }

再往下看,我们就看到了

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
    PersistenceExceptionTranslator exceptionTranslator) {

  notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  notNull(executorType, "Property 'executorType' is required");

  this.sqlSessionFactory = sqlSessionFactory;
  this.executorType = executorType;
  this.exceptionTranslator = exceptionTranslator;
  this.sqlSessionProxy = (SqlSession) newProxyInstance(
      SqlSessionFactory.class.getClassLoader(),
      new Class[] { SqlSession.class },
      new SqlSessionInterceptor());
}

这里通过动态代理创建了一个SqlSession。

参数映射、SQL解析

我们先看一下MapperFactoryBean类,这个类实现了FactoryBean在bean初始化的时候会调用getObject()方法我们看看这个类下重写的getObject()方法里的内容。

    @Override
  public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
  }

这里调用了sqlSessiongetMapper()方法。一层一层点进去里面返回的是一个代理对象。最后的执行是由MapperProxy执行。

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

接下来的流程我还是画个流程图,防止小伙伴们走丢。我这里的内容可能未必完全和小标题一样,我主要按照sql执行的流程讲解的。

Mybatis参数绑定

先看一下MapperProxy中的invoke方法,cachedMapperMethod()方法将MapperMethod缓存起来了。

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  try {
    if (Object.class.equals(method.getDeclaringClass())) {
      return method.invoke(this, args);
    } else if (isDefaultMethod(method)) {
      return invokeDefaultMethod(proxy, method, args);
    }
  } catch (Throwable t) {
    throw ExceptionUtil.unwrapThrowable(t);
  }
  final MapperMethod mapperMethod = cachedMapperMethod(method);
  return mapperMethod.execute(sqlSession, args);
}

 private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

我们在往下看mapperMethod.execute(sqlSession, args)方法。

public Object execute(SqlSession sqlSession, Object[] args) {
  Object result;
  switch (command.getType()) {
    case INSERT: {
    Object param = method.convertArgsToSqlCommandParam(args);
      result = rowCountResult(sqlSession.insert(command.getName(), param));
      break;
    }
    case UPDATE: {
      Object param = method.convertArgsToSqlCommandParam(args);
      result = rowCountResult(sqlSession.update(command.getName(), param));
      break;
    }
    case DELETE: {
      Object param = method.convertArgsToSqlCommandParam(args);
      result = rowCountResult(sqlSession.delete(command.getName(), param));
      break;
    }
    case SELECT:
      if (method.returnsVoid() && method.hasResultHandler()) {
        executeWithResultHandler(sqlSession, args);
        result = null;
      } else if (method.returnsMany()) {
        result = executeForMany(sqlSession, args);
      } else if (method.returnsMap()) {
        result = executeForMap(sqlSession, args);
      } else if (method.returnsCursor()) {
        result = executeForCursor(sqlSession, args);
      } else {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = sqlSession.selectOne(command.getName(), param);
      }
      break;
    case FLUSH:
      result = sqlSession.flushStatements();
      break;
    default:
      throw new BindingException("Unknown execution method for: " + command.getName());
  }
  if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
    throw new BindingException("Mapper method '" + command.getName() 
        + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
  }
  return result;
}

method.convertArgsToSqlCommandParam(args)这里就是处理参数转换的逻辑。还有很多细节由于篇幅有限以及时间仓促我们不做过多的赘述,感兴趣的小伙伴可以结合上面的图自己看看。下面我们看SQL的执行流程是怎么样的。整体流程如下图。

Mybatis执行流程

我们就不对每一个执行器都分析,我只挑一个SimpleExecutor来具体跟一下源码。我们还是先看看图吧,防止自己把自己搞蒙。

以simpleExecutor为例的执行流程
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.<E>query(stmt, resultHandler);
  } finally {
    closeStatement(stmt);
  }
}

这里获取了Configuration,创建了一个StatementHandler,预处理操作,具体执行的根据创建的预处理方法,最后执行query方法

@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  String sql = boundSql.getSql();
  statement.execute(sql);
  return resultSetHandler.<E>handleResultSets(statement);
}

到此我们整理了整个Mybatis的执行流程,分析了其中的源码,由于篇幅有限很多地方都没有细致的分析,但是也贴出了图,希望能帮助到你。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,585评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,923评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,314评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,346评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,718评论 3 291
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,828评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,020评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,758评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,486评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,722评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,196评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,546评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,211评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,132评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,916评论 0 200
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,904评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,758评论 2 274

推荐阅读更多精彩内容