The use of annotation @mapkey in MyBatis _java

Source: Internet
Author: User

MyBatis's original body is Ibatis, has now been divorced from the Apache Foundation, the net is http://www.mybatis.org/.

Before studying the MyBatis source code does not know this annotation's magical work, but when I saw the parameter parsing has this one annotation, therefore I understood a bit, when we return like Map<string, Map<string, object>> This type of time, we are often difficult to do, because there may be more than one table of data, so we can not build a model.

That's when we can use this annotation.

@Retention (retentionpolicy.runtime)
@Target (elementtype.method) public
@interface Mapkey {
String Value ();
}

Obviously, this annotation works on the method, and the specific usage is to set what the key of the outer map is. This allows us to query for very complex results without having to create a new entity.

PS: Look below MyBatis use @mapkey annotations to receive multiple queries to the map so that you can easily get the value of the field with the Getting () method

Requirements Scenario:

A batch of data is detected from the database, including ID and name two fields. It is hoped that the results can be received directly from the map and then conveniently obtained by Map.get (ID) the value of name.

Problem:

If you use the following code, if the query result is more than one error, because MyBatis is the result ("id": 123), ("name": "Jack") in the form of the saved in the map. So if you return a result that includes IDs and name records, there is no problem; If you return more than one record, there are multiple ("id": 123), ("id": 124), then the mybatis is silly to do not know how to deal with.

 
 

The solution is to use a map out of the way:

 
 

Then, add an annotation to the DAO's method:

<span style= "White-space:pre" > </span>/** 
<span style= "White-space:pre" > </span> * Bulk-get names based on multiple IDs 
<span style= "White-space:pre" > </span> * @param list contains a map key= "id" list 
<span style= "White-space:pre" > </span> * @return 
<span style= "White-space:pre" > </span> 
*/ <span style= "White-space:pre" > </span> @MapKey ("id") 

This annotation indicates that the key of the outermost map is the value of the field name "ID" in the query result.

The configuration in Mapper.xml is as follows:

<select id= "Getnamesbyids" resulttype= "Java.util.Map" > 
 select ID, name from tb_abc WHERE ID in 
 < foreach item= "Item" collection= "List" open= "(" separator= "," close= ")" > 
   #{item.id} 
 </foreach> 

The above is a small set to introduce the MyBatis annotation @mapkey in the use of detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.