JavaEE_09_Mybatis级联映射

《Mybatis级联映射》

目录

  • 对象关系(掌握)
  • 对多操作(熟练)
  • 对一操作(熟练)

一、对象关系

表与表之间主要有四种关系:一对一多对一一对多多对多

Mybatis提供了两个标签实现这种关系:association(对一),collection(对多)。

请添加图片描述

二、对多操作

  1. 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;
    }
}
  1. com.hpr.mapper.TeacherMapper
...
public interface TeacherMapper {
    ...
    List<Teacher> testToMany();
}
  1. 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>
  1. com.hpr.service.ITeacherService
...
public interface ITeacherService {
    ...
    Response<List<Teacher>> testToMany();

}
  1. com.hpr.service.impl.TeacherService
public class TeacherService implements ITeacherService {

    @Override
    public Response<List<Teacher>> testToMany() {
        return new Response<>(200, "success", teacherMapper.testToMany());
    }
}
  1. com.hpr.controller.MybatisController
...
public class MybatisController {
    ...
    @GetMapping("/testToMany")
    @ApiOperation("测试对多")
    public Response<List<Teacher>> testToMany() {
        return iTeacherService.testToMany();
    }
}
  1. 启动测试

请添加图片描述

执行结果

请添加图片描述

三、对一操作

  1. com.hpr.entity.Student
...
public class Student {
    ...
        
    //一个学生对应一个老师
    private Teacher teacher;

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}
  1. com.hpr.mapper.StudentMapper
...
@Repository
public interface StudentMapper {
    ...
    List<Student> testToOne();
}
  1. 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>
  1. com.hpr.service.IStudentService
...
public interface IStudentService {
    ...
    Response<List<Student>> testToOne();
}
  1. 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());
    }
}
  1. com.hpr.controller.MybatisController
...
public class MybatisController {
    ...
    @GetMapping("/testToOne")
    @ApiOperation("测试对一")
    public Response<List<Student>> testToOne() {
        return iStudentService.testToOne();
    }
}
  1. 启动测试

请添加图片描述

执行结果

请添加图片描述

总结

重点

  1. 数据库表关系判断;
  2. “对多”、“对一”实现手段。

难点

  1. “对多”、“对一”实现手段。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值