Version: Next

1. 一对多

一个老师拥有多个学生

对于老师而言就是一对多的关系

1.1 环境搭建

新建一个Mybatis07,和之前一样

  • 修改POJO

    • Student

      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      public class Student {
      private int id;
      private String name;
      private int tid;
      }
    • Teacher

      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class Teacher {
      private int id;
      private String name;
      private List<Student> students;
      }
  • 接口

    public interface TeacherMapper {
    //获取老师
    List<Teacher> getTeacher();
    }
  • Mapper.xml

    <mapper namespace="com.bsx.dao.TeacherMapper">
    <select id="getTeacher" resultType="com.bsx.pojo.Teacher">
    select * from teacher
    </select>
    </mapper>
  • 测试

    @Test
    public void testEnv() throws IOException {
    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sessionFactory.openSession();
    try {
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
    List<Teacher> teachers = mapper.getTeacher();
    for (Teacher teacher : teachers) {
    System.out.println(teacher);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    sqlSession.close();
    }
    }

2. 一对多需求

需求:获取指定老师及其所有学生

  • 接口

    public interface TeacherMapper {
    //获取老师
    // List<Teacher> getTeacher();
    //获取指定老师及其所有学生**
    Teacher getTeacher(@Param("tid") int id);
    }
  • mapper.xml

    • javaType:指定属性的类型,集合中的泛型信息我们使用ofType获取 students是一个List里面装着Student,所以在mapper.xml中指定students内的Student要用ofType而不是javaType
    <mapper namespace="com.bsx.dao.TeacherMapper">
    <!-- 按结果嵌套查询-->
    <select id="getTeacher" resultMap="TeacherStudent">
    SELECT
    t.id tid, t.name tname, s.id sid, s.name sname
    FROM
    student s, teacher t
    WHERE
    s.tid = t.id
    AND
    t.id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="com.bsx.pojo.Teacher">
    <result column="tid" property="id"/>
    <result column="tname" property="name"/>
    <collection property="students" ofType="com.bsx.pojo.Student" >
    <result column="sid" property="id"/>
    <result column="sname" property="name"/>
    <result column="tid" property="tid"/>
    </collection>
    </resultMap>
    </mapper>
  • 测试

    @Test
    public void testGetTeacher() throws IOException {
    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sessionFactory.openSession();
    try {
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher(1);
    System.out.println(teacher);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    sqlSession.close();
    }
    }
  • 结果

    Teacher(id=1, name=秦老师,
    students=[
    Student(id=1, name=小明, tid=1),
    Student(id=2, name=小红, tid=1),
    Student(id=3, name=小张, tid=1),
    Student(id=4, name=小李, tid=1),
    Student(id=5, name=小王, tid=1)])

3. 小结

  • 关联 - association [多对一]
  • 集合 - collection [一对多]
  • javaType & ofType
    • javaType用来指定实体类中属性的类型
    • ofTyoe用来指定映射到List或者集合中的POJO类型,即泛型类型

注意点:

  1. 保证sql语句的可读性
  2. 注意一对多和多对一中,属性名和字段的问题
  3. 如果问题不好排查,可以使用日志,建议使用LOG4J
  • Mysql引擎
  • InnoDB底层原理
  • 索引
  • 索引优化