MyBatis印象閱讀之ResultSetHandler解析

在在上一章內(nèi)容中我們還了關(guān)于KeyGenerator的技術(shù)債,下面還有這些技術(shù)債:

parameterHandler
resultSetHandler

今天我們就來償還關(guān)于resultSetHandler的內(nèi)容驶睦。

1. ResultSetHandler解析

首先這是一個(gè)接口顾瞪,我們先來看下這個(gè)源碼:

public interface ResultSetHandler {

  /**
   * 處理結(jié)果映射
   */
  <E> List<E> handleResultSets(Statement stmt) throws SQLException;

  /**
   * 處理游標(biāo)結(jié)果映射节视,我不太常用,不做展開
   */
  <E> Cursor<E> handleCursorResultSets(Statement stmt) throws SQLException;

  /**
   * 處理存儲過程結(jié)果映射退敦,我不太常用叽掘,不做展開
   */
  void handleOutputParameters(CallableStatement cs) throws SQLException;

}

這里其實(shí)我們最常用的也就一個(gè)方法:handleResultSets。

我們再來看下它的映射關(guān)系
ResultSetHandler繼承關(guān)系

別以為看到就一個(gè)繼承方法就可以松口氣道偷,打開一看這個(gè)類嚇?biāo)滥銅~~

不過還是要硬著頭皮去看缀旁,我們還是一步一步來,首先從它的構(gòu)造方法開始:

  public DefaultResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler<?> resultHandler, BoundSql boundSql,
                                 RowBounds rowBounds) {
    this.executor = executor;
    this.configuration = mappedStatement.getConfiguration();
    this.mappedStatement = mappedStatement;
    this.rowBounds = rowBounds;
    this.parameterHandler = parameterHandler;
    this.boundSql = boundSql;
    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
    this.objectFactory = configuration.getObjectFactory();
    this.reflectorFactory = configuration.getReflectorFactory();
    this.resultHandler = resultHandler;
  }

這里都還行勺鸦,我們再來看他的主要方法:handleResultSets

  @Override
  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;
    //獲取數(shù)據(jù)庫結(jié)果并巍,并裝飾了下
    ResultSetWrapper rsw = getFirstResultSet(stmt);

    //獲取自己配置的ResultMap
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    //檢查rsw與resultMapCount是否合理
    validateResultMapsCount(rsw, resultMapCount);
    //這里邏輯是遍歷resultMaps或rsw,一般來說不是存儲過程我們的返回結(jié)果只有一個(gè)Object
    //這里的理解是一個(gè)Object可能是List换途,包含多條記錄
    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);
  }

這個(gè)方法乍一看挺少懊渡,但是里面的調(diào)用比較深,所以看起來會很費(fèi)勁军拟,所以我們已理解整個(gè)思路為主剃执,而不過渡關(guān)注細(xì)節(jié)。

首先我們第一個(gè)方法: ResultSetWrapper rsw = getFirstResultSet(stmt);
這我們先不做過多深入懈息,記在技術(shù)債里肾档,只要知道這個(gè)是封裝數(shù)據(jù)庫結(jié)果的。

之后在最重要的就是handleResultSet方法辫继,我們來進(jìn)入:

  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 {
        //對應(yīng)resultMap進(jìn)入此處
        if (resultHandler == null) {
          DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
          multipleResults.add(defaultResultHandler.getResultList());
        } else {
          //這邊個(gè)人感覺除了存儲過程是不會進(jìn)來的怒见,因?yàn)橹罢f了rsw一個(gè)只有一個(gè)值,而resultHandler大多數(shù)不指定姑宽,初始都會null
          handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
        }
      }
    } finally {
      // issue #228 (close resultsets)
      closeResultSet(rsw.getResultSet());
    }
  }

這里會跳到我們下一個(gè)關(guān)鍵方法handleRowValues來處理行數(shù)據(jù):


  public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    //處理嵌套映射的情況
    if (resultMap.hasNestedResultMaps()) {
      ensureNoRowBounds();
      checkResultHandler();
      handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    } else {
      //處理簡單映射情況
      handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    }
  }

