Mybatis-数据库数据查出但是未返回到结果集

    <select id="selectByParams" resultType="order">
        select
        <include refid="Base_Column_List" />
        from order
        where id = #{id,jdbcType=BIGINT}
    </select>

上下两块代码块区别在于resultType和resultMap。

    <select id="selectByParams" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from order
        where id = #{id,jdbcType=BIGINT}
    </select>
    </select>

当sql查出数据sql参数字段和实体字段不匹配时就会错误,无法将结果集匹配set

具体源码:

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());
        //匹配是否有结果map 或者实体是否能获取到set方法
        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;
  }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,156评论 0 4
  • 19.6.171、ibatis.binding.BindingException: Invalid bound s...
    静心安分读书阅读 3,280评论 1 1
  • 在mybatis映射文件每编写一条sql语句,我们都必须关注sql的传参以及返回结果集映射。传入参数是为了动态的封...
    曾猪666阅读 5,946评论 0 0
  • 使用原生jdbc的问题 数据库连接, 使用时就创建,不使用就立即释放,对数据库进行频繁地链接开启和关闭,造成数据库...
    wtmxx阅读 4,010评论 1 3
  • 2018年7月15日 我看到无边的夜空布满了繁星。虽然天空没有幽深而通透的黑,繁星也缺少参差生机的闪动。但这却是我...
    丁肖东01阅读 3,226评论 0 3