浅谈MyBatis中@MapKey的妙用
作者:totally123 发布时间:2023-09-25 10:42:02
MyBatis @MapKey的妙用
背景
在实际开发中,有一些场景需要我们返回主键或者唯一键为Key、Entity为Value的Map集合,如Map<Long, User>,之后我们就可以直接通过map.get(key)的方式来获取Entity。
实现
MyBatis为我们提供了这种实现,Dao示例如下:
public interface UserDao {
@MapKey("id")
Map<Long, User> selectByIdList(@Param("idList") List<Long> idList);
}
需要注意的是:如果Mapper.xml中的select返回类型是List的元素,上面示例的话,resultType是User,因为selectMap查询首先是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方法其实是在selectList后的进一步处理,通过mapKey获取DefaultMapResultHandler类型的结果处理器,然后遍历list,调用handler的handleResult把每个结果处理后放到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从元数据中获取K,然后mappedResults.put(key, value)放到map中。
思考
@MapKey这种处理是在查询完后做的处理,实际上我们也可以自己写逻辑将List转成Map,一个Lambda表达式搞定,如下:
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 就使用他作为第一个测试类
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>
结果
1. MapKey注解有啥功能
Mapkey可以让查询的结果组装成Map,Map的key是@MapKey指定的字段,Value是实体类。如上图所示
2. MapKey的源码分析
还是从源码分析一下他是怎么实现的,要注意MapKey不是写在Xml中的,而是标注在方法上的。所以,对于XML文件来说,他肯定不是在解析文件的时候操作的。对于Mapper注解实现来说,理论上来说是在解析的时候用的,但是对比XML的解析来说,应该不是。多说一点,想想Spring ,开始的时候都是XML,最后采用的注解,并且注解的功能和XML的对应起来,所以在解析XML是怎么解析的,在解析注解的时候就应该是怎么解析的。
还是老套路,点点看看,看看哪里引用到了他,从下图看到,用到的地方一个是MapperMethod,一个是MapperAnnotationBuilder,在MapperAnnotationBuilder里面只是判断了一下,没有啥实质性的操作,这里就不用管。只看前者。
1. MapperMethod对MapKey的操作
看过之前文章的肯定知道,MapperMethod是在哪里创建的。这个是在调用mapper接口的查询的时候创建的。接口方法的执行最终会调用到这个对象的execute方法
MapperMethod里面包含两个对象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);
// 返回是否是一个列表或者数组
this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
// 返回值是否返回一个游标
this.returnsCursor = Cursor.class.equals(this.returnType);
// 返回值是否是一个Optional
this.returnsOptional = Optional.class.equals(this.returnType);
// 重点来了,这里会判断返回值是一个MapKey,并且会将MapKey里Value的值赋值给MapKey
this.mapKey = getMapKey(method);
// 返回值是否是一个map
this.returnsMap = this.mapKey != null;
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); //找到方法参数里面 第一个 参数类型为ResultHandler的值
// 这里是处理方法参数里面的param注解, 注意方法参数里面有两个特殊的参数 RowBounds和 ResultHandler
// 这里会判断@param指定的参数,并且会将这些参数组成一个map,key是下标,value是param指定的参数,如果没有,就使用方法参数名
this.paramNameResolver = new ParamNameResolver(configuration, method);
}
上面的代码这次最重要的是mapKey的赋值操作getMapKey,来看看他是什么样子
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是怎么应用的,抛开所有的不说,围绕查询来说。经过上面的介绍。已经对查询的流程很清晰了,因为查询还是普通的查询,所以,MapKey在组装值的时候才会发送作用,下面就看看吧
还是老套路,既然赋值给MethodSignature的mapKey了,点点看看,哪里引用了他
下面的没有啥可看的,看看上面,在MapperMethod里面用到了,那就看看
//看这个名字就能知道,这是一个执行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;
}
一直点下去,就看到下面的这个了,可以看到,这里将mapKey传递给了DefaultMapResultHandler,对查询的结果进行处理。
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
//这已经做了查询了
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();
}
这里很明确了,先做正常的查询,在对查询到的结果做处理(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的当前值。
final V value = context.getResultObject();
//构建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);
}
// 返回结果
public Map<K, V> getMappedResults() {
return mappedResults;
}
}
这里的逻辑很清晰,对查询查到的list。做遍历,利用反射获取MapKey指定的字段,并且组成Map,放在一个Map(mappedResults,这默认就是HashMap)里面。
问题?
1, 从结果中获取mapkey字段的操作,这个字段总是有的吗?
不一定,看这个例子,MapKey是一个不存在的属性值,那么在Map里面就会存在一个Null,这是Hashmap决定的。
综述:
在MapKey的使用中,要注意MapKey中Value字段的唯一性,否则就会造成Key值覆盖的操作。同时也要注意,Key要肯定存在,否则结果就是null,(如果有特殊操作的话,就另说)话说回来,这里我觉得应该增加强校验。
来源:https://blog.csdn.net/totally123/article/details/102723732


