转载

Mybatis: Cannot determine value type from string 'city2001'

当查询的实体类没有无参构造方法时,查询报错:

Cannot determine value type from string 'city2001'; nested exception is java.sql.SQLDataException: Cannot determine value type from string 'city2001'] with root cause

猜测

​ 依稀记得我对实体类创建了全参构造方法,但是忘记加上无参构造方法,而 Mybatis 的映射机制应该是使用 set 方法进行属性映射,当无参构造方法没了,new 不出对象,也就自然没法自动映射了。

实体类 Student.java

public class Student implements Serializable {
    private static final long serialVersionUID = -49141689091044211L;
    private Long id;
    private String name;
    private String city;
    private Integer age;

    public Student(String name, String city, Integer age) {
        this.name = name;
        this.city = city;
        this.age = age;
    }
    
    ...... 省略 Get Set

初步验证

​ 简单的 Debug 了一下源码,结合网上的资料, org.apache.ibatis.executor.resultset.DefaultResultSetHandle 类中发现方法 createAutomaticMappings ,该方法处理了前缀,并判断有没有对应的 Set 方法,然后进行注入。

......
......
final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
        if (property != null && metaObject.hasSetter(property)) {
          if (resultMap.getMappedProperties().contains(property)) {
            continue;
          }
......
......

入门小白,才学疏浅,若有疏漏,欢迎指出。

本文首次更新时间 2020-04-28

本文最后更新时间 2020-04-28 (若有突破,持续更新)

基于Halo 搭建的个人博客:https://undivided.top

原文  https://segmentfault.com/a/1190000022496201
正文到此结束
Loading...