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

MyBatis測(cè)試報(bào)錯(cuò):Cannot?determine?value?type?from?string?'xxx'的解決辦法

 更新時(shí)間:2023年02月17日 09:16:35   作者:大先生哈哈哈  
這篇文章主要給大家介紹了關(guān)于MyBatis測(cè)試報(bào)錯(cuò):Cannot?determine?value?type?from?string?'xxx'的解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

關(guān)于[Cannot determine value type from string ‘xxx’]的問(wèn)題

1、產(chǎn)生

實(shí)體類Student中屬性有id,name,email,age,未使用無(wú)參構(gòu)造器

在mapper.xml中只查詢name,email,age

測(cè)試時(shí)報(bào)錯(cuò)

Cannot determine value type from string '張三'

2、解決

實(shí)體類Student中添加無(wú)參構(gòu)造器

得到結(jié)果

Student{id=null, name='張三', email='zhangsan@126.com', age=22}

3、探究

跟蹤源碼

org.apache.ibatis.executor.resultset.DefaultResultSetHandler

這個(gè)類有個(gè)方法createResultObject

private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix)
      throws SQLException {
    final Class<?> resultType = resultMap.getType();
    final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory);
    final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings();
    if (hasTypeHandlerForResultObject(rsw, resultType)) {
      return createPrimitiveResultObject(rsw, resultMap, columnPrefix);
    } else if (!constructorMappings.isEmpty()) {
      return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix);
    } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
      return objectFactory.create(resultType);
    } else if (shouldApplyAutomaticMappings(resultMap, false)) {
      return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs);
    }
    throw new ExecutorException("Do not know how to create an instance of " + resultType);
  }

這個(gè)方法是根據(jù)結(jié)果集返回值的類型創(chuàng)建出相應(yīng)的bean字段對(duì)象

  • 當(dāng)實(shí)體使用無(wú)參構(gòu)造器時(shí)

mybatis會(huì)調(diào)用createResultObject方法中objectFactory.create(resultType),使用無(wú)參構(gòu)造器創(chuàng)建對(duì)象

private  <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    try {
      Constructor<T> constructor;
      if (constructorArgTypes == null || constructorArgs == null) {
        constructor = type.getDeclaredConstructor();
        try {
          return constructor.newInstance();
        } catch (IllegalAccessException e) {
          if (Reflector.canControlMemberAccessible()) {
            constructor.setAccessible(true);
            return constructor.newInstance();
          } else {
            throw e;
          }
        }
      }
      constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
      try {
        return constructor.newInstance(constructorArgs.toArray(new Object[0]));
      } catch (IllegalAccessException e) {
        if (Reflector.canControlMemberAccessible()) {
          constructor.setAccessible(true);
          return constructor.newInstance(constructorArgs.toArray(new Object[0]));
        } else {
          throw e;
        }
      }
    } catch (Exception e) {
      String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList)
          .stream().map(Class::getSimpleName).collect(Collectors.joining(","));
      String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList)
          .stream().map(String::valueOf).collect(Collectors.joining(","));
      throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
    }
  }

當(dāng)實(shí)體使用有參構(gòu)造參數(shù)

mybatis會(huì)調(diào)用createResultObject方法中createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs);

private Object createUsingConstructor(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, Constructor<?> constructor) throws SQLException {
    boolean foundValues = false;
    for (int i = 0; i < constructor.getParameterTypes().length; i++) {
      Class<?> parameterType = constructor.getParameterTypes()[i];
      String columnName = rsw.getColumnNames().get(i);
      TypeHandler<?> typeHandler = rsw.getTypeHandler(parameterType, columnName);
      Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
      constructorArgTypes.add(parameterType);
      constructorArgs.add(value);
      foundValues = value != null || foundValues;
    }
    return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
  }

? 這個(gè)代碼片段里面有個(gè)TypeHandler,這個(gè)是mybatis的類型處理器,用來(lái)處理 JavaType 與 JdbcType 之間的轉(zhuǎn)換。

