MyBatis 學(xué)習(xí)筆記(七)---源碼分析篇---SQL的執(zhí)行過程(一)

前言

接上一篇,今天我們接著來分析MyBatis的源碼。今天的分析的核心是SQL的執(zhí)行過程。主要分為如下章節(jié)進(jìn)行分析

  1. 代理類的生成
  2. SQL的執(zhí)行過程
  3. 處理查詢結(jié)果

mapper 接口的代理類的生成過程分析

首先我們來看看mapper 接口的代理類的生成過程,如下是一個(gè)MyBatis查詢的調(diào)用實(shí)例。

 StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
 List<Student> studentList = mapper.selectByName("點(diǎn)點(diǎn)");
  • 1
  • 2

上述方法sqlSession.getMapper(StudentMapper.class) 返回的其實(shí)是StudentMapper的代理類。
接著我們來看看調(diào)用的時(shí)序圖。
在這里插入圖片描述
如上時(shí)序圖我們可知,接口的代理類(MapperProxy)最終由MapperProxyFactory通過JDK動(dòng)態(tài)代理生成。接著我們一步步分析下。

//DefaultSqlSession
  @Override
  public <T> T getMapper(Class<T> type) {
    //最后會(huì)去調(diào)用MapperRegistry.getMapper
    return configuration.<T>getMapper(type, this);
  }

如上,DefaultSqlSession直接請求拋給Configuration。

//Configuration
 public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

同樣,Configuration也是一個(gè)甩手掌柜,將請求直接拋給了MapperRegistry 這個(gè)接盤俠。
接下來我們來看看接盤俠MapperRegistry。

//*MapperRegistry
  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);
    }
  }

如上,在MapperRegistry的getMapper的方法中,首先根據(jù)配置的Mapper 獲取其對(duì)應(yīng)的MapperProxyFactory。接著調(diào)用newInstance方法返回MapperProxy。最后我們來看看MapperProxyFactory

//*MapperProxyFactory
  protected T newInstance(MapperProxy<T> mapperProxy) {
    //用JDK自帶的動(dòng)態(tài)代理生成映射器
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

如上,通過JDK自帶的動(dòng)態(tài)代理生成映射器,PS: JDK 動(dòng)態(tài)代理需要接口。
分析完了MapperProxy的生成過程,接下來我們來分析下SQL的執(zhí)行過程。

SQL的執(zhí)行過程

SQL 的執(zhí)行過程是從MapperProxy的invoke方法開始。按照慣例我們還是先看看相關(guān)的時(shí)序圖。
在這里插入圖片描述
如上圖,在MapperProxy的invoke方法里調(diào)用了MapperMethod的execute方法,該方法是真正執(zhí)行SQL,返回結(jié)果的方法。接下來我們來看看。

//*MapperProxy
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //代理以后,所有Mapper的方法調(diào)用時(shí),都會(huì)調(diào)用這個(gè)invoke方法
    //并不是任何一個(gè)方法都需要執(zhí)行調(diào)用代理對(duì)象進(jìn)行執(zhí)行,如果這個(gè)方法是Object中通用的方法(toString、hashCode等)無需執(zhí)行
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    //這里優(yōu)化了,去緩存中找MapperMethod
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //執(zhí)行
    return mapperMethod.execute(sqlSession, args);
  }

如上,這里MyBatis做了個(gè)優(yōu)化,如果緩存中有MapperMethod,則取緩存中的,如果沒有則new一個(gè)MapperMethod實(shí)例。

//*MapperProxy
  //去緩存中找MapperMethod
  private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      //找不到才去new
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