這里我們只看簡單映射的情況:

  private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
      throws SQLException {
    DefaultResultContext<Object> resultContext = new DefaultResultContext<>();
    ResultSet resultSet = rsw.getResultSet();
    //根據(jù)rowBounds選定相應(yīng)的值遣耍,這里看出我們值分頁都是在應(yīng)用層而非數(shù)據(jù)庫層
    skipRows(resultSet, rowBounds);
    while (shouldProcessMoreRows(resultContext, rowBounds) && !resultSet.isClosed() && resultSet.next()) {
      // 根據(jù)該行記錄以及 ResultMap.discriminator ,決定映射使用的 ResultMap 對象
      ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(resultSet, resultMap, null);
      // 根據(jù)最終確定的 ResultMap 對 ResultSet 中的該行記錄進(jìn)行映射炮车,得到映射后的結(jié)果對象
      Object rowValue = getRowValue(rsw, discriminatedResultMap, null);
      // 將映射創(chuàng)建的結(jié)果對象添加到 ResultHandler.resultList 中保存
      storeObject(resultHandler, resultContext, rowValue, parentMapping, resultSet);
    }
  }
  • resolveDiscriminatedResultMap方法我們在一般使用中不會進(jìn)入舵变,所以不進(jìn)行深入分析
  • getRowValue 是實(shí)際映射行數(shù)據(jù)酣溃,所以我們進(jìn)行重點(diǎn)查看
  private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    // 創(chuàng)建映射后的結(jié)果對象,一般是初始化棋傍,還沒賦值
    Object rowValue = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
    if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
      final MetaObject metaObject = configuration.newMetaObject(rowValue);
      boolean foundValues = this.useConstructorMappings;
      //判斷是否開啟自動映射功能救拉,默認(rèn)不開啟嵌套的自動映射
      if (shouldApplyAutomaticMappings(resultMap, false)) {
        //自動映射未明確的列
        foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
      }
      //映射 ResultMap 中明確映射的列
      foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
      foundValues = lazyLoader.size() > 0 || foundValues;
      rowValue = foundValues || configuration.isReturnInstanceForEmptyRow() ? rowValue : null;
    }
    return rowValue;
  }

這里的步驟就和我們自己創(chuàng)建一個(gè)結(jié)果映射對象的順序差不多了:

  • 首先實(shí)例化一個(gè)結(jié)果類
  • 開始根據(jù)結(jié)果插入進(jìn)行對應(yīng)的數(shù)據(jù)庫值
    那么下面我們就來看他是如何實(shí)現(xiàn)這個(gè)的自動化的。

先來看下如果開啟了自動化映射功能之后applyAutomaticMappings的方法:


  private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
    // 獲得 UnMappedColumnAutoMapping 數(shù)組
    List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
    boolean foundValues = false;
    if (!autoMapping.isEmpty()) {
      for (UnMappedColumnAutoMapping mapping : autoMapping) {
        //從resultSet中獲取對應(yīng)column對應(yīng)的值
        final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
        if (value != null) {
          foundValues = true;
        }
        if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
          // gcode issue #377, call setter on nulls (value is not 'found')
          //給對應(yīng)映射對象賦值
          metaObject.setValue(mapping.property, value);
        }
      }
    }
    return foundValues;
  }

這里關(guān)于createAutomaticMappings方法的邏輯我們也不深入了瘫拣,我們來看下UnMappedColumnAutoMapping理解他干了哪些就行:

private static class UnMappedColumnAutoMapping {

    /**
     * 字段名
     */
    private final String column;
    /**
     * 屬性名
     */
    private final String property;
    /**
     * TypeHandler 處理器
     */
    private final TypeHandler<?> typeHandler;
    /**
     * 是否為基本屬性
     */
    private final boolean primitive;

    public UnMappedColumnAutoMapping(String column, String property, TypeHandler<?> typeHandler, boolean primitive) {
        this.column = column;
        this.property = property;
        this.typeHandler = typeHandler;
        this.primitive = primitive;
    }

}

這里理解一下邏輯,遍歷未 mapped 的字段的名字的數(shù)組告喊,映射每一個(gè)字段在結(jié)果對象的相同名字的屬性麸拄,最終生成 UnMappedColumnAutoMapping 對象。

