【Mybatis series】Many-to-one, one-to-many query

What is many-to-one and what is one-to-many should already be very clear. Let's explain how to use mybatis through an example of demand.

Many-to-one processing:

Requirement: multiple students correspond to one teacher. For students, it is a many-to-one phenomenon, that is, one teacher is associated with students!

  • 1. Create two entities:

Teacher entity:

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

Student entity:

@Data
public class Student {
   private int id;
   private String name;
   //多个学生可以是同一个老师,即多对一
   private Teacher teacher;
}
  •  2. Write the Mapper interface corresponding to the entity class:
public interface StudentMapper {
    public List<Student> getStudents();

}
public interface TeacherMapper {
}
  • 3. Write the corresponding Mapper file:

Nested processing according to the query:

?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.kuang.mapper.StudentMapper">
 
   <!--
   需求:获取所有学生及对应老师的信息
   思路:
       1. 获取所有学生的信息
       2. 根据获取的学生信息的老师ID->获取该老师的信息
       3. 思考问题,这样学生的结果集中应该包含老师,该如何处理呢,数据库中我们一般使用关联查询?
           1. 做一个结果集映射:StudentTeacher
           2. StudentTeacher结果集的类型为 Student
           3. 学生中老师的属性为teacher,对应数据库中为tid。
              多个 [1,...)学生关联一个老师=> 一对一,一对多
           4. 查看官网找到:association – 一个复杂类型的关联;使用它来处理关联查询
   -->
   <select id="getStudents" resultMap="StudentTeacher">
    select * from student
   </select>
   <resultMap id="StudentTeacher" type="Student">
       <!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
        <result property="id" column="id"/>
        <result property="name" column="name"/>
       <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>

In addition to the above ideas, there is another idea:

Nested processing according to the results:

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

summary

Nested processing according to the query is like a subquery in SQL

Nested processing according to the results is like a query in SQL

One-to-many processing:

One-to-many understanding:

· One teacher has multiple students

· For the teacher, it is a one-to-many phenomenon, that is, a group of students (collection) from one teacher!

  • 1. Entity class writing:
@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;
}
  • 2.TeacherMapper interface writing:
public Teacher getTeacher(int id);
  • 3. Write the Mapper configuration file corresponding to the interface

Nested processing by result: 

 

<mapper namespace="com.kuang.mapper.TeacherMapper">
 
   <!--
   思路:
       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="id" cloumn="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>
</mapper>

Processing by query nesting:

<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" 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>

to sum up:  

1. Association-association 

2. Collection

3. So association is used for one-to-one and many-to-one, and collection is used for one-to-many relationships

4. JavaType and ofType are both used to specify the object type, JavaType is used to specify the class of the attribute in the pojo, and ofType specifies the type of pojo mapped to the list collection attribute

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/108229071