我們接著來看看MapperMethod 中的execute方法,該方法主要是通過區(qū)分各種CURD操作(insert|update|delete|select),分別調(diào)用sqlSession中的4大類方法。源碼如下:

  public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    //可以看到執(zhí)行時(shí)就是4種情況,insert|update|delete|select,分別調(diào)用SqlSession的4大類方法
    if (SqlCommandType.INSERT == command.getType()) {
//      對(duì)用戶傳入的參數(shù)進(jìn)行轉(zhuǎn)換,下同
      Object param = method.convertArgsToSqlCommandParam(args);
// 執(zhí)行插入操作,rowCountResult方法用于處理返回值
      result = rowCountResult(sqlSession.insert(command.getName(), param));
    } else if (SqlCommandType.UPDATE == command.getType()) {
      Object param = method.convertArgsToSqlCommandParam(args);
      result = rowCountResult(sqlSession.update(command.getName(), param));
    } else if (SqlCommandType.DELETE == command.getType()) {
      Object param = method.convertArgsToSqlCommandParam(args);
      result = rowCountResult(sqlSession.delete(command.getName(), param));
    }
//    根據(jù)目標(biāo)返回方法的返回類型進(jìn)行相應(yīng)的查詢操作。
    else if (SqlCommandType.SELECT == command.getType()) {
      if (method.returnsVoid() && method.hasResultHandler()) {
        /*
          如果方法返回值為void,但參數(shù)列表中包含ResultHandler,
          想通過ResultHandler的方式獲取查詢結(jié)果,而非通過返回值獲取結(jié)果
        * */
        executeWithResultHandler(sqlSession, args);
        result = null;
      } else if (method.returnsMany()) {
        //如果結(jié)果有多條記錄
        result = executeForMany(sqlSession, args);
      } else if (method.returnsMap()) {
        //如果結(jié)果是map
        result = executeForMap(sqlSession, args);
      } else {
        //否則就是一條記錄
        Object param = method.convertArgsToSqlCommandParam(args);
        result = sqlSession.selectOne(command.getName(), param);
      }
    } else {
      throw new BindingException("Unknown execution method for: " + command.getName());
    }
//    如果方法的返回值是基本類型,而返回值卻為null,此種情況下應(yīng)拋出異常
    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;
  }

如上,代碼注釋比較詳細(xì)。前面也說過了,不同的操作調(diào)用sqlSession中不同的方法。這里我重點(diǎn)分析下查詢操作。查詢的情況分為四種:

  1. 返回值為空
  2. 返回多條記錄
  3. 返回map
  4. 返回單條記錄。
    返回值為空的情況下,直接返回 result 為null。其余幾種情況內(nèi)部都調(diào)用了sqlSession 中的selectList 方法。下面我就以返回單條記錄為例進(jìn)行分析。
//DefaultSqlSession
  public <T> T selectOne(String statement, Object parameter) {
    // Popular vote was to return null on 0 results and throw exception on too many.
    //轉(zhuǎn)而去調(diào)用selectList,很簡單的,如果得到0條則返回null,得到1條則返回1條,得到多條報(bào)TooManyResultsException錯(cuò)
    // 特別需要主要的是當(dāng)沒有查詢到結(jié)果的時(shí)候就會(huì)返回null。因此一般建議在mapper中編寫resultType的時(shí)候使用包裝類型
    //而不是基本類型,比如推薦使用Integer而不是int。這樣就可以避免NPE
    List<T> list = this.<T>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;
    }
  }

如果所示,如果selectList查詢返回1條,則直接返回,如果返回多條則拋出異常,否則直接返回null。我們接著往下看.

//DefaultSqlSession
  public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
      //根據(jù)statement id找到對(duì)應(yīng)的MappedStatement
      MappedStatement ms = configuration.getMappedStatement(statement);
      //轉(zhuǎn)而用執(zhí)行器來查詢結(jié)果,注意這里傳入的ResultHandler是null
      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();
    }
  }

如上,在selectList 內(nèi)部最終調(diào)用的是SimpleExecutor (執(zhí)行器)的query方法來執(zhí)行查詢結(jié)果。我們接著往下找
enter description here
根據(jù)類圖我們不難發(fā)現(xiàn)SimpleExecutor是BaseExecutor類的子類。在BaseExecutor 類中我們找到了query 方法。

  public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    //得到綁定sql
    BoundSql boundSql = ms.getBoundSql(parameter);
    //創(chuàng)建緩存Key
    CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
    //查詢
    return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
 }

