Mybatis3.5.1源码分析
- Mybatis-SqlSessionFactoryBuilder,XMLConfigBuilder,XPathParser源码解析
- Mybatis-Configuration源码解析
- Mybatis-事务对象源码解析
- Mybatis-数据源源码解析
- Mybatis缓存策略源码解析
- Mybatis-DatabaseIdProvider源码解析
- Mybatis-TypeHandler源码解析
- Mybatis-Reflector源码解析
- Mybatis-ObjectFactory,ObjectWrapperFactory源码分析
- Mybatis-Mapper各类标签封装类源码解析
- Mybatis-XMLMapperBuilder,XMLStatmentBuilder源码分析
- Mybatis-MapperAnnotationBuilder源码分析
- [Mybatis-MetaObject,MetaClass源码解析]https://www.jianshu.com/p/f51fa552f30a)
- Mybatis-LanguageDriver源码解析
- Mybatis-SqlSource源码解析
- Mybatis-SqlNode源码解析
- Mybatis-KeyGenerator源码解析
- Mybatis-Executor源码解析
- Mybatis-ParameterHandler源码解析
- Mybatis-StatementHandler源码解析
- Mybatis-DefaultResultSetHandler(一)源码解析
- Mybatis-DefaultResultSetHandler(二)源码解析
- Mybatis-ResultHandler,Cursor,RowBounds 源码分析
- Mybatis-MapperProxy源码解析
- Mybatis-SqlSession源码解析
- Mybatis-Interceptor源码解析
MapperRegistry
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
/**
* 映射器注册器
* @author Clinton Begin
* @author Eduardo Macarron
* @author Lasse Voss
*/
public class MapperRegistry {
/**
* Mybatis全局配置信息
*/
private final Configuration config;
/**
* 已注册的映射器接口类映射,key=>映射器接口类,value=>映射代理器工厂
*/
private final Map, MapperProxyFactory>> knownMappers = new HashMap<>();
/**
*
* @param config Mybatis全局配置信息
*/
public MapperRegistry(Configuration config) {
this.config = config;
}
/**
* 获取映射器
* @return
*/
@SuppressWarnings("unchecked")
public T getMapper(Class type, SqlSession sqlSession) {
//获取type对应的映射代理器工厂
final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) knownMappers.get(type);
//如果映射代理器工厂为null
if (mapperProxyFactory == null) {
//抛出异常
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//创建type代理对象
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
//抛出异常
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
/**
* 判断是否已经进行注册该类
*
* 从{@link #knownMappers}进行判断。
*
* @param type Mapper.xml对应的接口类
* @return 如果{@code type} 存在knownMappers中,返回true;否则返回false
*/
public boolean hasMapper(Class type) {
return knownMappers.containsKey(type);
}
/**
* 将 Mapper.xml对应的接口类 加入到knownMappper中
* @param type Mapper.xml对应的接口类
*/
public void addMapper(Class type) {
//只有接口才会进行注册...
if (type.isInterface()) {
//如果在knownMappers已经找到了对应的type,将抛出异常
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
//已加载完成标记
boolean loadCompleted = false;
try {
//将type和对应的映射代理器工厂添加到knowMappers中
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
/**
* 译文:
* 让type在解析器运行之前添加到knownMappers这个很重要。
* 否则,映射分析器可能会自动尝试绑定。如果type的已经添加到knownMappers后,它就不会尝试。
*/
//新建一个 映射器注解构建器
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
/**
* 解析映射接口类,解析mapper标签下的所有方法和注解,并对解析出来的信息加以封装,
* 然后添加到Mybatis全局配置信息中。然后重新解析Mybatis全局配置信息中未能完成解析的
* Method重新解析
*/
parser.parse();
//设置已加载完成标记为true
loadCompleted = true;
} finally {
//如果未能完成加载的接口映射类
if (!loadCompleted) {
//从knownMappers中移除接口映射类
knownMappers.remove(type);
}
}
}
}
/**
* 获取已注册的映射接口类集合
* @since 3.2.2
*/
public Collection> getMappers() {
//返回不能修改的Set
return Collections.unmodifiableCollection(knownMappers.keySet());
}
/**
* 添加映射器
*
* 从{@link @packageName}中获取{@link @superType}的子类,并将这些子类传入{@link #addMapper(Class)}进行注册
*
* @since 3.2.2
*/
public void addMappers(String packageName, Class> superType) {
//ResolverUtil:用于查找在类路径可用并满足任意条件的类。
ResolverUtil> resolverUtil = new ResolverUtil<>();
//查找在packageName下的superType的子类
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
//获取匹配的类集合
Set>> mapperSet = resolverUtil.getClasses();
//遍历映射类集合
for (Class> mapperClass : mapperSet) {
//将 映射接口类 加入到knownMappper中
addMapper(mapperClass);
}
}
/**
* 添加映射器
*
* 将{@link #Object}传入{@link #addMappers(String, Class)},传入{@link #Object}也就意味着
* 获取包中所有的类。
*
* @since 3.2.2
*/
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
}
}
MapperProxy
/**
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.ibatis.session.SqlSession;
/**
* 映射代理器工厂类
* @author Lasse Voss
*/
public class MapperProxyFactory {
/**
* 映射器接口类
*/
private final Class mapperInterface;
/**
* 方法缓存Map,映射接口类方法对象-映射方法类对象
*/
private final Map methodCache = new ConcurrentHashMap<>();
/**
*
* @param mapperInterface 映射接口类
*/
public MapperProxyFactory(Class mapperInterface) {
this.mapperInterface = mapperInterface;
}
/**
* 获取映射接口类
* @return {@link #mapperInterface}
*/
public Class getMapperInterface() {
return mapperInterface;
}
/**
* 方法缓存Map,映射接口类方法对象-映射方法类对象
* @return {@link #mapperInterface}
*/
public Map getMethodCache() {
return methodCache;
}
/**
* 创建mapperInterface代理对象
* @param mapperProxy 映射代理器对象
* @return mapperInterface代理对象
*/
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
/**
* 创建mapperInterface代理对象
* @param sqlSession 数据库对话对象
* @return
*/
public T newInstance(SqlSession sqlSession) {
//新建mapperInterface的 映射代理器对象
final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
//创建mapperInterface代理对象
return newInstance(mapperProxy);
}
}
MapperProxy
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Map;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.SqlSession;
/**
* 映射器代理
* @author Clinton Begin
* @author Eduardo Macarron
*/
public class MapperProxy implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
/**
* 数据库对话对象
*/
private final SqlSession sqlSession;
/**
* 映射器接口类
*/
private final Class mapperInterface;
/**
* 方法缓存Map,映射接口类方法对象-映射方法类对象
*/
private final Map methodCache;
/**
*
* @param sqlSession 数据库对话对象
* @param mapperInterface 映射器接口类
* @param methodCache 方法缓存Map,映射接口类方法对象-映射方法类对象
*/
public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
/**
* 代理方法回调
* @param proxy 代理后的实例对象
* @param method 对象被调用方法
* @param args 调用时的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
//Method.getDeclaringClass:返回声明的该方法的类
//如果声明method的类是Object
if (Object.class.equals(method.getDeclaringClass())) {
//直接执行
return method.invoke(this, args);
//Method.isDefault:如果此方法是默认方法,则返回true ; 返回false其他 默认方法是公共非抽象实例方法,即具有主体的非静态方法,以接口类型声明
//如果method是默认方法
} else if (method.isDefault()) {
//执行默认方法
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
//当(参数wrapped)属于InvocationTargetException和UndeclaredThrowableException的类型时,会从中 获取更加精确的异常抛出
throw ExceptionUtil.unwrapThrowable(t);
}
//从缓存Map中获取MapperMethod对象
final MapperMethod mapperMethod = cachedMapperMethod(method);
//执行SQL脚本,得到结果对象,然后转换成对应method返回类型的对象
return mapperMethod.execute(sqlSession, args);
}
/**
* /从缓存Map中获取method对应的MapperMethod对象
* @param method 方法对象
* @return method对应的MapperMethod对象
*/
private MapperMethod cachedMapperMethod(Method method) {
//从缓存Map中获取method对应的MapperMethod对象,如果没有,就新建一个MapperMethod对象
return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
}
/**
* 执行默认方法
* @param proxy 代理后的实例对象
* @param method 对象被调用方法
* @param args 调用时的参数
*/
private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
throws Throwable {
//获取MethodHandles.Lookup的接收Class和int的构造方法
final Constructor constructor = MethodHandles.Lookup.class
.getDeclaredConstructor(Class.class, int.class);
//如果构造函数不能访问
if (!constructor.isAccessible()) {
//设置构造函数为可访问
constructor.setAccessible(true);
}
//获取声明method的类
final Class> declaringClass = method.getDeclaringClass();
/**
* constructor.newInstance(declaringClass,
* MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
* | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
* :通过constructor构建MethodHandles.Lookup实例对象
* MethodHandles.Lookup.unreflectSpecial:生成可以调用反射方法【declaringClass的method】的方法处理器
* MethodHandle.bindTo(proxy):绑定需要放射方法的对象
* MethodHandle.invokeWithArguments:传入args,反射方法
*/
return constructor
.newInstance(declaringClass,
MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
| MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
.unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
}
}
MapperMethod
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.ibatis.annotations.Flush;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ParamNameResolver;
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
/**
* 映射方法
* @author Clinton Begin
* @author Eduardo Macarron
* @author Lasse Voss
* @author Kazuki Shimizu
*/
public class MapperMethod {
/**
*
*/
private final SqlCommand command;
private final MethodSignature method;
/**
*
* @param mapperInterface 映射器接口类
* @param method 方法对象
* @param config Mybatis全局配置信息
*/
public MapperMethod(Class> mapperInterface, Method method, Configuration config) {
//SqlCommand:通过mapperInterface+ method 找到的 MapperStatement对象,保存着MapperStatement对象的Id和SqlCommandType
//新建一个SqlCommand对象
this.command = new SqlCommand(config, mapperInterface, method);
// MethodSignature,封装着方法的返回类型的各个情况描述,指定类型的参数索引位置,以及参数名解析器
//新建一个方法签名对象
this.method = new MethodSignature(config, mapperInterface, method);
}
/**
* 执行SQL脚本,得到结果对象,然后转换成对应method返回类型的对象
* @param sqlSession 数据库会话对象
* @param args 参数对象数组
* @return
*/
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
//判断Sql指令类型
switch (command.getType()) {
//插入指令
case INSERT: {
//构建sql脚本参数名-参数对象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//执行插入Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
//修改指令
case UPDATE: {
//构建sql脚本参数名-参数对象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//执行修改Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
//删除指令
case DELETE: {
///构建sql脚本参数名-参数对象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//执行删除Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
//查询指令
case SELECT:
//如果method返回类型为void 而且 method有ResultHandler类型参数
if (method.returnsVoid() && method.hasResultHandler()) {
//用ResultHandler执行查询sql
executeWithResultHandler(sqlSession, args);
//结果对象赋值为null
result = null;
//如果method返回类型为Collection的子类或者返回类型是数组
} else if (method.returnsMany()) {
/**
* 执行查询SQL,默认返回结果对象集合,但如果method返回类型为数组,该方法就会自动将结果对象集合
* 转换成数组;如果method返回类型为自定义的Collection实现类,该方法也会将结果对象集合的元素添加到
* 自定义的Collection实现类对象中
*/
result = executeForMany(sqlSession, args);
//如果method的返回类型是Map
} else if (method.returnsMap()) {
//执行查询SQL,返回结果对象映射赋值给result
result = executeForMap(sqlSession, args);
//如果method返回类型是Cursor
} else if (method.returnsCursor()) {
//执行查询SQL,返回结果对象游标赋值给result
result = executeForCursor(sqlSession, args);
} else {
//构建sql脚本参数名-参数对象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//执行查询SQL,返回结果对象赋值给result
result = sqlSession.selectOne(command.getName(), param);
//如果method返回类型为Optional 且 结果对象为null 或者 method返回类型不等于结果对象类型
if (method.returnsOptional()
&& (result == null || !method.getReturnType().equals(result.getClass()))) {
//如果对象即可能是 null 也可能是非 null,使用 ofNullable() 方法可以防止空指针异常
result = Optional.ofNullable(result);
}
}
break;
//刷新指令
case FLUSH:
//执行全部等待批处理语句,并将结果封装到BatchResult集合中,然后赋值reuslt
result = sqlSession.flushStatements();
break;
//其他指令
default:
//抛出异常
throw new BindingException("Unknown execution method for: " + command.getName());
}
//如果结果对象为null 且 method的返回类型为原始类型 且 method的返回类型不是void
if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
//抛出异常
throw new BindingException("Mapper method '" + command.getName()
+ " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}
/**
* 将{@code rowCount} 转换成method所需要的返回类型对象
* @param rowCount 更新记录数
* @return method所需要的返回类型对象
*/
private Object rowCountResult(int rowCount) {
final Object result;
//如果method返回类型为Void
if (method.returnsVoid()) {
//直接返回null
result = null;
//如果method返回类型为Integer类型,或者method返回类型为int类型
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
//将rowCount赋值给result
result = rowCount;
//如果method返回类型为Long类型,或者method返回类型为long类型
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
//强转rowCount为long,将rowCount赋值给result
result = (long)rowCount;
//如果method返回类型为Boolean类型,或者method返回类型为boolean类型
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
//判断rowCount是否大于0,并将判断结果赋值到result
result = rowCount > 0;
//如果上面所支持的类型都不匹配
} else {
//抛出异常
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
}
return result;
}
/**
* 用ResultHandler执行查询sql
* @param sqlSession 数据库会话对象
* @param args 参数对象
*/
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
//获取command.name对应的MappedStatement对象
MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
//如果ms的StatementType不是存储过程 且 ms的返回映射的第一个类型是void
if (!StatementType.CALLABLE.equals(ms.getStatementType())
&& void.class.equals(ms.getResultMaps().get(0).getType())) {
//抛出异常
throw new BindingException("method " + command.getName()
+ " needs either a @ResultMap annotation, a @ResultType annotation,"
+ " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
}
//构建sql脚本参数名-参数对象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定义RowBound类型参数
if (method.hasRowBounds()) {
//获取RowBounds对象
RowBounds rowBounds = method.extractRowBounds(args);
//获取ResultHandler对象,然后执行查询SQL脚本,并将结果对象赋值到ResultHandlerd对象中
sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
} else {
//获取ResultHandler对象,然后执行查询SQL脚本,并将结果对象赋值到ResultHandlerd对象中
sqlSession.select(command.getName(), param, method.extractResultHandler(args));
}
}
/**
* 执行查询SQL,默认返回结果对象集合,但如果method返回类型为数组,该方法就会自动将结果对象集合
* 转换成数组;如果method返回类型为自定义的Collection实现类,该方法也会将结果对象集合的元素添加到
* 自定义的Collection实现类对象中
* @param sqlSession 数据库会话对象
* @param args 参数对象数组
* @param 结果对象类型
* @return 结果对象集合
*/
private Object executeForMany(SqlSession sqlSession, Object[] args) {
//定义一个结果对象集合
List result;
//构建sql脚本参数名-参数对象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定义RowBound类型参数
if (method.hasRowBounds()) {
//获取RowBounds对象
RowBounds rowBounds = method.extractRowBounds(args);
//传入rowBound对象,执行查询SQL,返回结果对象集合赋值给result
result = sqlSession.selectList(command.getName(), param, rowBounds);
} else {
//执行查询SQL,返回结果对象集合赋值给result
result = sqlSession.selectList(command.getName(), param);
}
// issue #510 Collections & arrays support
//如果method的返回类型不是result的父类
if (!method.getReturnType().isAssignableFrom(result.getClass())) {
//如果method的返回类型是数组
if (method.getReturnType().isArray()) {
//将集合转换成数组,然后返回出去
return convertToArray(result);
} else {
//将 list 转换成 method返回类型,method返回类型必须是Collection的子类,否则会抛出异常。然后返回出去
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
}
//直接返回结果对象集合
return result;
}
/**
* 执行查询SQL,返回结果对象游标
* @param sqlSession 数据库会话
* @param args 参数对象数组
* @param 结果对象类型
* @return 结果对象游标
*/
private Cursor executeForCursor(SqlSession sqlSession, Object[] args) {
//定义一个结果对象游标
Cursor result;
//构建sql脚本参数名-参数对象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定义RowBound类型参数
if (method.hasRowBounds()) {
//获取RowBounds对象
RowBounds rowBounds = method.extractRowBounds(args);
//传入rowBound对象,执行查询SQL,返回结果对象游标赋值给result
result = sqlSession.selectCursor(command.getName(), param, rowBounds);
} else {
//执行查询SQL,返回结果对象游标赋值给result
result = sqlSession.selectCursor(command.getName(), param);
}
return result;
}
/**
* 将 {@code list} 转换成 method返回类型,method返回类型必须是Collection的子类,否则
* 会抛出异常。
* @param config Mybatis全局配置项
* @param list 结果对象集合
* @param 结果对象类型
* @return method返回类型对象
*/
private Object convertToDeclaredCollection(Configuration config, List list) {
//使用对象工厂创建method返回类型的实例对象
Object collection = config.getObjectFactory().create(method.getReturnType());
//新建一个collection的元对象
MetaObject metaObject = config.newMetaObject(collection);
/**
* 将list的元素添加到collection里,addAll只有CollectionWrapper有实现,也只有
* method的返回类型是继承Collection才会引用CcllectionWrapper,而其他ObjectWrapper调用allAll都会抛出异常
*/
metaObject.addAll(list);
return collection;
}
/**
* 将集合转换成数组。
* @param list 结果对象集合
* @param 结果对象类型
* @return 数组
*/
@SuppressWarnings("unchecked")
private Object convertToArray(List list) {
//调用该方法的时候,method的返回类型已经确定是数组类型
//获取method返回类型数组的元素类型
Class> arrayComponentType = method.getReturnType().getComponentType();
//构建一个元素类型为arrayComponentType,长度为list.size()的数组对象
Object array = Array.newInstance(arrayComponentType, list.size());
//如果arryComponentType为原始类型(boolean、char、byte、short、int、long、float、double)
if (arrayComponentType.isPrimitive()) {
//遍历集合
for (int i = 0; i < list.size(); i++) {
//将list第i个位置的元素,赋值给array第i个位置的元素里
Array.set(array, i, list.get(i));
}
return array;
} else {
//调用List.toArray方法进行转换成数组
return list.toArray((E[])array);
}
}
/**
* 执行查询SQL,返回结果对象映射。
* @param sqlSession 数据库会话对象
* @param args 参数对象数组
* @param method的MapKey注解定义的属性对应的值
* @param 结果对象
* @return 结果对象映射
*/
private Map executeForMap(SqlSession sqlSession, Object[] args) {
//定义一个Map类型的结果对象映射
Map result;
//构建sql脚本参数名-参数对象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定义RowBound类型参数
if (method.hasRowBounds()) {
//获取RowBounds对象
RowBounds rowBounds = method.extractRowBounds(args);
//传入rowBound对象以及method里的MapKey注解定义的值,执行查询SQL,返回结果对象映射赋值给result
result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
//传入method里的MapKey注解定义的值,执行查询SQL,返回结果对象映射赋值给result
result = sqlSession.selectMap(command.getName(), param, method.getMapKey());
}
return result;
}
/**
* 参数Map
*
* HashMap的子类,对获取集合没有元素时会抛出异常。
*
* @param Object
*/
public static class ParamMap extends HashMap {
private static final long serialVersionUID = -2212268410512043556L;
@Override
public V get(Object key) {
//没有找到相应的key会抛出 BindingException
if (!super.containsKey(key)) {
throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + keySet());
}
return super.get(key);
}
}
/**
* SQL命令
*
* 通过mapperInterface+ method 找到的 MapperStatement对象,保存着MapperStatement对象的Id和SqlCommandType
*
*/
public static class SqlCommand {
/**
* 通过mapperInterface+ method 找到的 MapperStatement对象的Id
*/
private final String name;
/**
* 通过mapperInterface+ method 找到的 MapperStatement对象的SQL脚本类型
*/
private final SqlCommandType type;
/**
*
* @param configuration Mybatis全局配置信息
* @param mapperInterface 映射器接口类
* @param method 方法对象
*/
public SqlCommand(Configuration configuration, Class> mapperInterface, Method method) {
//获取方法名
final String methodName = method.getName();
//获取声明method的类
final Class> declaringClass = method.getDeclaringClass();
//获取 mapperInterface+ methodName 对应的 MappedStatement对象
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
configuration);
//如果ms对象为null
if (ms == null) {
//如果method加上了Flush注解
if (method.getAnnotation(Flush.class) != null) {
//将 通过mapperInterface+ method 找到的 MapperStatement对象的Id 置为null
name = null;
//Sql命令类型设置成FLUSH
type = SqlCommandType.FLUSH;
//如果没有加上Flush注解
} else {
//抛出异常
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
} else {
//保存 通过mapperInterface+ method 找到的 MapperStatement对象的Id
name = ms.getId();
//保存 ms的Sql命令类型
type = ms.getSqlCommandType();
//如果ms的Sql命令类型为UNKNOW
if (type == SqlCommandType.UNKNOWN) {
//抛出异常
throw new BindingException("Unknown execution method for: " + name);
}
}
}
/**
* 获取 通过mapperInterface+ method 找到的 MapperStatement对象的Id
* @return {@link #name}
*/
public String getName() {
return name;
}
/**
* 获取 通过mapperInterface+ method 找到的 MapperStatement对象的SQL脚本类型
* @return {@link #type}
*/
public SqlCommandType getType() {
return type;
}
/**
* 获取 {@code mapperInterface} + {@code methodName} 对应的 MappedStatement对象
* @param mapperInterface 映射器接口类
* @param methodName 方法名
* @param declaringClass 声明method的类
* @param configuration Mybatis全局配置信息
* @return {@code mapperInterface} + {@code methodName} 对应的 MappedStatement对象
*/
private MappedStatement resolveMappedStatement(Class> mapperInterface, String methodName,
Class> declaringClass, Configuration configuration) {
//拼装statementId,取映射器接口包名+类名+'.'+方法名
String statementId = mapperInterface.getName() + "." + methodName;
//如果存在statementId对应的MappedStatement对象
if (configuration.hasStatement(statementId)) {
//取出对应statementId的MappedStatement对象,并返回
return configuration.getMappedStatement(statementId);
//如果映射器接口类等于声明method的类
} else if (mapperInterface.equals(declaringClass)) {
//直接返回null
return null;
}
//遍历映射器接口类继承的所有接口
for (Class> superInterface : mapperInterface.getInterfaces()) {
//如果声明method的类是superInterface的父类
if (declaringClass.isAssignableFrom(superInterface)) {
//递归获取 superInterface + methodName 对应的 MappedStatement对象
MappedStatement ms = resolveMappedStatement(superInterface, methodName,
declaringClass, configuration);
//如果ms不为null
if (ms != null) {
//返回ms
return ms;
}
}
}
//连从继承的接口中去找都找不到时,就返回null
return null;
}
}
/**
* 方法签名,封装着方法的返回类型的各个情况描述,指定类型的参数索引位置,以及参数名解析器
*/
public static class MethodSignature {
/**
* 如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
*/
private final boolean returnsMany;
/**
* 如果 {@link #mapKey} 不为null,returnMap为true
*/
private final boolean returnsMap;
/**
* 如果返回类型为Void,returnsVoid为true
*/
private final boolean returnsVoid;
/**
* 如果返回类型为Cursor类型,returnCursor为true
*/
private final boolean returnsCursor;
/**
* 如果返回类型为Optional类型,returnsOptional为true
*/
private final boolean returnsOptional;
/**
* 方法的返回类型
*/
private final Class> returnType;
/**
* 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
*/
private final String mapKey;
/**
* resultHandler类型参数在方法中索引位置
*/
private final Integer resultHandlerIndex;
/**
* rowBounds类型参数在方法中索引位置
*/
private final Integer rowBoundsIndex;
/**
* 参数名解析器: 参数名称解析器,用于构建sql脚本参数名+参数对象映射集合
*/
private final ParamNameResolver paramNameResolver;
/**
*
* @param configuration Mybatis全局配置信息
* @param mapperInterface 映射器接口类
* @param method 方法对象
*/
public MethodSignature(Configuration configuration, Class> mapperInterface, Method method) {
//获取method在mapperInterface中的返回类型
Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
//如果返回类型为Class实例
if (resolvedReturnType instanceof Class>) {
//将返回类型强转为Class类型,赋值给returnType
this.returnType = (Class>) resolvedReturnType;
//如果返回类型为参数化类型,参数化类型:https://blog.csdn.net/JustBeauty/article/details/81116144
} else if (resolvedReturnType instanceof ParameterizedType) {
//将返回类型强转为ParameterizedType类型,然后取出声明的类型,如List,getRawType得到的就是List,然后强转声明类型为Class类型,赋值给returnType
this.returnType = (Class>) ((ParameterizedType) resolvedReturnType).getRawType();
} else {
//取出方法的返回类型
this.returnType = method.getReturnType();
}
//如果返回类型为Void,returnsVoid为true
this.returnsVoid = void.class.equals(this.returnType);
//如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
//如果返回类型为Cursor类型,returnCursor为true
this.returnsCursor = Cursor.class.equals(this.returnType);
//如果返回类型为Optional类型,returnsOptional为true
this.returnsOptional = Optional.class.equals(this.returnType);
//只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
this.mapKey = getMapKey(method);
//如果mapKey不为null,returnMap为true
this.returnsMap = this.mapKey != null;
//获取RowBounds.class 在 method 的参数类型数组中的唯一位置,赋值给rowBoundsIndex
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
//获取ResultHandler.class 在 method 的参数类型数组中的唯一位置,赋值给rowBoundsIndex
this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
//新建一个参数名解析器: 参数名称解析器,用于构建sql脚本参数名+参数对象映射集合
this.paramNameResolver = new ParamNameResolver(configuration, method);
}
/**
* 将参数对象转换为sql脚本参数,返回sql脚本参数名-参数对象映射集合;
* @param args 参数对象数值
* @return sql脚本参数名-参数对象映射集合
*/
public Object convertArgsToSqlCommandParam(Object[] args) {
return paramNameResolver.getNamedParams(args);
}
/**
* 是否存在RowRounds类型参数
* @return 存在返回true;否则返回false
*/
public boolean hasRowBounds() {
//如果rowBounds类型参数在方法中索引位置不为null
return rowBoundsIndex != null;
}
/**
* 提取RowBounds对象
* @param args 参数对象数组
* @return RowBounds对象
*/
public RowBounds extractRowBounds(Object[] args) {
//如果存在RowRounds类型参数,返回rowBoundsIndex的参数对象;否则返回null
return hasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null;
}
/**
* 是否存在ResultHandler类型参数
* @return 存在返回true;否则返回false
*/
public boolean hasResultHandler() {
//如果ResultHandler类型参数在方法中索引位置不为null
return resultHandlerIndex != null;
}
/**
* 提取ResultHandler对象
* @param args 参数对象数组
* @return ResultHandler对象
*/
public ResultHandler extractResultHandler(Object[] args) {
//如果存在ResultHandler类型参数,返回resultHandlerIndex的参数对象;否则返回null
return hasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null;
}
/**
* 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
*/
public String getMapKey() {
return mapKey;
}
/**
* 方法的返回类型
*/
public Class> getReturnType() {
return returnType;
}
/**
* 如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
*/
public boolean returnsMany() {
return returnsMany;
}
/**
* 如果 {@link #mapKey} 不为null,returnMap为true
*/
public boolean returnsMap() {
return returnsMap;
}
/**
*如果返回类型为Void,returnsVoid为true
*/
public boolean returnsVoid() {
return returnsVoid;
}
/**
* 如果返回类型为Cursor类型,returnCursor为true
*/
public boolean returnsCursor() {
return returnsCursor;
}
/**
* 如果返回类型为Optional类型,returnsOptional为true
* return whether return type is {@code java.util.Optional}.
* @return return {@code true}, if return type is {@code java.util.Optional}
* @since 3.5.0
*/
public boolean returnsOptional() {
return returnsOptional;
}
/**
* 获取 {@code paramType} 在 {@code method} 的参数类型数组中的唯一位置
* @param method 方法对象
* @param paramType 参数类型
* @return {@code paramType} 在 {@code method} 的参数类型数组中的唯一位置,如果出现多个paramType的位置,会抛出异常
*/
private Integer getUniqueParamIndex(Method method, Class> paramType) {
Integer index = null;
//获取method的参数类型数组
final Class>[] argTypes = method.getParameterTypes();
//遍历参数类型数组
for (int i = 0; i < argTypes.length; i++) {
//如果paramType为argType[i]的父类或者相同
if (paramType.isAssignableFrom(argTypes[i])) {
//如果位置为null
if (index == null) {
//将argType[i]的所在位置赋值给index
index = i;
} else {
//如果出现多个paramType的位置,抛出异常
throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");
}
}
}
return index;
}
/**
* 获取MapKey
* @param method 方法对象
* @return 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
*/
private String getMapKey(Method method) {
String mapKey = null;
//如果method的返回类型是Map的子类
if (Map.class.isAssignableFrom(method.getReturnType())) {
//获取method中的MapKey注解
final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);
//如果method有加上MapKey注解
if (mapKeyAnnotation != null) {
//获取MapKey注解的值
mapKey = mapKeyAnnotation.value();
}
}
//只要当method的返回类型为Map,且有加上MapKey注解,mapKey才不为null
return mapKey;
}
}
}