Mybatis源码解析之mapper的创建

阅读须知

  • Mybatis源码版本:3.4.4
  • 注释规则:
    • //单行注释做普通注释
    • /**/多行注释做深入分析
  • 建议配合Mybatis源码阅读

正文

在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命令执行过程的源码。

猜你喜欢

转载自blog.csdn.net/J_java1/article/details/83508779