由代碼我們看出,當(dāng)實(shí)體使用有參構(gòu)造函數(shù)時(shí),會(huì)遍歷有參構(gòu)造參數(shù)個(gè)數(shù),根據(jù)有參構(gòu)造參數(shù)下標(biāo) 查找相應(yīng)的數(shù)據(jù)庫(kù)字段名稱,根據(jù)有參構(gòu)造字段類型以及數(shù)據(jù)庫(kù)字段名稱找類型處理器。然后使用TypeHandler來(lái)處理JavaType 與 JdbcType 之間的轉(zhuǎn)換。當(dāng)轉(zhuǎn)換異常,就會(huì)報(bào)錯(cuò)。

4、總結(jié)

解決Cannot determine value type from string 'xxx’的方法有2種

  • 實(shí)體加無(wú)參構(gòu)造參數(shù)
  • mapper.xml中查詢的數(shù)據(jù)庫(kù)字段屬性的類型要和有參構(gòu)造器的字段類型一一匹配;查詢字段的個(gè)數(shù)要和有參構(gòu)造器個(gè)數(shù)一樣

到此這篇關(guān)于MyBatis測(cè)試報(bào)錯(cuò):Cannot determine value type from string 'xxx'解決的文章就介紹到這了,更多相關(guān)Cannot determine value type from string內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java獲取Process子進(jìn)程進(jìn)程ID方法詳解

    Java獲取Process子進(jìn)程進(jìn)程ID方法詳解

    這篇文章主要介紹了Java獲取Process子進(jìn)程進(jìn)程ID方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-12-12
  • springboot對(duì)數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn)

    springboot對(duì)數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn)

    這篇文章主要介紹了springboot對(duì)數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java對(duì)稱加密工作模式原理詳解

    Java對(duì)稱加密工作模式原理詳解

    這篇文章主要介紹了Java對(duì)稱加密工作模式原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • uploadify java實(shí)現(xiàn)多文件上傳和預(yù)覽

    uploadify java實(shí)現(xiàn)多文件上傳和預(yù)覽

    這篇文章主要為大家詳細(xì)介紹了java結(jié)合uploadify實(shí)現(xiàn)多文件上傳和預(yù)覽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • SpringBoot集成SSM、Dubbo、Redis、JSP的案例小結(jié)及思路講解

    SpringBoot集成SSM、Dubbo、Redis、JSP的案例小結(jié)及思路講解

    這個(gè)案例其實(shí)就是SpringBoot集成SSM、Dubbo、Redis、JSP,看起來(lái)感覺(jué)很繁瑣,其實(shí)就是很簡(jiǎn)單,下面通過(guò)案例分析給大家講解,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 淺談SpringMVC的執(zhí)行流程

    淺談SpringMVC的執(zhí)行流程

    下面小編就為大家?guī)?lái)一篇淺談SpringMVC的執(zhí)行流程。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 關(guān)于ReentrantLock的實(shí)現(xiàn)原理解讀

    關(guān)于ReentrantLock的實(shí)現(xiàn)原理解讀

    這篇文章主要介紹了關(guān)于ReentrantLock的實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Spring Boot配置元數(shù)據(jù)方法教程

    Spring Boot配置元數(shù)據(jù)方法教程

    這篇文章主要介紹了Spring Boot配置元數(shù)據(jù)方法教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • java獲取注冊(cè)ip實(shí)例

    java獲取注冊(cè)ip實(shí)例

    本文分享了java獲取注冊(cè)ip實(shí)例代碼,代碼簡(jiǎn)潔,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧
    2016-12-12
  • java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能

    java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論

临沂市| 绍兴县| 白朗县| 灵川县| 恩施市| 子长县| 星子县| 伊吾县| 甘孜县| 集安市| 兰西县| 泰安市| 休宁县| 孝感市| 阆中市| 大埔县| 防城港市| 拜泉县| 清流县| 宜丰县| 洞头县| 名山县| 襄樊市| 涟源市| 常熟市| 时尚| 科技| 宜兰市| 正定县| 神农架林区| 开平市| 临安市| 清水县| 武冈市| 宽甸| 邵阳县| 兴隆县| 墨竹工卡县| 神木县| 长葛市| 江口县|