gpt4 book ai didi

java - Spring MyBatis关系映射问题

转载 作者:行者123 更新时间:2023-12-02 09:46:19 27 4
gpt4 key购买 nike

我使用 MyBatis 和 h2 数据库来进行学习。当我想在查询中将子对象插入到父对象中时遇到问题,然后出现异常。

学生类

public class Student {
private Long id;
private String name;
private Index index;

public Student(Long id, String name, Index index) {
this.id = id;
this.name = name;
this.index = index;
}
// getters and setters..
}

索引类别

public class Index {
private Long id;
private String number;

public Index() { }

public Index(Long id, String number) {
this.id = id;
this.number = number;
}
// getters and setters..
}

学生资料库

@Mapper
@Repository
public interface StudentRepo {

@Select("SELECT * FROM student WHERE id=#{id}")
Student findById(long id);
// exception occurs for index field, which is my child object
@Insert("INSERT INTO student VALUES(#{id}, #{name}, #{index})")
int insert(Student student);
}

索引存储库

@Mapper
@Repository
public interface IndexRepo {

@Select("SELECT * FROM index WHERE id =#{id}")
Index findById(long id);

@Insert("INSERT INTO index VALUES(#{id}, #{number})")
int insert(Index index);
}

异常

Caused by: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'index'. It was either not specified and/or could not be found for the javaType (com.example.batis.domain.Index) : jdbcType (null) combination.
``

最佳答案

发生错误是因为你没有指示mybatis如何将Index类型的对象转换为存储在student表中的值(索引 我假设)。

您需要指定如何从可用的对象中获取要存储的值,如下所示:

@Insert("INSERT INTO student VALUES(#{id}, #{name}, #{index.id})")
int insert(Student student);

关于java - Spring MyBatis关系映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56620784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com