淺談MyBatis中@MapKey的妙用
MyBatis @MapKey的妙用
背景
在實(shí)際開發(fā)中,有一些場(chǎng)景需要我們返回主鍵或者唯一鍵為Key、Entity為Value的Map集合,如Map<Long, User>,之后我們就可以直接通過map.get(key)的方式來(lái)獲取Entity。
實(shí)現(xiàn)
MyBatis為我們提供了這種實(shí)現(xiàn),Dao示例如下:
public interface UserDao { ? ?
? ? @MapKey("id")
? ? Map<Long, User> selectByIdList(@Param("idList") List<Long> idList); ? ?
}需要注意的是:如果Mapper.xml中的select返回類型是List的元素,上面示例的話,resultType是User,因?yàn)閟electMap查詢首先是selectList,之后才是處理List。
源碼分析
package org.apache.ibatis.session.defaults;
public class DefaultSqlSession implements SqlSession {
? ... ...
? public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
? ? final List<?> list = selectList(statement, parameter, rowBounds);
? ? final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<K, V>(mapKey,
? ? ? ? configuration.getObjectFactory(), configuration.getObjectWrapperFactory());
? ? final DefaultResultContext context = new DefaultResultContext();
? ? for (Object o : list) {
? ? ? context.nextResultObject(o);
? ? ? mapResultHandler.handleResult(context);
? ? }
? ? Map<K, V> selectedMap = mapResultHandler.getMappedResults();
? ? return selectedMap;
? } ?
? ... ...
}selectMap方法其實(shí)是在selectList后的進(jìn)一步處理,通過mapKey獲取DefaultMapResultHandler類型的結(jié)果處理器,然后遍歷list,調(diào)用handler的handleResult把每個(gè)結(jié)果處理后放到map中,最后返回map。
package org.apache.ibatis.executor.result;
public class DefaultMapResultHandler<K, V> implements ResultHandler {
? private final Map<K, V> mappedResults; ?
? ... ...
? public void handleResult(ResultContext context) {
? ? // TODO is that assignment always true?
? ? final V value = (V) context.getResultObject();
? ? final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory);
? ? // TODO is that assignment always true?
? ? final K key = (K) mo.getValue(mapKey);
? ? mappedResults.put(key, value);
? }
? ... ... ?
}可以看出DefaultMapResultHandler是通過mapKey從元數(shù)據(jù)中獲取K,然后mappedResults.put(key, value)放到map中。
思考
@MapKey這種處理是在查詢完后做的處理,實(shí)際上我們也可以自己寫邏輯將List轉(zhuǎn)成Map,一個(gè)Lambda表達(dá)式搞定,如下:
? List<User> list = userDao.selectByIdList(Arrays.asList(1,2,3)); ? Map<Integer, User> map = list.stream().collect(Collectors.toMap(User::getId, user -> user));
Mybatis @MapKey分析
先上例子
@Test
public void testShouSelectStudentUsingMapperClass(){
//waring 就使用他作為第一個(gè)測(cè)試類
try (SqlSession session = sqlMapper.openSession()) {
StudentMapper mapper = session.getMapper(StudentMapper.class);
System.out.println(mapper.listStudentByIds(new int[]{1,2,3}));
}
}
@MapKey("id")
Map<Integer,StudentDo> listStudentByIds(int[] ids);
<select id="listStudentByIds" resultType="java.util.Map">
select * from t_student
where id in
<foreach collection="array" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</select>
結(jié)果

