Mybatis' original body is Ibatis, and now it has separated from the Apache Foundation. The new official website is http://www.mybatis.org/.
I didn't know the magical use of this annotation before studying the Mybatis source code, but when I saw that there was this annotation when parsing the parameter, I understood that when we return types like Map<String, Map<String, Object>>, it is often difficult for us to do it, because it may be data from multiple tables, so it is impossible for us to build another model.
Then we can use this annotation
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface MapKey {String value();}It is obvious that this annotation is used on the method, and the specific usage is to set what the KEY of the external map is. This way we can query very complex results without creating a new entity.
PS: Let's take a look at MyBatis using @MapKey annotation to receive multiple query records into the Map, so as to easily obtain the value of the field using the get() method.
Requirement scenario:
Batch-find several pieces of data from the database, including two fields: id and name. I hope that the result can be received directly using Map, and then the value of name can be easily obtained through map.get(id).
question:
If you use the following code, if there are multiple query results, an error will be reported, because MyBatis saves the results in the Map in the form of ("id":123) and ("name":"Jack"). So if you return the result, it is OK to have a record including id and name; if you return multiple records, that is, there are multiple ("id":123) and ("id":124), then MyBatis will be stupid and don't know how to deal with it.
Map<String, Object> m = abcDao.getNamesByIds(idList);
The solution is to use another map outside:
Map<Integer, Map<String, Object>> m = abcDao.getNamesByIds(idList);
Then, add an annotation to this method:
<span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * Get the name in batches based on multiple ids <span style="white-space:pre"> </span> * @param list list containing Map key="id" <span style="white-space:pre"> </span> * @return <span style="white-space:pre"> </span> */ <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>The above is a detailed explanation of the use of @MapKey in Mybatis annotation introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!