MyBatis~associated nested Select query and associated nested result mapping (use of association elements), nested Select query of collection and nested result mapping of collection (use of collection)

Solve the one-to-many data problem

  • In a one-to-many data table structure, for example, a teacher educates many students. For students, this is a one-to-many problem
  • The main solution to this is that if one of the attributes of our POJO object is a reference type, not a basic type or String, then a simple SQL statement must not be able to solve it.
  • Student type
public class Student {
    
    

    private int id;
    private String name;
    private Teacher teacher;
}
  • Teacher type
public class Teacher {
    
    

    private int id;
    private String name;
}
  • For example, we now have a requirement, how to find all student objects in the database?

Associated nested Select query

    <select id="selectStudent" resultMap="studentTeacher">
        select * from student;
    </select>

    <resultMap id="studentTeacher" type="student">
        <association property="teacher" javaType="teacher" column="tid" select="selectOneTeacher"/>
    </resultMap>

    <select id="selectOneTeacher" resultType="teacher">
        select * from teacher where id=#{
    
    tid};
    </select>
  • Take you to interpret the above code. First, the id corresponds to the method of our dao layer interface. The return value type is initially set to resultmap. The first thing we find is all the information in the database student table.
  • Then go to the resultMap tag, the id here should correspond to the name of the resultMap we set above. This return type is the real return type, so it is student, where the id and name of the student have been automatically mapped, and we don’t need to do it again. To establish the mapping relationship, the most important thing is the mapping of the teacher object. We use the association tag, and the property is mapped to the teacher reference name of the student type. Javatype indicates that the return is a teacher type, and the column indicates that the tid in the student information found is going to go. This teacher object establishes a connection, select means to call the following select statement
  • In the bottom select statement, the return type is the teacher type and corresponds to the above. In the SQL statement, the value of the column set above is used to establish a connection.
  • The last is to return the student object
    Insert picture description here

Associated nested result mapping

    <select id="selectStudent2" 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">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>
        <association property="teacher" javaType="teacher">
            <result column="tname" property="name"/>
        </association>
    </resultMap>
  • Using this code is easy to understand, but the sql statement will become complicated
  • At the beginning, we used a very long SQL statement to establish a connection between the teacher and the student to query all the information
  • So in the resultmap, we only need to map the result of this information, but we should pay attention to the result map of the reference type must be used in the association element, and the type of return must be specified.
  • Note that I did not set the mapping relationship for this attribute of the teacher id, so the default value of this data type must be displayed.
    Insert picture description here

Solve one-to-many problems

  • This is the first example. A teacher educates many students. For the teacher, this is a one-to-many relationship. But here I want to complicate it. A class has both teachers and students, that is, there are both reference types. There are collection types, how to query this
  • OK, I have a requirement now. I specify the id of the teacher to get all the information of this classroom, including the information of the teacher and the information of all students
  • Classroom type
public class ClassRoom {
    
    

    private Teacher teacher;
    private List<Student> students;
}

Nested Select query of collection

    <select id="getClassRoom" parameterType="_int" resultMap="classRoomMap">
        select * from teacher where id=#{
    
    tid};
    </select>

    <resultMap id="classRoomMap" type="classRoom">
        <association property="teacher" javaType="teacher">
            <result column="id" property="id"/>
            <result column="name" property="name"/>
        </association>
        <collection property="students" javaType="ArrayList" ofType="student" column="id" select="students"/>
    </resultMap>

    <select id="students" resultType="student">
        select id, name from student where tid=#{
    
    id};
    </select>
  • First of all, the id object method name is getClassRoom, which is searched by the teacher's id, so there will be a parameter parameterType of int type, and the initial return type is resultmap. In the preliminary SQL statement, I find all of the teacher's id through the teacher information
  • In the corresponding name classRoomMap of resultMap, I specify that the return type is of course a classroom type, and the next step is to use the association element to attribute the reference to the teacher to establish a mapping relationship
  • Next, use the element of collection to construct the collection of students. First, use the element of collection explicitly. You first need to establish a mapping exercise with the attribute name of the classroom, and then you must specify javatype and oftype. What type of collection is javatype, oftype? It is to specify the type of the generic parameter in this collection type, this is very important, and then the column specifies what data to establish a connection with the subsequent select
  • In the final selection, we use the teacher id passed in the above column to establish contact with all student substitutions, find the corresponding required information, return the student type, and then return the query result of the classRoom type.

Insert picture description here

Nested result mapping for collections

  • One thing needs to be changed here, that is, if you want to meet our needs, using the above classroom type, if you use the nested result mapping of the collection, it is impossible to achieve at the SQL level, so you have to change the data type of the classroom.
  • New classroom type
public class ClassRoom {
    
    

    private int tid;
    private String tname;
    private List<Student> students;
}
  • ok
    <select id="getClassRoom2" parameterType="_int" resultMap="classRoomMap2">
        select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid=t.id and t.id=#{
    
    tid};
    </select>

    <resultMap id="classRoomMap2" type="classRoom">
            <result column="tid" property="tid"/>
            <result column="tname" property="tname"/>
        <collection property="students" javaType="ArrayList" ofType="student">
            <result column="sid" property="id"/>
            <result column="sname" property="name"/>
        </collection>
    </resultMap>
  • As long as the result set mapping is used, all the information is found at the beginning, and then the attribute name and the column name in the database table are connected in a resultMap.
  • But it should be noted that the SQL statement here is more complicated than the above, and it is important to note that as long as the collection element is used, the Javatype and oftype must be set.
    Insert picture description here

summary

  1. Association, used when there are objects in the attribute, used in a one-to-many environment
  2. Collection, used when there is a collection type in the attribute, used in a many-to-one environment
  3. javatype, used to specify the type in the specified entity attribute in java, just like this specifies the List type
  4. oftype, used to specify the specific type that is mapped to the generic type in the set of specified entity attributes in java. This specifies the type of Student in the List
  5. If you need to use the reference type, association, just indicate the attribute name, indicate the javatype, and then set the value inside.
  6. If it is a collection type, use collection, indicate the attribute name, indicate the javatype type, and the generic type oftype in the collection type, and then enter the result value in it.

important point:

  1. Ensure the readability of sql, try to ensure that it is easy to understand
  2. Pay attention to the matching of attribute names and field names in many-to-one and one-to-many, which are mostly used in logs

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/109372123