1. MapKey注解有啥功能
Mapkey可以讓查詢的結(jié)果組裝成Map,Map的key是@MapKey指定的字段,Value是實(shí)體類。如上圖所示
2. MapKey的源碼分析
還是從源碼分析一下他是怎么實(shí)現(xiàn)的,要注意MapKey不是寫在Xml中的,而是標(biāo)注在方法上的。所以,對(duì)于XML文件來(lái)說,他肯定不是在解析文件的時(shí)候操作的。對(duì)于Mapper注解實(shí)現(xiàn)來(lái)說,理論上來(lái)說是在解析的時(shí)候用的,但是對(duì)比XML的解析來(lái)說,應(yīng)該不是。多說一點(diǎn),想想Spring ,開始的時(shí)候都是XML,最后采用的注解,并且注解的功能和XML的對(duì)應(yīng)起來(lái),所以在解析XML是怎么解析的,在解析注解的時(shí)候就應(yīng)該是怎么解析的。
還是老套路,點(diǎn)點(diǎn)看看,看看哪里引用到了他,從下圖看到,用到的地方一個(gè)是MapperMethod,一個(gè)是MapperAnnotationBuilder,在MapperAnnotationBuilder里面只是判斷了一下,沒有啥實(shí)質(zhì)性的操作,這里就不用管。只看前者。

1. MapperMethod對(duì)MapKey的操作
看過之前文章的肯定知道,MapperMethod是在哪里創(chuàng)建的。這個(gè)是在調(diào)用mapper接口的查詢的時(shí)候創(chuàng)建的。接口方法的執(zhí)行最終會(huì)調(diào)用到這個(gè)對(duì)象的execute方法
MapperMethod里面包含兩個(gè)對(duì)象SqlCommand和MethodSignature,就是在MethodSignature里面引用了MapKey的
public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
//判斷此方法的返回值的類型
Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
if (resolvedReturnType instanceof Class<?>) {
this.returnType = (Class<?>) resolvedReturnType;
} else if (resolvedReturnType instanceof ParameterizedType) {
this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
} else {
this.returnType = method.getReturnType();
}
// 返回值是否為空
this.returnsVoid = void.class.equals(this.returnType);
// 返回是否是一個(gè)列表或者數(shù)組
this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
// 返回值是否返回一個(gè)游標(biāo)
this.returnsCursor = Cursor.class.equals(this.returnType);
// 返回值是否是一個(gè)Optional
this.returnsOptional = Optional.class.equals(this.returnType);
// 重點(diǎn)來(lái)了,這里會(huì)判斷返回值是一個(gè)MapKey,并且會(huì)將MapKey里Value的值賦值給MapKey
this.mapKey = getMapKey(method);
// 返回值是否是一個(gè)map
this.returnsMap = this.mapKey != null;
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); //找到方法參數(shù)里面 第一個(gè) 參數(shù)類型為ResultHandler的值
// 這里是處理方法參數(shù)里面的param注解, 注意方法參數(shù)里面有兩個(gè)特殊的參數(shù) RowBounds和 ResultHandler
// 這里會(huì)判斷@param指定的參數(shù),并且會(huì)將這些參數(shù)組成一個(gè)map,key是下標(biāo),value是param指定的參數(shù),如果沒有,就使用方法參數(shù)名
this.paramNameResolver = new ParamNameResolver(configuration, method);
}
上面的代碼這次最重要的是mapKey的賦值操作getMapKey,來(lái)看看他是什么樣子
private String getMapKey(Method method) {
String mapKey = null;
if (Map.class.isAssignableFrom(method.getReturnType())) {
final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);
if (mapKeyAnnotation != null) {
mapKey = mapKeyAnnotation.value();
}
}
return mapKey;
}
上面介紹了MapKey是在哪里解析的,下面分析Mapkey是怎么應(yīng)用的,拋開所有的不說,圍繞查詢來(lái)說。經(jīng)過上面的介紹。已經(jīng)對(duì)查詢的流程很清晰了,因?yàn)椴樵冞€是普通的查詢,所以,MapKey在組裝值的時(shí)候才會(huì)發(fā)送作用,下面就看看吧
還是老套路,既然賦值給MethodSignature的mapKey了,點(diǎn)點(diǎn)看看,哪里引用了他

