Mybatis学习笔记7:关联查询的一对多多对一

Posted Vincent9847

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习笔记7:关联查询的一对多多对一相关的知识,希望对你有一定的参考价值。

一、一对多

理解:一个老师对多个学生

1.实体类

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

@Data 
public class Teacher {
   private int id;
   private String name;
   //一个老师多个学生
   private List<Student> students;
}

1.按结果嵌套处理

1.TeacherDao接口

//获取指定老师,及老师下的所有学生
public Teacher getTeacher(int id);

2.teacherMapper

<mapper namespace="com.dao.TeacherDao">

   <!--
   思路:
       1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
       2. 对查询出来的操作做结果集映射
           1. 集合的话,使用collection!
               JavaType和ofType都是用来指定对象类型的
               JavaType是用来指定pojo中属性的类型
               ofType指定的是映射到list集合属性中pojo的类型。
   -->
   <select id="getTeacher" resultMap="TeacherStudent">
      select s.id sid, s.name sname , t.name tname, t.id tid
      from student s,teacher t
      where s.tid = t.id and t.id=#{id}
   </select>

   <resultMap id="TeacherStudent" type="Teacher">
       <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>
</mapper>

 3.注册mapper

<mappers>
   <mapper resource="mapper/TeacherMapper.xml"/>
</mappers>

 4.测试

@Test
public void testGetTeacher(){
   SqlSession session = MybatisUtils.getSession();
   TeacherDao teacherDao = session.getMapper(TeacherDao.class);
   Teacher teacher = teacherDao.getTeacher(1);
   System.out.println(teacher.getName());
   System.out.println(teacher.getStudents());
}

2. 按查询嵌套处理

<select id="getTeacher" resultMap="TeacherStudent">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent" type="Teacher">
   <!--column是一对多的外键 , 写的是一的主键的列名-->
   <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
  select * from student where tid = #{id}
</select>

二、多对一

理解:多个学生对一个老师

1.实体类

@Data //GET,SET,ToString,有参,无参构造
public class Teacher {
   private int id;
   private String name;
}

@Data
public class Student {
   private int id;
   private String name;
   //多个学生可以是同一个老师,即多对一
   private Teacher teacher;
}

2.接口

public interface StudentMapper {
}
public interface TeacherMapper {
}

3.mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.StudentDao">

</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.TeacherDao">

</mapper>

1.按查询嵌套处理关联查询

1.给StudentDao接口增加方法

//获取所有学生及对应老师的信息
public List<Student> getStudents();

2.编写studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.StudentDao">

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

   <resultMap id="StudentTeacher" type="Student">
       <!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
       <association property="teacher"  column="tid" javaType="Teacher" select="getTeacher"/>
   </resultMap>
   <!--
   这里传递过来的id,只有一个属性的时候,下面可以写任何值
   association中column多参数配置:
       column="{key=value,key=value}"
       其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
   -->
   <select id="getTeacher" resultType="teacher">
      select * from teacher where id = #{id}
   </select>

</mapper>

3.记得在配置文件中注册studentMapper

4.测试

@Test
public void testGetStudents(){
   SqlSession session = MybatisUtils.getSession();
   StudentDao studentDao = session.getMapper(studentDao .class);

   List<Student> students = studentDao.getStudents();

   for (Student student : students){
       System.out.println("学生名:"+ student.getName()+"\\t老师:"+student.getTeacher().getName());
  }
}

2.按结果嵌套处理

<!--
按查询结果嵌套处理
思路:
   1. 直接查询出结果,进行结果集的映射
-->
<select id="getStudents" resultMap="StudentTeacher" >
  select s.id sid, s.name sname , t.name tname
  from student s,teacher t
  where s.tid = t.id
</select>

<resultMap id="StudentTeacher" type="Student">
   <id property="id" column="sid"/>
   <result property="name" column="sname"/>
   <!--关联对象property 关联对象在Student实体类中的属性-->
   <association property="teacher" javaType="Teacher">
       <result property="name" column="tname"/>
   </association>
</resultMap>

 

以上是关于Mybatis学习笔记7:关联查询的一对多多对一的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 一对一对多多对多

mybatis学习笔记(10)-一对一查询

mybatis学习笔记(11)-一对多查询

Mybatis之关联关系(一对多多对多)

MyBatis --- 映射关系一对一对多多对多,懒加载机制

mybatis 详解------一对一对多多对多