Mybatis源码解析之mapper的创建

阅读须知

  • Mybatis源码版本:3.4.4
  • 文章中使用/* */注释的方法会做深入分析

正文

在Mybatis标签解析源码分析的文章中,我们看到,整个标签解析其实就是构建SqlSessionFactory的过程,使用时我们需要调用SqlSessionFactory的openSession获取一个SqlSession,我们可以使用SqlSession来执行我们的mapper方法或者获取mapper对象,我们来分析这个过程:
DefaultSqlSessionFactory:

public SqlSession openSession() {
    /* 构建SqlSession */
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}

DefaultSqlSessionFactory:

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = configuration.getEnvironment();
        // 获取配置的TransactionFactory,如果没有配置,默认为ManagedTransactionFactory
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        // 构建Transaction,默认为ManagedTransaction
        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
        /* 构建Executor执行器 */
        final Executor executor = configuration.newExecutor(tx, execType);
        return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
        closeTransaction(tx); // 异常关闭事务
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}

Configuration:

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    // 执行器类型,默认为SIMPLE
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    // 根据配置的不同的执行器类型构造不同的执行器
    if (ExecutorType.BATCH == executorType) {
        executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
        executor = new ReuseExecutor(this, transaction);
    } else {
        executor = new SimpleExecutor(this, transaction);
    }
    // 如果配置了cacheEnabled为true(默认为true),则用CachingExecutor包装执行器
    if (cacheEnabled) {
        executor = new CachingExecutor(executor);
    }
    // 如果我们配置了Mybatis拦截器,这里会调用拦截器的plugin方法
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
}

这里提到了Mybatis拦截器,我们会用单独文的章来分析Mybatis拦截器。CachingExecutor的作用是实现Mybatis的二级缓存,我们同样会用单独的文章来分析Mybatis的缓存,有兴趣的读者可以关注一下。构建好SqlSession后,我们可以通过SqlSession来获取mapper或者使用SqlSession的一些statement方法直接执行mapper方法,如sqlSession.select("com.xxx.dao.UserDao.getById", 1L);我们首先分析获取mapper的过程:
DefaultSqlSession:

public <T> T getMapper(Class<T> type) {
    /* 获取mapper */
    return configuration.<T>getMapper(type, this);
}

Configuration:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    /* 获取mapper */
    return mapperRegistry.getMapper(type, sqlSession);
}

MapperRegistry:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    // 在Mybatis标签解析的文章中解析mapper配置时我们看到了mapper在knownMappers中添加了映射
    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);
    }
}

MapperProxyFactory:

public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    /* 创建代理 */
    return newInstance(mapperProxy);
}

MapperProxyFactory:

protected T newInstance(MapperProxy<T> mapperProxy) {
    /* 创建代理 */
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}

这里我们看到了Mybatis用JDK的动态代理为mapper创建代理对象,到这里,mapper的创建就完成了,下篇文章我们来分析sql命令执行过程的源码。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值