Mybatis出错 缺无参构造函数 映射出错

Mybatis出错:Error instantiating class com.User with invalid types () or values ()
出错位置:联表映射时,使用one=@One(***)无响应

实体类

省略属性...

public Student() {
    }
public Student(String studentNumber, String sname, String smajor, String sclass, Teacher teacherInfo, Topic topic) {
        this.studentNumber = studentNumber;
        this.sname = sname;
        this.smajor = smajor;
        this.sclass = sclass;
        this.teacherInfo = teacherInfo;
        this.topic = topic;
    }
    
省略get和set方法...

mapper(使用mybatis注解的方式进行映射)

@Select("select * from student_list")
    @Results(
            id = "studentMap",
            value= {
                    @Result(column = "student_number",property = "studentNumber"),
                    @Result(column = "Sname",property = "sname"),
                    @Result(column = "Smajor",property = "smajor"),
                    @Result(column = "Sclass",property = "sclass"),
                    @Result(column = "teacher_number",property = "teacherInfo",one = @One(select = "com.example.system.dao.mapper.ITeacherMapper.getTeacherOne")),
                    @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.system.dao.mapper.ITopicMapper.selectTopicOne"))
            }
    )
public List<Student> selectStudentList();

原来是由于我的Bean没有无参构造器,加入无参构造函数后,错误消失。
也就是说mybatis在创建bean映射的时候需要无参的构造其来构造对象,然后才进行赋值操作。
因此,Bean必须要有无参构造函数才能正确映射。

你可能感兴趣的:(Java,java,java-ee,开发语言,mybatis,mysql)