Use of annotation @MapKey in Mybatis

  • 2020-05-12 02:38:53
  • OfStack

mybatis original body is ibatis, is now out of apache foundation, the new website is http: / / www mybatis. org /.

I didn't know the magic of this annotation until I looked at the Mybatis source code, but when I saw the parameter parsing there was this one annotation, so I learned 1 when we returned something like Map < String, Map < String, Object > > This type of time, we often difficult to do, because there may be more than one table of data, so we can not build a model.

At this point we can use this annotation


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MapKey {
String value();
}

Obviously, this annotation is applied to the method, and the specific usage is to set what KEY of Map is outside. This allows us to query very complex results without having to create a new entity.

PS: let's see that MyBatis receives multiple query records into Map using the @MapKey annotation so that it is easy to get the values of the fields using the get() method

Requirements scenario:

A number of data are retrieved from the database in batches, including the id and name fields. You want to be able to receive the result directly in Map, and then easily get the value of name through map.get (id).

Question:

If you use the following code, you will get an error if the query results are multiple, because MyBatis stores the results in Map (" id":123), ("name":"Jack"). So if you return a record that includes id and name, that's fine; If multiple records are returned, i.e., multiple ("id":123), ("id":124), then MyBatis is at a loss for what to do.


Map<String, Object> m = abcDao.getNamesByIds(idList); 

The solution is to use another Map on the outside:


Map<Integer, Map<String, Object>> m = abcDao.getNamesByIds(idList); 

Then, add a note to the dao method:


<span style="white-space:pre"> </span>/** 
<span style="white-space:pre"> </span> *  According to the multiple id Batch fetch name  
<span style="white-space:pre"> </span> * @param list  contains Map key="id" the list 
<span style="white-space:pre"> </span> * @return 
<span style="white-space:pre"> </span> */ 
<span style="white-space:pre"> </span>@MapKey("id") 
<span style="white-space:pre"> </span>public Map<Integer, Map<String, Object>> getNamesByIds(List<Map<String, Object>> list); 

This annotation indicates that the key of the outermost Map is the value of the field named "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> 
</select> 

Related articles: