MyBatis SQL mapping File Select tag and application to Resultmap tag

Source: Internet
Author: User

1. Customize the packaging rules for a javabean:


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/97/9F/wKioL1kwrCvxpj4pAAArdWlULiQ058.png "title=" 91. PNG "alt=" Wkiol1kwrcvxpj4paaardwluliq058.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/97/9F/wKioL1kwrAOA8hzvAABwPHDeTls926.png "style=" float : none; "title=" 92.png "alt=" Wkiol1kwraoa8hzvaabwphdetls926.png "/>


Note: You can see that the value of the defined Resultmap is referenced in the select tag. The type in the Resultmap tag is JavaBean and the ID is a unique identifier. The ID sub-tag defines the primary key (this defines the bottom layer as an optimization.) The Column property is the field name and the property attribute is the field value.


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/97/9E/wKiom1kwrATQ5dDRAAAwwVNL06Q580.png "style=" float : none; "title=" 93.png "alt=" Wkiom1kwratq5ddraaawwvnl06q580.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/97/9F/wKioL1kwrASD9pczAABBzqADOd0639.png "style=" float : none; "title=" 94.png "alt=" Wkiol1kwrasd9pczaabbzqadod0639.png "/>

Note: You can see that there is no problem with the test results and that the code is not error.


2. Customize the cascading properties of a JavaBean encapsulation:


A, preparation: MySQL database, on the basis of the previous Tbl_employees table (previous blog example), create a tbl_depts table, and add a d_id field in the Tbl_employees table, and establish a foreign key associated with the Tbl_depts table 。 (and test the SQL statement in the SQL script compiler of the SQLyog software) and add the Department attribute and the corresponding Set/get method to the Employee class.

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/97/C1/wKiom1kyZyLxPjbhAAFhBZBWa5o609.png "title=" 95. PNG "alt=" Wkiom1kyzylxpjbhaafhbzbwa5o609.png "/>


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/97/C1/wKiom1kyZyPwZ4iaAAAg3RDzVwg727.png "title=" 96. PNG "alt=" Wkiom1kyzypwz4iaaaag3rdzvwg727.png "/>


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/97/C2/wKioL1kyZyOjFGexAAAsrhgMSG8134.png "title=" 97. PNG "alt=" Wkiol1kyzyojfgexaaasrhgmsg8134.png "/>


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/97/C2/wKioL1kyZySRN4uNAABIh0e2yCU615.png "title=" 98. PNG "alt=" Wkiol1kyzysrn4unaabih0e2ycu615.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/97/C4/wKiom1kypE-j8vUpAAArG_prDig793.png "title=" 108. PNG "alt=" Wkiom1kype-j8vupaaarg_prdig793.png "/>


B, write the method in the interface, pass in the parameter ID. and write the Resultmap and select tags in the SQL mapping file. (as you can see, property= "dept.id", description mybatis support cascading queries)


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/97/C1/wKiom1kyaIPB6UX7AAAtRjugwUo150.png "title=" 99. PNG "alt=" Wkiom1kyaipb6ux7aaatrjugwuo150.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/97/C1/wKiom1kyaISApO3xAABSJq5enZI181.png "style=" float : none; "title=" 100.png "alt=" Wkiom1kyaisapo3xaabsjq5enzi181.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/97/C2/wKioL1kyaIXTFKpQAACPJskArwg905.png "title=" 101. PNG "alt=" Wkiol1kyaixtfkpqaacpjskarwg905.png "/>


C, the association label with the RESULTMAP label is defined (you need to specify the JavaBean type), you can also get the same effect.


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/97/C1/wKiom1kybVyAvT-aAACl1iZcK4w964.png "title=" 102. PNG "alt=" Wkiom1kybvyavt-aaacl1izck4w964.png "/>

3, the Use of association for step-by query function:


A, preparation: Add a Departmentmapper interface (the interface defines the Getdeptbyid method) and a departmentmapper.xml mapping file. (The select tag is defined in the mapping file and mapped with the interface method)


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/97/C3/wKioL1kydMKizBjNAAAmaHxvQZk848.png "title=" 103. PNG "alt=" Wkiol1kydmkizbjnaaamahxvqzk848.png "/>


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/97/C3/wKioL1kydMKxdbTuAABHEWGWD6M001.png "title=" 104. PNG "alt=" Wkiol1kydmkxdbtuaabhewgwd6m001.png "/>


