学习mybatis的关联关系,主要注解在代码上,这里不做解释。配置文件一样的就不贴了
1.关联关系表创建(学生对应老师 多对一)
学生老师表
2.表对应的实体类
package com.home.entity;/** * 此类是:学生 * @author hpc * @2017年1月14日下午7:06:33 */public class Student { private Integer student_id;//学生id private String student_name;//学生名字 private Integer student_number;//学生编号 private Teacher student_teacher;//是哪个教师的学生 public Integer getStudent_id() { return student_id; } public void setStudent_id(Integer student_id) { this.student_id = student_id; } public String getStudent_name() { return student_name; } public void setStudent_name(String student_name) { this.student_name = student_name; } public Integer getStudent_number() { return student_number; } public void setStudent_number(Integer student_number) { this.student_number = student_number; } public Teacher getStudent_teacher() { return student_teacher; } public void setStudent_teacher(Teacher student_teacher) { this.student_teacher = student_teacher; } @Override public String toString() { return "Student [student_id=" + student_id + ", student_name=" + student_name + ", student_number=" + student_number + ", student_teacher=" + student_teacher + "]"; }}
package com.home.entity;import java.util.HashSet;import java.util.Set;/** * 此类是:老师 * @author hpc * @2017年1月14日下午7:06:47 */public class Teacher { private Integer teacher_id;// 老师id private String teacher_name;// 老师名字 private Integer teacher_number;// 老师编号 private Setteacher_student = new HashSet ();// 老师教过哪些学生 public Integer getTeacher_id() { return teacher_id; } public void setTeacher_id(Integer teacher_id) { this.teacher_id = teacher_id; } public String getTeacher_name() { return teacher_name; } public void setTeacher_name(String teacher_name) { this.teacher_name = teacher_name; } public Integer getTeacher_number() { return teacher_number; } public void setTeacher_number(Integer teacher_number) { this.teacher_number = teacher_number; } public Set getTeacher_student() { return teacher_student; } public void setTeacher_student(Set teacher_student) { this.teacher_student = teacher_student; } @Override public String toString() { return "Teacher [teacher_id=" + teacher_id + ", teacher_name=" + teacher_name + ", teacher_number=" + teacher_number + ", teacher_student=" + teacher_student + "]"; } }
3.核心主要是配置文件
4. 测试
package com.home.mybatis;import java.io.IOException;import org.apache.ibatis.session.SqlSession;import com.home.entity.Student;import com.home.entity.Teacher;public class TestApp { public static void main(String[] args) throws IOException { // 我将获取session的代码封装成了一个工具类,直接用工具类来获取session SqlSession session = SessionUtils.getSession("mybatis.xml"); String loadTeacher="com.home.entity.teacherMapper.loadTeacher"; String loadStudent="com.home.entity.studentMapper.loadStudent"; Teacher teacher = session.selectOne(loadTeacher,1); Student student = session.selectOne(loadStudent,1); System.out.println(student); System.out.println(teacher); // 将数据属性到数据中去 session.commit(); // 关闭session session.close(); } }
5.结果
Student [student_id=4, student_name=hpc, student_number=1, student_teacher=Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[]]]Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[Student [student_id=5, student_name=zll, student_number=2, student_teacher=null], Student [student_id=4, student_name=hpc, student_number=1, student_teacher=null]]]