其实就是 实现 两个实体类之间的 互相引用 可以看一下我的 博客(1对多关系表的创建)
下面 简单的 说一下 详情参考我的博客:
两个 实体类 的 书写:
package store_entity; import java.util.Set; public class Teacher { private int id; private String name; private int age; private int group_id; private Set<Book> set;//存储 教师 所有拥有的书 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGroup_id() { return group_id; } public void setGroup_id(int group_id) { this.group_id = group_id; } public Set<Book> getSet() { return set; } public void setSet(Set<Book> set) { this.set = set; } }
package store_entity; public class Book { private int id; private String name; private String Description; private Teacher teacher;//设置外键 应用 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }
下面的 工作 就是 在 相应的 映射文件中 配置关联 外键什么的 ;
望采纳 !
select * from book where id = xxx; select * from teacher where id = (select teacher_id from book where id = xxx);
实体类就按照表字段建,然后传bookid查book数据取得teacher_id查teacher表,数据不就都出来了