猜你喜欢
- 循环结构可以实现一个程序模块的重复执行,它对于我们简化程序,更好地组织算法有着重要的意义。C#为我们提供了若干种循环语句,分别适用于不同的情
- 本文实例讲述了Java实现的求解经典罗马数字和阿拉伯数字相互转换问题。分享给大家供大家参考,具体如下:古罗马帝国开创了辉煌的人类文明,但他们
- 本文实例讲述了Android弹出窗口实现方法。分享给大家供大家参考,具体如下:直接上代码:/*** 弹窗--新手指引* @param cxt
- 一、获取接口请求的数据可以在Interceptor的afterCompletion中实现但是要重写RequestWrapper代码记录如下:
- 一. 多任务和Task、启动模式Android 手机在早期,下方通常会内置三个实体的触摸按键,分别是:桌面、菜单、返回。大概在Android
- 这篇文章主要介绍了spring如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- thymeleaf介绍简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP
- CompletableFuture 介绍CompletableFuture是1.8引入的新特性,一些比较复杂的异步计算场景,尤其是需要串联多
- 一、对AOP的初印象首先先给出一段比较专业的术语(来自百度):在软件业,AOP为Aspect Oriented Programming的缩写
- 一、@Value读取application.properties配置文件中的值application.properties配置文件fileN
- EasyCode 插件EasyCode 插件 是一款根据表结构生成代码的很方便的Idea插件, 强烈推荐. 并且可以自定义模板来控制生成的类
- Spring Boot是什么众所周知 Spring 应用需要进行大量的配置,各种 XML 配置和注解配置让人眼花缭乱,且极容易出错,因此 S
- CancellationTokenCancellationToken有一个构造函数,可以传入一个bool类型表示当前的Cancellatio
- 何时需要削峰当上游调用下游服务速率高于下游服务接口QPS时,那么如果不对调用速率进行控制,那么会发生很多失败请求通过消息队列的削峰方法有两种
- springboot 2.0 mybatis mapper-locations扫描多个路径mapper-locations扫描多个路径,中间
- 我们在开发Java项目的时候,经常需要对参数进行一些必填项、格式、长度等进行校验,如果手写代码对参数校验,每个接口会需要很多低级的代码,这样
- Java反射机制深入理解一.概念 反射就是把Java的各种成分映射成相应的Java类。Class类的构造方法是private,由JVM创建。
- 1、下载内嵌浏览器Jar包下载地址:点击下载2、项目下加入对应jar;然后右键:Add as Library...3、添加启动项目后事件效果
- 统计输入的行数标准库保证输入文本流以行序列的形式出现,每一行均以换行符结束。因此,统计行数等价于统计换行符的个数。#include <
- 一、概念:LINQ to Entities - ADO.NET | Microsoft 官方文档EF实体框架目前版本为EF6。EF6 可实现