myBatis一对多和多对一处理

使用场景:
数据库中的多张表,表之间通过一个相同的属性关联,一张表中的多条记录对应另一张表的一条记录,称作多对一;反之,一对多
如图:
myBatis一对多和多对一处理_第1张图片
myBatis一对多和多对一处理_第2张图片
student和teacher之间通过关联

环境搭建

myBatis一对多和多对一处理_第3张图片

//学生类
package com.LinXiaoDe.pojo;
public class Student {
    private int id;
    private String name;
    Teacher teacher;//注意这里和数据库中的tid不一致
}

//老师类
package com.LinXiaoDe.pojo;

public class Teacher {
        private int id;
        private String name;
}

多对一

查询所有学生的信息,并且获取学生老师的信息,sql可以如下查询:

> select s.id,s.name,t.name from student s,teacher t where s.tid = t.id;

myBatis一对多和多对一处理_第4张图片

> select * from student s,teacher t where s.tid = t.id;

myBatis一对多和多对一处理_第5张图片

如何用mybatis的mapper实现?

方法1:按照查询嵌套处理(sql中的子查询)

(1)接口编写

//StudentMapper 类
public interface StudentMapper {
    public List<Student> getStudents();
}

//TeacherMapper 类
public interface TeacherMapper {
    List<Teacher> getTeachers();
}

(2)编写mapper.xml

  • 实现思路:
//StudentMapper.xml中
<select id="getStudents" resultType="student">
    select * from student;
</select>

//如果只执行上面这一句,只能查找到学生的信息(id,name,null),因为最后一个teacher属性在数据库中字段是int tid,是无法直接查询到的,考虑下面这句
//下面的select可以通过id查找到teacher,因此我们的思路是通过getStudents找到tid,然后嵌套getTeacherById获取teacher
<select id="getTeacherById" resultType="teacher">
    select * from teacher where id=#{id};
</select>
  • 具体实现:
  • association关联属性
  • property属性名
  • javaType属性类型
  • column在多的一方的表中的列名
//用将两个select嵌套起来,其中:
//javaType="teacher" 指定类未teacher
//select="getTeacherById"指定嵌套的select语句

<resultMap id="studentTeacherResult1" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" column="tid" javaType="teacher" select="getTeacherById"/>
</resultMap>

<select id="getStudents" resultMap="studentTeacherResult1">
    select * from student;
</select>

<select id="getTeacherById" resultType="teacher">
    select * from teacher where id=#{id};
</select>
  • 查询结果
    myBatis一对多和多对一处理_第6张图片

方法2:按结果嵌套处理(推荐)

  • 将teacher属性用结果映射到id
  • association
  • result
<resultMap id="studentTeacherResult2" type="student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="teacher">
        <result property="name" column="tname"/>
    association>
resultMap>
<select id="getStudents2" resultMap="studentTeacherResult2">
    select s.id as sid,s.name as sname,t.name as tname
    from student s,teacher t
    where s.tid = t.id;
select>

///可以简化如下:
<resultMap id="studentTeacherResult2" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" javaType="teacher">
        <result property="name" column="name"/>
    association>
resultMap>
<select id="getStudents2" resultMap="studentTeacherResult2">
    select *
    from student s,teacher t
    where s.tid = t.id;
select>

myBatis一对多和多对一处理_第7张图片

一对多

数据库不变,但是将Teacher增加属性private List students;,Student中将Teacher 改为tid

//student
public class Student {
    private int id;
    private String name;
    int tid;
}

//teacher
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}

方法1:按结果嵌套处理(推荐)

  • 给数据起别名,在结果映射中进行映射
  • 对于集合类型,要用colloction而不是association
  • colloction对于集合类型,用ofType
<resultMap id="getTeacherResult" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="student">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
                <result property="tid" column="tid"/>
        collection>
resultMap>
<select id="getTeachers" resultMap="getTeacherResult">
        select t.id as tid, t.name tname,s.name as sname,s.id sid
        from teacher t,student s
        where t.id=s.tid and t.id=#{id};
select>

在这里插入图片描述

方法2:按照查询嵌套处理(sql中的子查询)

<resultMap id="getTeachersResult2" type="teacher">
        <collection property="students" javaType="ArrayList" ofType="student" column="id" select="getStudentsById"/>
</resultMap>
<select id="getTeachers2" resultMap="getTeachersResult2">
        select * from teacher where id=#{id}
</select>
<select id="getStudentsById" resultType="student">
        select * from student where tid=#{id};
</select>

在这里插入图片描述

总结

  • 关联-association
  • 集合-collection
  • 所以association是用于一对一和多对一,而collection是用于一对多的关系
  • JavaType和ofType都是用来指定对象类型的
  • JavaType是用来指定pojo中属性的类型
  • ofType指定的是映射到list集合属性中pojo的类型。

你可能感兴趣的:(mybatis)