MyBatis (VIII): Advanced mapping results

This article is in accordance with mad God said notes instructional videos to learn, highly recommended, again in layman's language teaching to understand! b Search mad God says or click the link below

https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2

United-table query

Preparing the Environment

  • Two very simple table, table records the student's teacher pk

 

 

Zero, with a simple table lookup

  • The following two entity classes, students record the teacher's table pk ----> Teacher's id is Student of tid.

// omitted without parameters, there are configuration parameters, get, set, override toString 
public  class Student {
     Private  int ID;
     Private String name;
     Private  int TID; 
} 
public  class Teacher {
     Private  int ID;
     Private String name; 
}

 

1. If each student needs is to find out information and corresponding teacher's name, then the sql statement should be
 
select s.*,t,name from student s,teacher t where s.tid = t.id

 

2. Direct the results on the map inside it
  • Mapper.xml

<mapper namespace="com.rzp.dao.StudentMapper">
    <select id="getByTeacher" resultType="map">
            select s.*,t.name as tname from student s,teacher t where t.id = s.tid
    </select>
</mapper>

 

  • Mapper Interface
public interface StudentMapper {
    List<HashMap> getByTeacher();
}

 

  • test

    @Test
    public void getByTeacher(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<HashMap> studentList = mapper.getByTeacher();
        for (Map student : studentList) {
            //通过get方法可以直接取出来
            System.out.print(student.get("id")+"  ");
            System.out.print(student.get("name")+"  ");
            System.out.print(student.get("tid")+"  ");
            System.out.print(student.get("tname")+"  ");
            System.out.println();
        }
        sqlSession.close();
    }

 

 

 

3. This way if you just do some statistics should be relatively easy to use, but if Shihai not as direct as in this case a temp Riga Student property, but also directly to such a generic set of Student List, the operation more convenient Student .
4. However, the following two cases not easy to use:
  • If the entity is another attribute class entity class object, such as a property, there Teacher Student object.

     

     

  • Or is an entity class attributes of another entity class is a container, such as a Student Teacher List of there.

 

 

  • This time is necessary to use an object (associtation) and result set mapping set (Collection) property.

  • My understanding is actually the properties of the ordinary result set mapping replace associtation it feels like in the xml file also defines an entity class, which objects to use associtation defined.

Associtation

First, according to a nested query processing

  • Student in Teacher subject to the

// omitted without parameters, there are configuration parameters, get, set, override toString 
public  class Student {
     Private  int ID;
     Private String name;
     Private  int TID;
     Private Teacher Teacher; 
} 
public  class Teacher {
     Private  int ID;
     Private String name; 
}

 

  • Mapper Interface

public  interface StudentMapper {
     // get all students information 
    List <Student> getAllStudent (); 
}

 

  • Mapper.xml

<Mapper namespace = "com.rzp.dao.StudentMapper"> 
    <-! query corresponding to all students and teachers, thinking:
     1 . All student information query
     2 according to inquiries from students tid find the corresponding teacher.
     -> 
    <SELECT ID = "getAllStudent" the resultMap = "StudentMap"> 
        SELECT * from Student
     </ SELECT> 
    <the resultMap ID = "StudentMap" type = "Student"> 
        <- complex properties, need to be processed separately:! associtation set: Collection -> 
        <!- result set mapping is defined in an "object" (associtation) Properties 
        Property --- Name student in this object 
        the javaType --- class student in this object 
        the SELECT --- get this object query (method) 
        column--- This parameter statement to be entered, the plurality is = column "{the prop1 = col1, col2 = prop2}" 
        -> 
        <Association Property = "Teacher" column = "TID" the javaType = "Teacher" SELECT = " getAllStudendTest "/> 
    </ The resultMap> <SELECT ID =" getAllStudendTest "the resultType =" Teacher "> 
        SELECT * WHERE from Teacher ID = # {ID}
     </ SELECT> 
</ Mapper>
    
        

 

  • Test Methods

    //查询所有学生
    @Test
    public void getAllTeacher(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getAllStudent();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }
​

 

  • This way personal feeling is not in line with the general thinking queries, more like nested subqueries.

Second, according to the results of processing nested

  • Mapper Interface

     // accordance with the processing result of the nested 
    List <Student> getAllStudent2 ();

 

  • Mapper.xml

    <select id="getAllStudent2" resultMap="StudentMap2">
        select s.id,s.name,s.tid,t.name tname
        from student s,teacher t
        where s.tid = t.id
    </select>
    <resultMap id="StudentMap2" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="tid" column="tid"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

 

  • test

    //查询所有学生2
    @Test
    public void getAllTeacher2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getAllStudent2();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }

 


 

 

  • Note: Among the test results, id teacher of the object is 0, personally feel that much like a constructor parameter.

 

 

collection

Third, according to the results of processing nested

  • Entity class

public class Student {
    private int id;
    private String name;
    private int tid;
}
public class Teacher {
    private int id;
    private String name;
    private List<Student> studentsList;
}

 


  • Mapper.xml

    <!--按结果嵌套查询-->
    <select id="getTeacherById" resultMap="TeacherMap">
        select s.*,t.name tname from student s,teacher t
        where s.tid = t.id and t.id = #{tid}
    </select><resultMap id="TeacherMap" type="Teacher">
        <result property="name" column="tname"/>
        <collection property="studentsList" ofType="Student" >
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

 

  • Mapper Interface

    Teacher getTeacherById(@Param("tid") int id);

 

  • test

    @Test
    public void test1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacherById(1);
        System.out.println(teacher);
        sqlSession.close();
    }
​

 

 

 

Fourth, according to a nested query processing

  • Mapper.xml

    <!--按查询嵌套处理-->
    <select id="getTeacherById2" resultMap="TeacherMap1">
        select t.* from teacher t where t.id = #{id}
    </select>

    <resultMap id="TeacherMap1" type="Teacher">
        <result property="id" column="id"/>
        <!--ofType--泛型-->
        <collection property="studentsList" javaType="ArrayList" ofType="Student" column="id" select="getStudent"/>
    </resultMap>

    <select id="getStudent" resultType="Student">
        select * from student where tid = #{id}
    </select>

 

  • Mapper Interface

   Teacher getTeacherById2(int id);

 

  • test

    @Test
    public void test2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacherById2(1);
        System.out.println(teacher);
        sqlSession.close();
    }

 

 

 

Compared

  • According to the results of the query, sql complex, less the number of inquiries, simple xml structure.

  • Press nested query processing, sql simple query many times, complicated xml structure.

Personally feel that the results of the query by a simpler, xml complex structure can be difficult to troubleshoot.

Guess you like

Origin www.cnblogs.com/renzhongpei/p/12590310.html