下面的沒有啥可看的,看看上面,在MapperMethod里面用到了,那就看看
//看這個(gè)名字就能知道,這是一個(gè)執(zhí)行Map查詢的操作
private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Map<K, V> result;
Object param = method.convertArgsToSqlCommandParam(args);
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
// 將Map傳遞給sqlSession了,那就一直往下走
result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
result = sqlSession.selectMap(command.getName(), param, method.getMapKey());
}
return result;
}
一直點(diǎn)下去,就看到下面的這個(gè)了,可以看到,這里將mapKey傳遞給了DefaultMapResultHandler,對(duì)查詢的結(jié)果進(jìn)行處理。
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
//這已經(jīng)做了查詢了
final List<? extends V> list = selectList(statement, parameter, rowBounds);
final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<>(mapKey,
configuration.getObjectFactory(), configuration.getObjectWrapperFactory(), configuration.getReflectorFactory());
final DefaultResultContext<V> context = new DefaultResultContext<>();
// 遍歷list,利用mapResultHandler處理List
for (V o : list) {
context.nextResultObject(o);
mapResultHandler.handleResult(context);
}
return mapResultHandler.getMappedResults();
}
這里很明確了,先做正常的查詢,在對(duì)查詢到的結(jié)果做處理(DefaultMapResultHandler)。
2. DefaultMapResultHandler是什么
/**
* @author Clinton Begin
*/
public class DefaultMapResultHandler<K, V> implements ResultHandler<V> {
private final Map<K, V> mappedResults;
private final String mapKey;
private final ObjectFactory objectFactory;
private final ObjectWrapperFactory objectWrapperFactory;
private final ReflectorFactory reflectorFactory;
@SuppressWarnings("unchecked")
public DefaultMapResultHandler(String mapKey, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
this.objectFactory = objectFactory;
this.objectWrapperFactory = objectWrapperFactory;
this.reflectorFactory = reflectorFactory;
this.mappedResults = objectFactory.create(Map.class);
this.mapKey = mapKey;
}
// 邏輯就是這里,
@Override
public void handleResult(ResultContext<? extends V> context) {
//拿到遍歷的list的當(dāng)前值。
final V value = context.getResultObject();
//構(gòu)建MetaObject,
final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
// TODO is that assignment always true?
// 獲取mapKey指定的屬性,放在mappedResults里面。
final K key = (K) mo.getValue(mapKey);
mappedResults.put(key, value);
}
// 返回結(jié)果
public Map<K, V> getMappedResults() {
return mappedResults;
}
}
這里的邏輯很清晰,對(duì)查詢查到的list。做遍歷,利用反射獲取MapKey指定的字段,并且組成Map,放在一個(gè)Map(mappedResults,這默認(rèn)就是HashMap)里面。
問題?
1, 從結(jié)果中獲取mapkey字段的操作,這個(gè)字段總是有的嗎?

不一定,看這個(gè)例子,MapKey是一個(gè)不存在的屬性值,那么在Map里面就會(huì)存在一個(gè)Null,這是Hashmap決定的。
綜述:
在MapKey的使用中,要注意MapKey中Value字段的唯一性,否則就會(huì)造成Key值覆蓋的操作。同時(shí)也要注意,Key要肯定存在,否則結(jié)果就是null,(如果有特殊操作的話,就另說)話說回來(lái),這里我覺得應(yīng)該增加強(qiáng)校驗(yàn)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Java關(guān)鍵詞synchronized的使用
Synchronized是java虛擬機(jī)為線程安全而引入的。這篇文章主要為大家介紹一下Java關(guān)鍵詞synchronized的使用與原理,需要的可以參考一下2022-12-12
基于python locust庫(kù)實(shí)現(xiàn)性能測(cè)試
這篇文章主要介紹了基于python locust庫(kù)實(shí)現(xiàn)性能測(cè)試,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
SpringMVC實(shí)現(xiàn)文件上傳和下載功能
這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)文件上傳和下載功能 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
SpringBoot創(chuàng)建自定義Starter代碼實(shí)例
這篇文章主要介紹了SpringBoot創(chuàng)建自定義Starter代碼實(shí)例,自定義 Starter 是一種在軟件開發(fā)中常用的技術(shù),它可以幫助開發(fā)者快速搭建項(xiàng)目的基礎(chǔ)框架和配置,可以將一些常用的功能、依賴和配置封裝成一個(gè)可復(fù)用的模塊,方便在不同的項(xiàng)目中使用,需要的朋友可以參考下2023-11-11
Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理
這篇文章主要介紹了Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