看到自動化映射之后黔姜,我們再找來看下默認(rèn)的映射方式applyPropertyMappings:

 private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix)
      throws SQLException {
    // 獲得 mapped 的字段的名字的數(shù)組
    final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
    boolean foundValues = false;
    final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
    for (ResultMapping propertyMapping : propertyMappings) {
      String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
      if (propertyMapping.getNestedResultMapId() != null) {
        // the user added a column attribute to a nested result map, ignore it
        column = null;
      }
      if (propertyMapping.isCompositeResult()
          || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH)))
          || propertyMapping.getResultSet() != null) {
        //獲取對應(yīng)的值
        Object value = getPropertyMappingValue(rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix);
        // issue #541 make property optional
        final String property = propertyMapping.getProperty();
        if (property == null) {
          continue;
        } else if (value == DEFERRED) {
          foundValues = true;
          continue;
        }
        if (value != null) {
          foundValues = true;
        }
        if (value != null || (configuration.isCallSettersOnNulls() && !metaObject.getSetterType(property).isPrimitive())) {
          // gcode issue #377, call setter on nulls (value is not 'found')
          metaObject.setValue(property, value);
        }
      }
    }
    return foundValues;
  }

這個(gè)方法的邏輯比較簡單拢切,通過我們設(shè)置的resultMap配置來獲取數(shù)據(jù)庫對應(yīng)的值并映射進(jìn)去。這里不管是什么方式秆吵,我們可以看到映射方法都是通過metaObject.setValue(property, value);

上面還有一個(gè)比較關(guān)鍵的方法getPropertyMappingValue:

  private Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix)
      throws SQLException {
    //嵌套查詢
    if (propertyMapping.getNestedQueryId() != null) {
      return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
    } else if (propertyMapping.getResultSet() != null) {
      //存儲過程相關(guān)淮椰,忽略
      addPendingChildRelation(rs, metaResultObject, propertyMapping);   // TODO is that OK?
      return DEFERRED;
    } else {
      //直接獲取
      final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
      final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
      return typeHandler.getResult(rs, column);
    }
  }

2.今日總結(jié)

今天我們主要分析的是MyBatis是如何自動映射結(jié)果對象的,設(shè)計(jì)到的過程也相對比較負(fù)責(zé)纳寂,中心思想就是通過resultMap的配置主穗,去數(shù)據(jù)庫取,并通過MeteObject輔助類反射進(jìn)去值毙芜。
這里我們又欠下了關(guān)于ResultSetWrapper的技術(shù)債忽媒,我們現(xiàn)在來整理下:

parameterHandler
ResultSetWrapper

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市腋粥,隨后出現(xiàn)的幾起案子晦雨,更是在濱河造成了極大的恐慌,老刑警劉巖隘冲,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件闹瞧,死亡現(xiàn)場離奇詭異,居然都是意外死亡展辞,警方通過查閱死者的電腦和手機(jī)奥邮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纵竖,“玉大人漠烧,你說我怎么就攤上這事∶移觯” “怎么了已脓?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長通殃。 經(jīng)常有香客問我葱她,道長,這世上最難降的妖魔是什么茂附? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任剖张,我火速辦了婚禮,結(jié)果婚禮上入挣,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好佑惠,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著齐疙,像睡著了一般膜楷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贞奋,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天赌厅,我揣著相機(jī)與錄音,去河邊找鬼轿塔。 笑死特愿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的勾缭。 我是一名探鬼主播揍障,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼漫拭!你這毒婦竟也來了亚兄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤采驻,失蹤者是張志新(化名)和其女友劉穎审胚,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體礼旅,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡膳叨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了痘系。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片菲嘴。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖汰翠,靈堂內(nèi)的尸體忽然破棺而出龄坪,到底是詐尸還是另有隱情,我是刑警寧澤复唤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布健田,位于F島的核電站,受9級特大地震影響佛纫,放射性物質(zhì)發(fā)生泄漏妓局。R本人自食惡果不足惜总放,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望好爬。 院中可真熱鬧局雄,春花似錦、人聲如沸存炮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽僵蛛。三九已至尚蝌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間充尉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工衣形, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留驼侠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓谆吴,卻偏偏與公主長得像倒源,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子句狼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容