Two forms of MyBatis association-MyBatis study note 4

Source: Internet
Author: User

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:
 
 
  1. <? Xmlversion = "1.0" encoding = "utf8"?>

  2. <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd ">

  3. <Mappernamespace = "com. abc. mapper. StudentMapper">

  4. <Selectid = "getById" parameterType = "int" resultMap = "studentResultMap">

  5. Select s. id s_id,

  6. S. name s_name,

  7. S. gender s_gender,

  8. S. major s_major,

  9. S. grade s_grade,

  10. T. id t_id,

  11. T. name t_name,

  12. T. gender t_gender,

  13. T. title t_title,

  14. T. research_area t_research_area

  15. From student s left join teacher t

  16. On s. supervisor_id = t. id

  17. Where s. id = # {id}

  18. </Select>

  19. <ResultMapid = "studentResultMap" type = "Student">

  20. <Idproperty = "id" column = "s_id"/>

  21. <Resultproperty = "name" column = "s_name"/>

  22. <Resultproperty = "gender" column = "s_gender"/>

  23. <Resultproperty = "major" column = "s_major"/>

  24. <Resultproperty = "grade" column = "s_grade"/>

  25. <! -- Use the resultMap attribute to reference the following instructor entity ing -->

  26. <Associationproperty = "supervisor" javaType = "Teacher"

  27. ResultMap = "supervisorResultMap"/>

  28. </ResultMap>

  29. <! -- Instructor entity ing -->

  30. <ResultMapid = "supervisorResultMap" type = "Teacher">

  31. <Idproperty = "id" column = "t_id"/>

  32. <Resultproperty = "name" column = "t_name"/>

  33. <Resultproperty = "gender" column = "t_gender"/>

  34. <Resultproperty = "researchArea" column = "t_research_area"/>

  35. <Resultproperty = "title" column = "t_title"/>

  36. </ResultMap>

  37. </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:
 
 
  1. <? Xmlversion = "1.0" encoding = "utf8"?>

  2. <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd ">

  3. <Mappernamespace = "com. abc. mapper. StudentMapper">

  4. <Selectid = "getById" parameterType = "int" resultMap = "studentResultMap">

  5. Select id, name, gender, major, grade, supervisor_id from student where id =#{ id}

  6. </Select>

  7. <Selectid = "selectSupervisor" parameterType = "int" resultMap = "supervisorResultMap">

  8. Select id, name, gender, title, research_area

  9. From teacher where id =#{ id}

  10. </Select>

  11. <ResultMapid = "studentResultMap" type = "Student">

  12. <Idproperty = "id" column = "id"/>

  13. <Resultproperty = "name" column = "name"/>

  14. <Resultproperty = "gender" column = "gender"/>

  15. <Resultproperty = "major" column = "major"/>

  16. <Resultproperty = "grade" column = "grade"/>

  17. <! -- Column = "supervisor_id" cannot be less. The value of this column is used as a parameter.

  18. The select statement to be referenced to query the instructor of the corresponding student.

  19. . Select attribute specifies the select statement to be referenced -->

  20. <Associationproperty = "supervisor" javaType = "Teacher"

  21. Column = "supervisor_id" select = "selectSupervisor"/>

  22. </ResultMap>

  23. <! -- Instructor entity ing -->

  24. <ResultMapid = "supervisorResultMap" type = "Teacher">

  25. <Idproperty = "id" column = "id"/>

  26. <Resultproperty = "name" column = "name"/>

  27. <Resultproperty = "gender" column = "gender"/>

  28. <Resultproperty = "researchArea" column = "research_area"/>

  29. <Resultproperty = "title" column = "title"/>

  30. </ResultMap>

  31. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.