One, nested resultmap
This method is essentially the method described in the previous blog post, except that the teacher entity mapping is extracted from the association element and represented by a RESULTMAP element. The association element then references the Resultmap element. The Studentmapper.xml to modify the previous post example is as follows:
<?XML version= "1.0" encoding= "UTF8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Com.abc.mapper.StudentMapper"><SelectID= "GetById"ParameterType= "int"Resultmap= "Studentresultmap">Select S.id s_id,s.name s_name,s.gender s_gender,s.major s_major,s.grade s_grade,t.id t_id,t.name t_name,t.gender T_gender,t.title T_title,t.research_area T_research_areafrom student s left join teacher ton s.supervisor_id = T.idwhere s . Id=#{id}</Select><ResultmapID= "Studentresultmap"type= "Student"><ID Property= "id"column= "s_id"/><result Property= "Name"column= "S_name"/><result Property= "Gender"column= "S_gender"/><result Property= "Major"column= "S_major"/><result Property= "Grade"column= "S_grade"/><!--Use the Resultmap property to refer to the following teacher entity mappings -<Association Property= "Supervisor"Javatype= "Teacher"Resultmap= "Supervisorresultmap"/></Resultmap><!--Teacher Entity Mapping -<ResultmapID= "Supervisorresultmap"type= "Teacher"><ID Property= "id"column= "t_id"/><result Property= "Name"column= "T_name"/><result Property= "Gender"column= "T_gender"/><result Property= "Researcharea"column= "T_research_area"/><result Property= "title"column= "T_title"/></Resultmap></Mapper>
Here, in order to make the SELECT statement more readable, each field uses an alias. The attribute of the association element javatype= "Teacher" can not, unlike the previous post example, without this attribute is an error. This is probably because the referenced Resultmap element already indicates that the type of the map is "Teacher". In addition, it is discovered today that the encoding attribute of Studentmapper.xml is changed from "UTF-8" to "UTF8" to add Chinese annotation.
The results of the operation are as follows:
The advantage of this approach is that this resultmap element can be reused elsewhere (download the sample source at the download of the attachment below the blog post).
Second, nested SELECT statements
This approach is to use a separate SELECT statement to load the associated entity (in this case the teacher entity) and then reference this SELECT statement in the association element (Note: This method produces a n+1 problem, which can be referred to in this blog series " N+1 problem in MyBatis "). Modify the Studentmapper.xml as follows:
<?XML version= "1.0" encoding= "UTF8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Com.abc.mapper.StudentMapper"><SelectID= "GetById"ParameterType= "int"Resultmap= "Studentresultmap">Select id,name,gender,major,grade,supervisor_id from student where Id=#{id}</Select><SelectID= "Selectsupervisor"ParameterType= "int"Resultmap= "Supervisorresultmap">Select Id,name,gender,title,research_areafrom Teacher where Id=#{id}</Select><ResultmapID= "Studentresultmap"type= "Student"><ID Property= "id"column= "id"/><result Property= "Name"column= "Name"/><result Property= "Gender"column= "Gender"/><result Property= "Major"column= "Major"/><result Property= "Grade"column= "Grade"/><!--column= "supervisor_id" cannot be less. The value of this column is passed as a parameter to the SELECT statement to be referenced, which is used to query the information of the appropriate student's instructor. Select property specifies the SELECT statement to reference -<Association Property= "Supervisor"Javatype= "Teacher"column= "supervisor_id"Select= "Selectsupervisor"/></Resultmap><!--Teacher Entity Mapping -<ResultmapID= "Supervisorresultmap"type= "Teacher"><ID Property= "id"column= "id"/><result Property= "Name"column= "Name"/><result Property= "Gender"column= "Gender"/><result Property= "Researcharea"column= "Research_area"/><result Property= "title"column= "title"/></Resultmap></Mapper>
The attribute of the association element javatype= "Teacher" is also not possible, which is probably because the referenced SELECT statement already indicates the RESULTMAP to be used.
The results are as follows (you can download the sample source at the download section below the blog post):
MyBatis Association's two forms--mybatis study notes four