《Mybatis级联映射》
目录
一、对象关系
Mybatis提供了两个标签实现这种关系:association(对一),collection(对多)。
二、对多操作
- com.hpr.entity.Teacher
...
public class Teacher {
...
//一个老师对应多个学生
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
- com.hpr.mapper.TeacherMapper
...
public interface TeacherMapper {
...
List<Teacher> testToMany();
}
- mappers/TeacherMappper.xml
<!-- 外键结果映射 -->
<resultMap id="FkResultMap" type="com.hpr.entity.Teacher">
<id column="teacher_id" property="teacherId"/>
<result column="teacher_name" property="teacherName"/>
<result column="age" property="age"/>
<result column="phone_number" property="phoneNumber"/>
<result column="info" property="info"/>
<!-- 对多 -->
<collection property="students" resultMap="com.hpr.mapper.StudentMapper.BaseResultMap"/>
</resultMap>
...
<!-- 测试一对多 -->
<select id="testToMany" resultMap="FkResultMap">
select *
from teacher t,
student s
where t.teacher_id = s.teacher_id
</select>
- com.hpr.service.ITeacherService
...
public interface ITeacherService {
...
Response<List<Teacher>> testToMany();
}
- com.hpr.service.impl.TeacherService
public class TeacherService implements ITeacherService {
@Override
public Response<List<Teacher>> testToMany() {
return new Response<>(200, "success", teacherMapper.testToMany());
}
}
- com.hpr.controller.MybatisController
...
public class MybatisController {
...
@GetMapping("/testToMany")
@ApiOperation("测试对多")
public Response<List<Teacher>> testToMany() {
return iTeacherService.testToMany();
}
}
- 启动测试
执行结果
三、对一操作
- com.hpr.entity.Student
...
public class Student {
...
//一个学生对应一个老师
private Teacher teacher;
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
- com.hpr.mapper.StudentMapper
...
@Repository
public interface StudentMapper {
...
List<Student> testToOne();
}
- mappers/StudentMappper.xml
<!-- 外键结果映射 -->
<resultMap id="FkResultMap" type="com.hpr.entity.Student">
<id column="student_id" property="studentId"/>
<result column="student_name" property="studentName"/>
<result column="chinese" property="chinese"/>
<result column="math" property="math"/>
<result column="english" property="english"/>
<result column="teacher_id" property="teacherId"/>
<!-- 对一 -->
<association property="teacher" resultMap="com.hpr.mapper.TeacherMapper.BaseResultMap"/>
</resultMap>
...
<select id="testToOne" resultMap="FkResultMap">
select *
from student s,
teacher t
where s.teacher_id = t.teacher_id
</select>
- com.hpr.service.IStudentService
...
public interface IStudentService {
...
Response<List<Student>> testToOne();
}
- com.hpr.service.impl.StudentService
...
@Service
public class StudentService implements IStudentService {
@Autowired
private StudentMapper studentMapper;
...
@Override
public Response<List<Student>> testToOne() {
return new Response<>(200, "success", studentMapper.testToOne());
}
}
- com.hpr.controller.MybatisController
...
public class MybatisController {
...
@GetMapping("/testToOne")
@ApiOperation("测试对一")
public Response<List<Student>> testToOne() {
return iStudentService.testToOne();
}
}
- 启动测试
执行结果
总结
重点
- 数据库表关系判断;
- “对多”、“对一”实现手段。
难点
- “对多”、“对一”实现手段。