打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
mybatis源码分析(二) 执行过程

这边博客衔接上一篇mybatis的xml解析的博客,在xml解析完成之后,首先会解析成一个Configuration对象,然后创建一个DefaultSqlSessionFactory的session工厂。在这一切的准备过程完成之后,就可以开始对数据库的操作了。

首先看openSession()方法

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {    Transaction tx = null;    try {      final Environment environment = configuration.getEnvironment();      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);      final Executor executor = configuration.newExecutor(tx, execType);      return new DefaultSqlSession(configuration, executor, autoCommit);    } catch (Exception e) {      closeTransaction(tx); // may have fetched a connection so lets call close()      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);    } finally {      ErrorContext.instance().reset();    }  }

首先,根据configuration中取出的environment ,然后获取一个TransactionFactory,接着通过事务工厂新建一个事务对象,其实在这一个步骤,并没有对数据库进行操作newTransaction方法仅仅是返回了一个Transaction对象,这个对象包含了Datasource, level,autocommit这几个属性,并没有做其他操作。(这里我xml中配置了JDBC事务,具体看这个事务,而不是第三方的事务)。

接下来创建一个Executor对象

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {    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);    }    if (cacheEnabled) {      executor = new CachingExecutor(executor);    }    executor = (Executor) interceptorChain.pluginAll(executor);    return executor;  }

当前肯定是创建了一个默认的Executor,就是SimpleExecutor,然后往下,判断是否配置了开启缓存,是的话则通过装饰器模式创建一个CachingExecutor,接着调用interceptorChain.pluginAll方法返回一个被层层代理的对象,这部分在上一篇博客中分析过。返回executor对象,再接下来,new了一个DefaultSqlSession再返回,至此openSession方法就执行结束了。接下来我们就可以调用DefaultSqlSession的select或者update等方法操作数据库了,不过还是看比较主流的方法。

BlogMapper mapper = session.getMapper(BlogMapper.class);  Blog blog = mapper.selectBlog(101);

首先看getMapper方法,调用的是configuration对象中mapRegister的getMapper方法

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);    }  }

所以在上一篇博客中看到哪个addMapper方法,存放的是一个MapperProxyFactory工厂,就是因为这里每次getMapper会从对应的工厂中创建代理,这里是Proxy动态代理

@SuppressWarnings("unchecked")  protected T newInstance(MapperProxy<T> mapperProxy) {    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);  }  public T newInstance(SqlSession sqlSession) {    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);    return newInstance(mapperProxy);  }

先返回,此时已经获取到了BlogMapper的代理对象,然后执行selectBlog方法,这时候会执行到之前的代理方法中,找到之前的

MapperProxy类的invoke方法

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {    try {      if (Object.class.equals(method.getDeclaringClass())) {        return method.invoke(this, args);      } else {        return cachedInvoker(method).invoke(proxy, method, args, sqlSession);      }    } catch (Throwable t) {      throw ExceptionUtil.unwrapThrowable(t);    }  }

这里肯定不是Object类,所以执行cachedInvoker()

private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {    try {      return methodCache.computeIfAbsent(method, m -> {        if (m.isDefault()) {          try {            if (privateLookupInMethod == null) {              return new DefaultMethodInvoker(getMethodHandleJava8(method));            } else {              return new DefaultMethodInvoker(getMethodHandleJava9(method));            }          } catch (IllegalAccessException | InstantiationException | InvocationTargetException              | NoSuchMethodException e) {            throw new RuntimeException(e);          }        } else {          return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));        }      });    } catch (RuntimeException re) {      Throwable cause = re.getCause();      throw cause == null ? re : cause;    }  }

computeIfAbsent,这是jdk8的语法,大概就是看map中有没有这个key,没有就新建一个并返回新建的这个,有就直接返回,所以这里就是对方法会做一个缓存。现在是第一次执行,肯定是没有,所以会执行后面的创建方法。m.isDefault这些是兼容jdk8以上的接口的默认方法,实现是直接运行那个默认方法。

直接看PlainMethodInvoker,进入new MapperMethod

public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {    this.command = new SqlCommand(config, mapperInterface, method);    this.method = new MethodSignature(config, mapperInterface, method);  }

先看SqlCommand的创建

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {      final String methodName = method.getName();      final Class<?> declaringClass = method.getDeclaringClass();      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,          configuration);      if (ms == null) {        if (method.getAnnotation(Flush.class) != null) {          name = null;          type = SqlCommandType.FLUSH;        } else {          throw new BindingException("Invalid bound statement (not found): "              + mapperInterface.getName() + "." + methodName);        }      } else {        name = ms.getId();        type = ms.getSqlCommandType();        if (type == SqlCommandType.UNKNOWN) {          throw new BindingException("Unknown execution method for: " + name);        }      }    }

首先,先从configuration中查找出对应的MappedStatement,查找的过程是这样的,先查看当前的类是否存在对应的MappedStatement,如果有直接返回,否则从父类中查找是否有对应的MappedStatement

