【Mybatis】(九)多对一关系&一对多关系处理
1.搭建测试环境
-
数据库
create table teacher( id int(10) not null, name varchar(30) default null, primary key (id) )ENGINE = INNODB DEFAULT CHARSET=utf8; #key是普通索引 create table student( id int(10) not null, name varchar(30) default null, tid int(10) default null, primary key(id), key fktid (tid), CONSTRAINT fktid foreign KEY (tid) references teacher (id) )engine=innodb default charset=utf8; INSERT INTO teacher values (1,'笑笑'); INSERT INTO student (id,name,tid) values (1,'小明','1'); INSERT INTO student (id,name,tid) values (2,'小红','1'); INSERT INTO student (id,name,tid) values (3,'小李','1'); INSERT INTO student (id,name,tid) values (4,'小王','1'); -
导入lombok
-
新建实体类Teacher,Student
-
建立Mapper接口
-
建立Mapper.XML文件
-
在核心配置文件中绑定注册我们的Mapper接口或者文件
-
测试查询是否能成功
2.多对一处理
- 多个学生,对应一个老师
- 对于学生而已,关联:多个学生,关联一个老师【多对一】
- 对于老师而言,集合:一个老师,有很多学生【一对多】
2.1 按照查询嵌套处理
-
Student
public class Student { private int id; private String name; private Teacher teacher; } -
Teacher
public class Teacher { private int id; private String name; } -
StudentMapper.xml中
<select id="getStudent" resultMap="StudentTeacher"> select * from student; </select> <resultMap id="StudentTeacher" type="Student"> <result column="id" property="id" /> <result column="name" property="name" /> <!--复杂的属性,需要单独处理--> <!--对象:association--> <!--集合:collection--> <association column="tid" property="teacher" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where id = #{tid} </select> -
StudentMapper.java
//查询所有的学生信息,以及对应的老师的信息 public List<Student> getStudent(); -
测试结果

2.2 按照结果嵌套处理
-
StudentMapper.xml中
<select id="getStudent" resultMap="StudentTeacher2"> SELECT s.id sid,s.name sname,t.name tname from student s ,teacher t where s.tid=t.id; </select> <resultMap id="StudentTeacher2" type="Student"> <result column="sid" property="id"></result> <result column="sname" property="name"></result> <association property="teacher" javaType="Teacher"> <result column="tname" property="name"></result> </association> </resultMap>
*mysql多对一查询方式:
- 子查询
- 联表查询
3. 一对多处理
- 对于老师而言,集合:一个老师,有很多学生【一对多】
3.1 按照结果嵌套查询
-
Student
public class Student { private int id; private String name; private int tid; } -
Teacher
public class Teacher { private int id; private String name; //一个老师有多个学生 private List<Student> students; } -
TeacherMapper.java
//获取指定老师下的所有学生及老师的信息 Teacher getTeacherById(@Param("tid") int id); -
TeacherMapper.xml中
<select id="getTeacherById" resultMap="TeacherStudent"> Select s.id sid,s.name sname,t.name tname,t.id tid from student s,teacher t where s.tid=t.id and t.id=#{tid}; </select> <resultMap id="TeacherStudent" type="Teacher"> <result column="tid" property="id" /> <result column="tname" property="name" /> <!-- javaType 制定属性的类型 集合中的泛型信息,使用ofType获取 --> <collection property="students" ofType="Student"> <result column="sid" property="id" /> <result column="sname" property="name" /> <result column="tid" property="id" /> </collection> </resultMap> -
测试结果
Teacher(id=1, name=萧老师, students=[Student(id=1, name=小明, tid=0), Student(id=1, name=小红, tid=0), Student(id=1, name=小李, tid=0), Student(id=1, name=小王, tid=0)])
3.2 按照查询嵌套处理
-
TeacherMapper.xml中
<!--按照查询嵌套处理--> <select id="getTeacherById" resultMap="TeacherStudent2"> select * from mybatiss.teacher where id=#{tid} </select> <resultMap id="TeacherStudent2" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherById" column="id"/> </resultMap> <select id="getStudentByTeacherById" resultType="Student"> select * from mybatiss.student where tid=#{id}; </select>
4. 小结
1.关联 - association【多对一】
2.集合 - collection【一对多】
3.javaType & ofType
javaType:用来指定实体类中属性的类型
ofType:用来指定映射到List或集合中的pojo类型a,泛型中的约束类型
4.注意点:
1)保证SQL的可读性
2)注意一对多和多对一中,属性名和字段的问题
3)问题不好排查,可以使用日志,建议log4j
5.面试高频
1)Mysql引擎
2)InnoDB底层原理
3)索引
4)索引优化

浙公网安备 33010602011771号