mybatis-plus timestamp返回为null问题排除

问题是这样的:

在开发时,为了节约时间,我选择了mybatis框架来开发,然后又在网上找了一个许多人都推荐的mybatis-plus来作为持久层框架。

于是乎我按照官方的DEMO下了一个springBoot的mybatis-plus版本的DEMO,地址为:https://gitee.com/baomidou/mybatisplus-spring-boot 

这个DEMO是基于H2数据库的,跑了下没有问题。DEMO是能正常运行的。

然后我将这个工程的代码快速拷贝的新的一个工程里,并把数据库由H2换为了MYSQL。但项目跑起来时,出现了如下问题:

数据库里的数据如下图:


表结构如下图:


查询出来的结果中对于timestamp类型的字段为空


为了解决这个问题,我决定通过debug断点观察下是否查询是把数据数据查出来了

由于用的mybatis-plus框架来开发,而mybaits-plus框架是基于mybatis框架的,为了看是否把数据查询出来,直接在ResultSetHandler上把一个dubug就好。在默认情况下,mybatis框架使用的ResultSetHandler为DefaultResultSetHandler,当查询mybatis查询完毕后,会通过ResultSetHandler的handleResultSets(Statement stmt)方法对查询的数据结果集进行封装

所以将断点打在handlerResultSets方法上最为合适。

再通过statement -> wrapper ->results -> rowData ->rows观察发现如下数据:


查询返回的结果集中rows的记录数为1,第1个字段的ascii为49,而49是ascii中数字1的值。而第二个字段也有值,说明所对应的timestamp字段是有返回值的,数据库查询是没有问题的。

然而通过mybatis代码进一步封装后的数据multipleResults又表示,只查询到了类型为Int的字段的数据

这么也就是说,问题应该是出在了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);这句代码上了

通过代码跟踪,发现如下代码:

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

继而发现如下的核心代码:

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;
}
通过断点发现以下数据


在获取这个查询的的返回字段时,只获取出来两个,即int类型和varchar类型的.

再通过跟踪发现了如下代码:

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后,就可以了!
没想到这么一个小错误,让我纠结了这么久!还好能跟踪源码!
通过这次的问题排查,让我明白了一个道理: 如果不知道某个框架原理的情况下,不要随便填写它的配置信息。在享受到框架的便捷性的同时,最好也得要明白它的原理,这样当出现问题时,才好快速定位。

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
mybatis-plus在使用jdk8的LocalDateTime查询时,需要添加一个配置项。在mybatis-plus的配置文件中添加以下配置: ```yaml mybatis-plus: configuration: # 查询返回null值时,不会映射到实体类上 map-underscore-to-camel-case: true # 配置 LocalDateTime 和 Date 的序列化和反序列化方式 # 使用 jdk8 的时间类型,需要配置 # 下面两个配置可以根据自己的需要进行配置 # LocalDateTime 序列化方式 # 可选值:java.util.Date、java.sql.Date # 默认值:java.util.Date # type-handle: java.sql.Date type-handlers-package: com.baomidou.mybatisplus.extension.handlers ``` 在以上配置中,需要注意的是 `type-handlers-package` 配置项。它指定了mybatis-plus的类型处理器包路径,其中包含有针对jdk8的LocalDateTime类型的处理器。 另外,如果在实体类中使用了jdk8的LocalDateTime类型,需要在对应的mapper.xml文件中,将数据库中的datetime类型字段映射为LocalDateTime类型,例如: ```xml <resultMap id="BaseResultMap" type="com.example.entity.User"> <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/> <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/> </resultMap> <select id="getUserById" resultMap="BaseResultMap"> select id, name, age, gmt_create, gmt_modified from user where id = #{id} </select> ``` 这样就可以在mybatis-plus中使用jdk8的LocalDateTime类型进行查询了。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水中加点糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值