private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,        Class<?> declaringClass, Configuration configuration) {      String statementId = mapperInterface.getName() + "." + methodName;      if (configuration.hasStatement(statementId)) {        return configuration.getMappedStatement(statementId);      } else if (mapperInterface.equals(declaringClass)) {        return null;      }      for (Class<?> superInterface : mapperInterface.getInterfaces()) {        if (declaringClass.isAssignableFrom(superInterface)) {          MappedStatement ms = resolveMappedStatement(superInterface, methodName,              declaringClass, configuration);          if (ms != null) {            return ms;          }        }      }      return null;    }  }

SqlCommand创建完成之后,再看MethodSignature的创建

public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {      Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);      if (resolvedReturnType instanceof Class<?>) {        this.returnType = (Class<?>) resolvedReturnType;      } else if (resolvedReturnType instanceof ParameterizedType) {        this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();      } else {        this.returnType = method.getReturnType();      }      //返回类型是否是void      this.returnsVoid = void.class.equals(this.returnType);      //返回类型是否是集合      this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();      //返回类型是否是游标      this.returnsCursor = Cursor.class.equals(this.returnType);      //返回类型是否是Optional      this.returnsOptional = Optional.class.equals(this.returnType);      //如果有@Mapkey,返回mapKey      this.mapKey = getMapKey(method);      //是否是Map      this.returnsMap = this.mapKey != null;      //找到第几个参数是RowBounds      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);      //找到第几个参数是ResultHandler      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);      //参数解析器,解析@Param中的名称      this.paramNameResolver = new ParamNameResolver(configuration, method);    }

创建完成之后,返回,调用PlainMethodInvoker的invoke方法,方法中调用的是mapperMethod的execute

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);          if (method.returnsOptional()              && (result == null || !method.getReturnType().equals(result.getClass()))) {            result = Optional.ofNullable(result);          }        }        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;  }

这里看select方法

result = sqlSession.selectOne(command.getName(), param);
@Override  public <T> T selectOne(String statement, Object parameter) {    // Popular vote was to return null on 0 results and throw exception on too many.    List<T> list = this.selectList(statement, parameter);    if (list.size() == 1) {      return list.get(0);    } else if (list.size() > 1) {      throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());    } else {      return null;    }  }
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {    try {      MappedStatement ms = configuration.getMappedStatement(statement);      return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);    } catch (Exception e) {      throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);    } finally {      ErrorContext.instance().reset();    }  }

在这里调用executor的query方法,因为这里是个装饰者对象,所以看CachingExecutor的query方法

public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {    BoundSql boundSql = ms.getBoundSql(parameterObject);    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);  }
@Override  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)      throws SQLException {    Cache cache = ms.getCache();    if (cache != null) {      flushCacheIfRequired(ms);      if (ms.isUseCache() && resultHandler == null) {        ensureNoOutParams(ms, boundSql);        @SuppressWarnings("unchecked")        List<E> list = (List<E>) tcm.getObject(cache, key);        if (list == null) {          list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);          tcm.putObject(cache, key, list); // issue #578 and #116        }        return list;      }    }    return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);  }

首先从MappedStatement中获取Cache,这个Cache在解析xml的时候就已经创建了,如果获取到的不是null,那么首先执行flushCacheIfRequired,这个是通过在解析xml的时候判断是不是select决定的,除了select语句都是true,执行清除缓存,接下来从缓存中获取,如果有缓存,直接返回,如果没有,就执行查询。

接着看委托类的实现,这个委托类的实现在BaseExecutor中

public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());    if (closed) {      throw new ExecutorException("Executor was closed.");    }    if (queryStack == 0 && ms.isFlushCacheRequired()) {      clearLocalCache();    }    List<E> list;    try {      queryStack++;      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;      if (list != null) {        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);      } else {        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);      }    } finally {      queryStack--;    }    if (queryStack == 0) {      for (DeferredLoad deferredLoad : deferredLoads) {        deferredLoad.load();      }      // issue #601      deferredLoads.clear();      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {        // issue #482        clearLocalCache();      }    }    return list;  }

这里又是一个Cache,不过这个Cache是mybatis内置的Cache,这就是常说的一级缓存,而这个一级缓存的清除,从代码上看,首先是配置了LocalCacheScope是STATEMENT的时候,默认是Session,然后就是当执行了close方法的时候。

再接着往下看,如果没有命中缓存,就会继续执行查询方法

private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {    List<E> list;    localCache.putObject(key, EXECUTION_PLACEHOLDER);    try {      list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);    } finally {      localCache.removeObject(key);    }    localCache.putObject(key, list);    if (ms.getStatementType() == StatementType.CALLABLE) {      localOutputParameterCache.putObject(key, parameter);    }    return list;  }
@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.query(stmt, resultHandler);    } finally {      closeStatement(stmt);    }  }

doQuery方法是实现类中的方法,当前是SimpleExecutor,先看StatementHandler 的获取

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);    return statementHandler;  }
public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {    switch (ms.getStatementType()) {      case STATEMENT:        delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);        break;      case PREPARED:        delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);        break;      case CALLABLE:        delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);        break;      default:        throw new ExecutorException("Unknown statement type: " + ms.getStatementType());    }  }

