关于[Cannot determine value type from string ‘xxx‘]的一些问题

这篇具有很好参考价值的文章主要介绍了关于[Cannot determine value type from string ‘xxx‘]的一些问题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

关于[Cannot determine value type from string ‘xxx’]的问题

1、产生

实体类Student中属性有id,name,email,age,未使用无参构造器

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

测试时报错

Cannot determine value type from string '张三'

2、解决

实体类Student中添加无参构造器

得到结果

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

3、探究

跟踪源码

org.apache.ibatis.executor.resultset.DefaultResultSetHandler

这个类有个方法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);
  }

这个方法是根据结果集返回值的类型创建出相应的bean字段对象

  • 当实体使用无参构造器时

    mybatis会调用createResultObject方法中objectFactory.create(resultType),使用无参构造器创建对象

    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);
        }
      }
    
  • 当实体使用有参构造参数

    mybatis会调用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;
      }
    

​ 这个代码片段里面有个TypeHandler,这个是mybatis的类型处理器,用来处理 JavaType 与 JdbcType 之间的转换。

由代码我们看出,当实体使用有参构造函数时,会遍历有参构造参数个数,根据有参构造参数下标 查找相应的数据库字段名称,根据有参构造字段类型以及数据库字段名称找类型处理器。然后使用TypeHandler来处理JavaType 与 JdbcType 之间的转换。当转换异常,就会报错

4、总结

解决Cannot determine value type from string 'xxx’的方法有2种文章来源地址https://www.toymoban.com/news/detail-407754.html

  1. 实体加无参构造参数
  2. mapper.xml中查询的数据库字段属性的类型要和有参构造器的字段类型一一匹配;查询字段的个数要和有参构造器个数一样

到了这里,关于关于[Cannot determine value type from string ‘xxx‘]的一些问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【报错】Cannot deserialize value of type `java.time.LocalDateTime` from String

    接口测试中报错 这个错误是因为无法将字符串\\\"2023-10-10 17:23:35\\\"反序列化为java.time.LocalDateTime类型的对象。在Java中,LocalDateTime类不支持直接从字符串进行反序列化的操作。 在实体类的LocalDateTime 类型的字段上加@JsonFormat注解即可

    2024年02月03日
    浏览(22)
  • 报错:SON parse error: Cannot deserialize value of type `java.lang.String` from Array value (token `Jso

    详细报错 JSON parse error: Cannot deserialize value of type java.lang.String from Array value (token JsonToken.START_ARRAY ); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.lang.String from Array value (token JsonToken.START_ARRAY ) at [Source: (PushbackInputStream); line: 47, column

    2024年02月15日
    浏览(15)
  • 已解决JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String

    已解决JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String 下滑查看解决方法 JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String 这个问题通常出现在将一个字符串转换为LocalDateTime对象时。 下滑查看解决方法 解决这个问题的方法取决于你使用的

    2024年02月08日
    浏览(21)
  • 一步解决 JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String

    1.问题描述 JSON parse error: Cannot deserialize value of type java.time.LocalDate from String “2023-11-06 08:00:00.0”: Failed to deserialize java.time.LocalDate 2.原因分析 传递的日期格式与LocalDate类型不匹配,LocalDate只能有年月日,没有时分秒 3.解决方案 在传递日期格式时出现错误的可能性比较高,要注

    2024年02月04日
    浏览(20)
  • 解决 “JSON parse error: Cannot deserialize value of type java.util.Date from String“ 错误的方法

    在使用 Java 开发时,当处理 JSON 数据与 java.util.Date 类型相互转换时,有时会遇到错误信息 “JSON parse error: Cannot deserialize value of type java.util.Date from String”。这个错误通常发生在将 JSON 字符串转换成 Java 对象时,或将 Java 对象转换成 JSON 字符串时,由于日期格式不匹配,导致无

    2024年02月12日
    浏览(21)
  • Cannot get a STRING value from a NUMERIC cell

    Cannot get a STRING value from a NUMERIC cell 错误一: 导入Excel表格数据,该列设置数据类型为String,输入数据是int类型 解决方法:cell.setCellType(CellType.STRING); 错误二:日期类型需要定义格式 解决方法如下:   -------------------------------------以下无正文--------------------------------------------

    2024年02月16日
    浏览(28)
  • Cannot deserialize value of type `java.lang.Integer` from Object value (token `JsonToken.START_OBJEC

    错误信息:org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.lang.Integer from Object value (token JsonToken.START_OBJECT ); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.lang.Integer from Object value (token

    2024年02月12日
    浏览(17)
  • JSON parse error: Cannot deserialize value of type `java.util.ArrayList<XXX>……的解决方案

    “JSON parse error: Cannot deserialize value of type java.util.ArrayListXXX from String value (token JsonToken.VALUE_STRING ); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.ArrayListcom.ruoyi.sc.domain.ScHdImg from String value (token JsonToken.VALUE_STRING )n at [Source: (org.sp

    2024年02月11日
    浏览(23)
  • JSON parse error: Cannot deserialize value of type `java.util.ArrayList<java.lang.String>`

    在前后端传递参数时,如果为JSON,后端使用@RequestBody对象类型接受数据,会出现 500/400错误。 也就是说,在前后端发送数据时,出现JSON格式转换错误,从而访问不到后台接口。 不添加 @RequestBody 虽然可以成功访问,但是无法获取到对象数据 警告内容: 不要使用对象类型接受

    2024年02月11日
    浏览(15)
  • JSON parse error: Cannot deserialize value of type `java.util.ArrayList<java.lang.Long>` from Object

    JSON parse error: Cannot deserialize value of type `java.util.ArrayListjava.lang.Long` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayListjava.lang.Long` from Object value (token `JsonToken.START_O

    2024年02月09日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包