1. nested resultMap
This method is essentially the method described in the previous blog post. It just extracts the ing of the instructor entity from the association element and represents it with a resultMap element. Then the association element references the resultMap element. Modify the StudentMapper. xml in the previous blog as follows:
<? Xmlversion = "1.0" encoding = "utf8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" 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_area
From student s left join teacher t
On s. supervisor_id = t. id
Where s. id = # {id}
</Select>
<ResultMapid = "studentResultMap" type = "Student">
<Idproperty = "id" column = "s_id"/>
<Resultproperty = "name" column = "s_name"/>
<Resultproperty = "gender" column = "s_gender"/>
<Resultproperty = "major" column = "s_major"/>
<Resultproperty = "grade" column = "s_grade"/>
<! -- Use the resultMap attribute to reference the following instructor entity ing -->
<Associationproperty = "supervisor" javaType = "Teacher"
ResultMap = "supervisorResultMap"/>
</ResultMap>
<! -- Instructor entity ing -->
<ResultMapid = "supervisorResultMap" type = "Teacher">
<Idproperty = "id" column = "t_id"/>
<Resultproperty = "name" column = "t_name"/>
<Resultproperty = "gender" column = "t_gender"/>
<Resultproperty = "researchArea" column = "t_research_area"/>
<Resultproperty = "title" column = "t_title"/>
</ResultMap>
</Mapper>
Here, aliases are used for each field to make the select statement clearer and readable. The attribute javaType = "Teacher" of the association element is optional. If this attribute is not specified in the previous blog, an error is returned. This is probably because the referenced resultMap element has indicated that the ing type is "Teacher ". In addition, it was accidentally discovered that the encoding attribute of StudentMapper. xml could be changed from "UTF-8" to "utf8" to add Chinese comments. The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1224392T7-0.png "border =" 0 "/>
The advantage of this method is that the resultMap element can be reused elsewhere and the sample source code can be downloaded at the attachment download section below the blog ).
Ii. nested select statements
This method uses a separate select statement to load the associated entity. In this example, it is the instructor entity.) then, reference this select statement in the association element. Note: this method generates N + 1 problems. For more information, see "N + 1 Problems in MyBatis" in this blog series "). Modify StudentMapper. xml as follows:
<? Xmlversion = "1.0" encoding = "utf8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" 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_area
From teacher where id =#{ id}
</Select>
<ResultMapid = "studentResultMap" type = "Student">
<Idproperty = "id" column = "id"/>
<Resultproperty = "name" column = "name"/>
<Resultproperty = "gender" column = "gender"/>
<Resultproperty = "major" column = "major"/>
<Resultproperty = "grade" column = "grade"/>
<! -- Column = "supervisor_id" cannot be less. The value of this column is used as a parameter.
The select statement to be referenced to query the instructor of the corresponding student.
. Select attribute specifies the select statement to be referenced -->
<Associationproperty = "supervisor" javaType = "Teacher"
Column = "supervisor_id" select = "selectSupervisor"/>
</ResultMap>
<! -- Instructor entity ing -->
<ResultMapid = "supervisorResultMap" type = "Teacher">
<Idproperty = "id" column = "id"/>
<Resultproperty = "name" column = "name"/>
<Resultproperty = "gender" column = "gender"/>
<Resultproperty = "researchArea" column = "research_area"/>
<Resultproperty = "title" column = "title"/>
</ResultMap>
</Mapper>
The attribute javaType = "Teacher" of the association element is optional, probably because the resultMap to be used has been specified in the referenced select statement.
The execution result is as follows. You can download the sample source code from the attachment download section below the blog ):
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1224395348-1.png "border =" 0 "/>
MyBatis Study Notes series I: download and install ant
Prepare for MyBatis Study Notes Series II: ant getting started
MyBatis learning notes: MyBatis getting started
MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis
MyBatis Study Notes Series 3: association examples of MyBatis
MyBatis Study Notes Series 4: two forms of MyBatis association
MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring
MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring
MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association
MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration
MyBatis Study Notes: two forms of MyBatis collection
MyBatis Study Notes Series 10: Log4j example of MyBatis logs
MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters
MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer
MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer
MyBatis Study Notes: N + 1 in MyBatis
MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis
MyBatis learning notes: Example of Spring declarative Transaction Management
MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/940072