多对一处理 和一对多处理的处理

在前面创建的两个表中,学生和老师的关系,学生对于老师是多对一的关系,老师对于学生是一对多的关系。在MyBatis中处理这样的相对复杂的关系需要用到对象(association)和 集合(collection)。

多对一

  • 实体类

    public class Student2 {
      private int id;
      private String name;
      //学生和老师是多对一的关系,需要在学生中关联老师
      private Teacher2 teacher;
    }
    public class Teacher2 {
    private int id;
    private String name;
    }
  • 接口

    public interface Student2Mapper {
      //获取所有学生信息以及他的老师
      List<Student2> getStudent();
      List<Student2> getStudent2();
    }
  • xml配置

    <!--按照结果嵌套处理-->
      <select id="getStudent" resultMap="StudentTeacher">
          select s.id sid,s.name sname,t.name tname,t.id tid
          from student s,teacher t
          where s.tid = t.id
      </select>
    
      <resultMap id="StudentTeacher" type="Student2">
          <result property="id" column="sid"/>
          <result property="name" column="sname"/>
          <association property="teacher" javaType="Teacher2">
              <result property="id" column="tid"/>
              <result property="name" column="tname"/>
          </association>
      </resultMap>
    
      <!--通过子查询的方法-->
      <select id="getStudent2" resultMap="StudentTeacher2">
          select * from student
      </select>
    
      <resultMap id="StudentTeacher2" type="Student2">
          <!-- 复杂的属性需要单独处理 对象:association 集合:collection -->
          <association property="teacher" column="tid" javaType="Teacher2" select="getTeacher"/>
      </resultMap>
      <select id="getTeacher" resultType="Teacher2">
          select * from teacher where id = #{tid}
      </select>
  • 一对多

这是从老师的角度,实体类如下

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

public class Teacher {
  private int id;
 private String name;
 private List<Student> students;
}
List<Teacher> getTeacher2(@Param("tid") int id);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hudu.dao.TeacherMapper">
    <select id="getTeacher" resultType="Teacher">
        select * from teacher
    </select>

    <!--按照结果嵌套-->
    <select id="getTeacher2" resultMap="TeacherStudent">
        SELECT s.id sid,s.name sname,t.name tname,t.id tid
        FROM teacher t,student s
        where t.id = s.tid and t.id = #{tid}
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--复杂的属性,我们需要单独处理 对象:association 集合:collection
        javaType=""指定属性的类型
        集合中的泛型信息,我们使用ofType获取
        -->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

    <select id="getTeacher3" resultMap="TeacherStudent3">
        select * from teacher where id = #{tid}
    </select>

    <resultMap id="TeacherStudent3" type="Teacher">
        <!--<result property="id" column="id"/>-->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>

    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid = #{tid};
    </select>
</mapper>
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!