Mybatis-數(shù)據(jù)庫數(shù)據(jù)查出但是未返回到結(jié)果集

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

上下兩塊代碼塊區(qū)別在于resultType和resultMap。

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

當(dāng)sql查出數(shù)據(jù)sql參數(shù)字段和實(shí)體字段不匹配時(shí)就會錯(cuò)誤,無法將結(jié)果集匹配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());
        //匹配是否有結(jié)果map 或者實(shí)體是否能獲取到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;
  }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,289評論 0 4
  • 19.6.171、ibatis.binding.BindingException: Invalid bound s...
    靜心安分讀書閱讀 511評論 1 1
  • 在mybatis映射文件每編寫一條sql語句,我們都必須關(guān)注sql的傳參以及返回結(jié)果集映射。傳入?yún)?shù)是為了動態(tài)的封...
    曾豬666閱讀 2,020評論 0 0
  • 使用原生jdbc的問題 數(shù)據(jù)庫連接, 使用時(shí)就創(chuàng)建,不使用就立即釋放,對數(shù)據(jù)庫進(jìn)行頻繁地鏈接開啟和關(guān)閉,造成數(shù)據(jù)庫...
    wtmxx閱讀 800評論 1 3
  • 2018年7月15日 我看到無邊的夜空布滿了繁星。雖然天空沒有幽深而通透的黑,繁星也缺少參差生機(jī)的閃動。但這卻是我...
    丁肖東01閱讀 543評論 0 3

友情鏈接更多精彩內(nèi)容