b, add the Getempbystep method to the Employeemapperplus interface, and add the Select tag in the Employeemapper.xml file. (There is a association sub-tag in the select tag, the attribute select is defined in the label, and column (the field that needs to be passed in))


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/97/C2/wKiom1kyde2yL9IaAAA2RvAYOsY260.png "title=" 106. PNG "alt=" Wkiom1kyde2yl9iaaaa2rvayosy260.png "/>


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/97/C2/wKiom1kydafRdOS6AABGChq72zE989.png "title=" 105. PNG "alt=" Wkiom1kydafrdos6aabgchq72ze989.png "/>


Note: The deferred load setting for a step-by-point query sets two properties in the global configuration file (that is, the Mybatis-config.xml file). (In Settings tags)


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/97/C4/wKioL1kyfHTyc3XxAAAtFUoG0tA790.png "title=" 107. PNG "alt=" Wkiol1kyfhtyc3xxaaatfuog0ta790.png "/>


4. Collection defines the wrapper rules for association objects:


A, add the list property of the Employee to the Departmnet class, and add a Getdeptbyidplus method to the Departmentmapper interface.


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/97/C6/wKioL1kypY_RJtETAAAPzLyD2bk121.png "title=" 109. PNG "alt=" Wkiol1kypy_rjtetaaapzlyd2bk121.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/97/C4/wKiom1kypY_DOkaFAAAv3dCDZqc741.png "title=" 110. PNG "alt=" Wkiom1kypy_dokafaaav3dcdzqc741.png "/>


b, as you can see, add a Select tag to the Departmentmapper.xml SQL mapping file, and SQL in the label uses a left outer join to correlate the tbl_employees and tbl_depts two tables. and use the collection tag in the RESULTMAP tag to encapsulate the collection properties. (OfType represents the element type in the collection)


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/97/C6/wKioL1kypZCAvACJAAC1WJ_YgE0187.png "title=" 111. PNG "alt=" Wkiol1kypzcavacjaac1wj_yge0187.png "/>


C, shown, the test results are not a problem, showing the expected results


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/97/C6/wKioL1kypZGQPD4fAAA6AAeQkuE953.png "title=" 112. PNG "alt=" Wkiol1kypzgqpd4faaa6aaeqkue953.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/97/C4/wKiom1kypZHy9DaUAABQ4rxJ-6M437.png "title=" 113. PNG "alt=" Wkiom1kypzhy9dauaabq4rxj-6m437.png "/>


5, the use of collection tags for the step query function:


A, add the Getempbydeptid method to the Employeemapperplus interface, and add a select tag in the Employeemapperplus.xml SQL mapping file to remove the qualifying record.


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/97/C6/wKiom1kysP-haGnSAAA_0EPuqYU978.png "title=" 120. PNG "alt=" Wkiom1kysp-hagnsaaa_0epuqyu978.png "/>


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/97/C7/wKioL1kysQDjlDx2AAAQjyK2MYk382.png "title=" 122. PNG "alt=" Wkiol1kysqdjldx2aaaqjyk2myk382.png "/>


b, add a Getdeptbyid interface in the Departmentmapper interface, add a collection tag to the Departmentmapper.xml SQL mapping file, add the Select attribute to the label, and the Employee Mapperplus.getempbydeptid is associated with the column= "id", which represents the passed-in parameter ID.


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/97/C7/wKioL1kysL6igclcAAAw2RSShxw878.png "style=" float : none; "title=" 118.png "alt=" Wkiol1kysl6igclcaaaw2rsshxw878.png "/>


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/97/C6/wKiom1kysL_DZpQEAAA8500k20A467.png "style=" float : none; "title=" 119.png "alt=" Wkiom1kysl_dzpqeaaa8500k20a467.png "/>


C, test output results, no problem.


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/97/C8/wKioL1kysr6hsfdOAAC1WJ_YgE0972.png "title=" 111. PNG "alt=" Wkiol1kysr6hsfdoaac1wj_yge0972.png "/>


Note: Use column for multi-column value passing

Multi-column value passing can encapsulate map delivery---> column= "{key1=column1, key2=column2}, such as:


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/97/C8/wKioL1kytRjC9UURAAAu08Z1Wig269.png "title=" 125. PNG "alt=" Wkiol1kytrjc9uuraaau08z1wig269.png "/>



This article is from the "12392717" blog, please be sure to keep this source http://12402717.blog.51cto.com/12392717/1931980

MyBatis SQL mapping File Select tag and application to Resultmap tag

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.