最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于mybatis-plus timestamp返回為null問題的排除

 更新時間:2021年08月31日 11:58:39   作者:水中加點糖  
這篇文章主要介紹了mybatis-plus timestamp返回為null問題的排除,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

問題是這樣的

在開發(fā)時,為了節(jié)約時間,我選擇了mybatis框架來開發(fā),然后又在網上找了一個許多人都推薦的mybatis-plus來作為持久層框架。

于是乎我按照官方的DEMO下了一個springBoot的mybatis-plus版本的DEMO

這個DEMO是基于H2數(shù)據庫的,跑了下沒有問題。DEMO是能正常運行的。

然后我將這個工程的代碼快速拷貝的新的一個工程里,并把數(shù)據庫由H2換為了MYSQL。但項目跑起來時,出現(xiàn)了如下問題:

數(shù)據庫里的數(shù)據如下圖

表結構如下圖

查詢出來的結果中對于timestamp類型的字段為空

為了解決這個問題,我決定通過debug斷點觀察下是否查詢是把數(shù)據數(shù)據查出來了

由于用的mybatis-plus框架來開發(fā),而mybaits-plus框架是基于mybatis框架的,為了看是否把數(shù)據查詢出來,直接在ResultSetHandler上把一個dubug就好。在默認情況下,mybatis框架使用的ResultSetHandler為DefaultResultSetHandler,當查詢mybatis查詢完畢后,會通過ResultSetHandler的handleResultSets(Statement stmt)方法對查詢的數(shù)據結果集進行封裝

所以將斷點打在handlerResultSets方法上最為合適。

再通過statement -> wrapper ->results -> rowData ->rows觀察發(fā)現(xiàn)如下數(shù)據:

查詢返回的結果集中rows的記錄數(shù)為1,第1個字段的ascii為49,而49是ascii中數(shù)字1的值。而第二個字段也有值,說明所對應的timestamp字段是有返回值的,數(shù)據庫查詢是沒有問題的。

然而通過mybatis代碼進一步封裝后的數(shù)據multipleResults又表示,只查詢到了類型為Int的字段的數(shù)據

這么也就是說,問題應該是出在了multipleResults的賦值問題上了

handleResultSets的完整代碼為

//
// HANDLE RESULT SETS
//
@Override
public List<Object> handleResultSets(Statement stmt) throws SQLException {
  ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
  final List<Object> multipleResults = new ArrayList<Object>();
  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);
}

說明問題出在 handleResultSet(rsw, resultMap, multipleResults, null);這句代碼上了

通過代碼跟蹤,發(fā)現(xiàn)如下代碼

//
// GET VALUE FROM ROW FOR SIMPLE RESULT MAP
//
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
  final ResultLoaderMap lazyLoader = new ResultLoaderMap();
  Object rowValue = createResultObject(rsw, resultMap, lazyLoader, null);
  if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
    final MetaObject metaObject = configuration.newMetaObject(rowValue);
    boolean foundValues = this.useConstructorMappings;
    if (shouldApplyAutomaticMappings(resultMap, false)) {
      foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
    }
    foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
    foundValues = lazyLoader.size() > 0 || foundValues;
    rowValue = (foundValues || configuration.isReturnInstanceForEmptyRow()) ? rowValue : null;
  }
  return rowValue;
}

繼而發(fā)現(xiàn)如下的核心代碼

private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
  List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
  boolean foundValues = false;
  if (!autoMapping.isEmpty()) {
    for (UnMappedColumnAutoMapping mapping : autoMapping) {
      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')
        metaObject.setValue(mapping.property, value);
      }
    }
  }
  return foundValues;
}

通過斷點發(fā)現(xiàn)以下數(shù)據

在獲取這個查詢的的返回字段時,只獲取出來兩個,即int類型和varchar類型的.

再通過跟蹤發(fā)現(xiàn)了如下代碼

private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
  final String mapKey = resultMap.getId() + ":" + columnPrefix;
  List<UnMappedColumnAutoMapping> autoMapping = autoMappingsCache.get(mapKey);
  if (autoMapping == null) {
    autoMapping = new ArrayList<UnMappedColumnAutoMapping>();
    final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
    for (String columnName : unmappedColumnNames) {
      String propertyName = columnName;
      if (columnPrefix != null && !columnPrefix.isEmpty()) {
        // When columnPrefix is specified,
        // ignore columns without the prefix.
        if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
          propertyName = columnName.substring(columnPrefix.length());
        } else {
          continue;
        }
      }
      final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
      if (property != null && metaObject.hasSetter(property)) {
        if (resultMap.getMappedProperties().contains(property)) {
          continue;
        }
        final Class<?> propertyType = metaObject.getSetterType(property);
        if (typeHandlerRegistry.hasTypeHandler(propertyType, rsw.getJdbcType(columnName))) {
          final TypeHandler<?> typeHandler = rsw.getTypeHandler(propertyType, columnName);
          autoMapping.add(new UnMappedColumnAutoMapping(columnName, property, typeHandler, propertyType.isPrimitive()));
        } else {
          configuration.getAutoMappingUnknownColumnBehavior()
              .doAction(mappedStatement, columnName, property, propertyType);
        }
      } else {
        configuration.getAutoMappingUnknownColumnBehavior()
            .doAction(mappedStatement, columnName, (property != null) ? property : propertyName, null);
      }
    }
    autoMappingsCache.put(mapKey, autoMapping);
  }
  return autoMapping;
}