首先看RoutingStatementHandler,是通过不同的StatementType创建不同的Handler处理器,MappedStatement 新建默认是PREPARED,CALLABLE是存储过程,STATEMENT就不说了,所以正常情况下创建的都是PreparedStatementHandler,进入构造方法

public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {    super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);  }
protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {    this.configuration = mappedStatement.getConfiguration();    this.executor = executor;    this.mappedStatement = mappedStatement;    this.rowBounds = rowBounds;    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();    this.objectFactory = configuration.getObjectFactory();    if (boundSql == null) { // issue #435, get the key before calculating the statement      generateKeys(parameterObject);      boundSql = mappedStatement.getBoundSql(parameterObject);    }    this.boundSql = boundSql;    this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);    this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);  }

重点是最后两行,参数处理器和结果处理器的创建

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {    ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);    return parameterHandler;  }
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,      ResultHandler resultHandler, BoundSql boundSql) {    ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);    resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);    return resultSetHandler;  }

创建之后,两者都有一个操作,就是使用interceptorChain.pluginAll进行了包装代理,

返回到newStatementHandler,interceptorChain.pluginAll对RoutingStatementHandler同样做了一个包装代理,继续返回

再往下看prepareStatement方法

private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {    Statement stmt;    Connection connection = getConnection(statementLog);    stmt = handler.prepare(connection, transaction.getTimeout());    handler.parameterize(stmt);    return stmt;  }

首先获取一个连接,接着执行handler的prepare方法,方法中调用的是委托类也就是PrepareStatementHandler的prepare方法,其是由父类实现的

@Override  public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {    ErrorContext.instance().sql(boundSql.getSql());    Statement statement = null;    try {      statement = instantiateStatement(connection);      setStatementTimeout(statement, transactionTimeout);      setFetchSize(statement);      return statement;    } catch (SQLException e) {      closeStatement(statement);      throw e;    } catch (Exception e) {      closeStatement(statement);      throw new ExecutorException("Error preparing statement.  Cause: " + e, e);    }  }

这就是创建了一个JDBC的statement,接下来返回继续执行parameterize方法

public void parameterize(Statement statement) throws SQLException {    parameterHandler.setParameters((PreparedStatement) statement);  }

可以看到调用的是之前创建的ParameterHandler的setParameters方法,把参数设置到statement中,这里需要注意的是,虽然ParameterHandler被plugins代理比RoutingStatementHandler晚,但是实际上ParameterHandler方法的调用是在后面,所以拦截的顺序也在后面。

再往下看,接着会调用query方法

public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {    PreparedStatement ps = (PreparedStatement) statement;    ps.execute();    return resultSetHandler.handleResultSets(ps);  }

这里就是JDBC的执行了,最后看ResultSetHandler执行的handleResultSets方法

public List<Object> handleResultSets(Statement stmt) throws SQLException {    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());    final List<Object> multipleResults = new ArrayList<>();    int resultSetCount = 0;    ResultSetWrapper rsw = getFirstResultSet(stmt);    List<ResultMap> resultMaps = mappedStatement.getResultMaps();    int resultMapCount = resultMaps.size();    validateResultMapsCount(rsw, resultMapCount);    while (rsw != null && resultMapCount > resultSetCount) {      ResultMap resultMap = resultMaps.get(resultSetCount);      handleResultSet(rsw, resultMap, multipleResults, null);      rsw = getNextResultSet(stmt);      cleanUpAfterHandlingResultSet();      resultSetCount++;    }    String[] resultSets = mappedStatement.getResultSets();    if (resultSets != null) {      while (rsw != null && resultSetCount < resultSets.length) {        ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);        if (parentMapping != null) {          String nestedResultMapId = parentMapping.getNestedResultMapId();          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);          handleResultSet(rsw, resultMap, null, parentMapping);        }        rsw = getNextResultSet(stmt);        cleanUpAfterHandlingResultSet();        resultSetCount++;      }    }    return collapseSingleResultList(multipleResults);  }

这段代码大概就是获取出resultsmap,然后对结果进行解析,所以重点是handeResultSet方法

private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {    try {      if (parentMapping != null) {        handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);      } else {        if (resultHandler == null) {          DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);          multipleResults.add(defaultResultHandler.getResultList());        } else {          handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);        }      }    } finally {      // issue #228 (close resultsets)      closeResultSet(rsw.getResultSet());    }  }

首先判断parentMapping 是否null,null就是最外层的resultMap, 然后判断是否有自定义的resultHandler,有的话用自定义的,没有就用默认的,解析完成之后返回,一路回到SimpleExecutor中,最后会执行closeStatement方法关闭连接。

返回结果,执行结束,mybatis的执行流程也就结束了。

 

关注公众号:java宝典

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
阿里面试:Mybatis中方法和SQL是怎么关联起来的呢?
Mybatis的工作原理,你了解过吗?
Mybatis之工作原理
Myabtis源码如何阅读,教你一招!!!
深入浅出Mybatis
设计模式-门面模式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服