該方法主要有兩步,

  1. 得到綁定的SQL,
  2. 調(diào)用其重載query方法。
    綁定SQL的過程,我們稍后分析。我們接著來看看其重載的query方法。
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
   ····· 省略部分代碼
    try {
      //加一,這樣遞歸調(diào)用到上面的時(shí)候就不會(huì)再清局部緩存了
      queryStack++;
      //先根據(jù)cachekey從localCache去查
      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
      if (list != null) {
        //若查到localCache緩存,處理localOutputParameterCache
        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {
        //從數(shù)據(jù)庫查
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    ···· 省略部分代碼
    }
    return list;
  }

該方法核心的步驟是,首先根據(jù)cacheKey 從localCache 中去查,如果不為空的話則直接取緩存的,否則查詢數(shù)據(jù)庫。我們主要看看查詢數(shù)據(jù)庫的queryFromDatabase方法。

//BaseExecutor
  //從數(shù)據(jù)庫查
  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 {
//      調(diào)用doQuery進(jìn)行查詢
      list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
      //最后刪除占位符
      localCache.removeObject(key);
    }
    //加入緩存
    localCache.putObject(key, list);
    //如果是存儲(chǔ)過程,OUT參數(shù)也加入緩存
    if (ms.getStatementType() == StatementType.CALLABLE) {
      localOutputParameterCache.putObject(key, parameter);
    }
    return list;
  }
    //query-->queryFromDatabase-->doQuery
  protected abstract <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
      throws SQLException;

此處doQuery方法是抽象方法,定義了模板供子類實(shí)現(xiàn)。此處用到了模板模式。
首先,此方法首先調(diào)用doQuery方法執(zhí)行查詢,然后將查詢的結(jié)果放入緩存中。
接著我們再來看看SimpleExcutor中的doQuery方法。

//*SimpleExcutor
  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();
      //新建一個(gè)StatementHandler
      //這里看到ResultHandler傳入了
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      //準(zhǔn)備語句
      stmt = prepareStatement(handler, ms.getStatementLog());
      //StatementHandler.query(實(shí)際調(diào)用的是PreparedStatementHandler)
      return handler.<E>query(stmt, resultHandler);
    } finally {
//      關(guān)閉statement
      closeStatement(stmt);
    }
  }

如上,該方法主要有三步:

  1. 新建一個(gè)StatementHandler
  2. 獲取Statement
  3. StatementHandler.query(實(shí)際調(diào)用的是PreparedStatementHandler)獲取查詢結(jié)果。
    第一步比較簡單,我們首先來看看第二步
//*SimpleExcutor
  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
//   獲取數(shù)據(jù)庫連接
    Connection connection = getConnection(statementLog);
    //創(chuàng)建Statement
    stmt = handler.prepare(connection);
    //為Statement設(shè)置IN參數(shù)
    handler.parameterize(stmt);
    return stmt;
  }

對(duì)于prepareStatement方法里的相關(guān)步驟,相信大家都不會(huì)陌生。獲取數(shù)據(jù)庫連接,創(chuàng)建Statement; 為Statement設(shè)置IN參數(shù)。都是我們非常熟悉的。我們接著看看第三步。

  public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
    PreparedStatement ps = (PreparedStatement) statement;
//    執(zhí)行SQL
    ps.execute();
//    處理執(zhí)行結(jié)果
    return resultSetHandler.<E> handleResultSets(ps);
  }

這一步到了最終的執(zhí)行鏈。還是先執(zhí)行SQL,然后處理執(zhí)行結(jié)果。限于篇幅,在此不展開分析了。

總結(jié)

本文通過兩個(gè)時(shí)序圖,為主線來展開分析了Mapper接口代理類的生成過程,以及SQL的執(zhí)行過程。希望對(duì)大家有所幫助。





作者:碼農(nóng)飛哥
微信公眾號(hào):碼農(nóng)飛哥