直到看到這里

final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase()); 

才知道問題所在了, configuration.isMapUnderscoreToCamelCase()的值為true,即開啟了駝峰命令。

所以查找字段時就找不到。 把之前的類似crt_time改為crtTime后,就可以了! 沒想到這么一個小錯誤,讓我糾結了這么久!還好能跟蹤源碼!

通過這次的問題排查,讓我明白了一個道理: 如果不知道某個框架原理的情況下,不要隨便填寫它的配置信息。在享受到框架的便捷性的同時,最好也得要明白它的原理,這樣當出現(xiàn)問題時,才好快速定位。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java實現(xiàn)單鏈表基礎操作

    Java實現(xiàn)單鏈表基礎操作

    大家好,本篇文章主要講的是Java實現(xiàn)單鏈表基礎操作,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Java 獲取兩個List的交集和差集,以及應用場景操作

    Java 獲取兩個List的交集和差集,以及應用場景操作

    這篇文章主要介紹了Java 獲取兩個List的交集和差集,以及應用場景操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 一文掌握spring cloud gateway(總結篇)

    一文掌握spring cloud gateway(總結篇)

    Spring Cloud Gateway是Spring Cloud的全新項目,該項目是基于Spring 5.0,Spring WebFlux和Project Reactor等技術開發(fā)的網關,它旨在為微服務架構提供一種簡單有效的統(tǒng)一的API路由管理方式,本文通過實例代碼總結介紹spring cloud gateway的相關知識,感興趣的朋友一起看看吧
    2024-12-12
  • JAVA字符串占位符使用方法實例

    JAVA字符串占位符使用方法實例

    今天同事又問起類似符串占位符使用的功能,所以下面這篇文章主要給大家介紹了關于JAVA字符串占位符使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Spring Boot集成Mybatis中如何顯示日志的實現(xiàn)

    Spring Boot集成Mybatis中如何顯示日志的實現(xiàn)

    這篇文章主要介紹了Spring Boot集成Mybatis中如何顯示日志的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Spring Eureka 未授權訪問漏洞修復問題小結

    Spring Eureka 未授權訪問漏洞修復問題小結

    項目組使用的 Spring Boot 比較老,是 1.5.4.RELEASE ,最近被檢測出 Spring Eureka 未授權訪問漏洞,這篇文章主要介紹了Spring Eureka 未授權訪問漏洞修復問題小結,需要的朋友可以參考下
    2024-04-04
  • 詳解spring boot配置單點登錄

    詳解spring boot配置單點登錄

    本篇文章主要介紹了詳解spring boot配置單點登錄,常用的安全框架有spring security和apache shiro。shiro的配置和使用相對簡單,本文使用shrio對接CAS服務。
    2017-03-03
  • SpringBoot3.x打包Docker容器的實現(xiàn)

    SpringBoot3.x打包Docker容器的實現(xiàn)

    這篇文章主要介紹了SpringBoot3.x打包Docker容器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • java Collection 之Set使用說明

    java Collection 之Set使用說明

    本篇文章小編為大家介紹,java Collection 之Set使用說明。需要的朋友參考下
    2013-04-04
  • 淺談java中為什么重寫equals后需要重寫hashCode

    淺談java中為什么重寫equals后需要重寫hashCode

    今天帶各位學習一下java中為什么重寫equals后需要重寫hashCode,文中有非常詳細的圖文介紹及代碼示例,對正在學習java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05

最新評論

南投县| 始兴县| 紫阳县| 洪泽县| 博客| 高阳县| 威信县| 广西| 榆中县| 九寨沟县| 景泰县| 巧家县| 大英县| 牡丹江市| 科技| 阿图什市| 贡觉县| 长泰县| 和龙市| 旺苍县| 昌吉市| 勃利县| 日土县| 金平| 赣州市| 玉树县| 九江市| 临沂市| 浦北县| 丰都县| 麻江县| 南郑县| 定日县| 盈江县| 长沙市| 南通市| 出国| 明星| 重庆市| 松滋市| 东乌珠穆沁旗|