Mybatis3.5.1源碼分析
- Mybatis-SqlSessionFactoryBuilder,XMLConfigBuilder,XPathParser源碼解析
- Mybatis-Configuration源碼解析
- Mybatis-事務(wù)對象源碼解析
- Mybatis-數(shù)據(jù)源源碼解析
- Mybatis緩存策略源碼解析
- Mybatis-DatabaseIdProvider源碼解析
- Mybatis-TypeHandler源碼解析
- Mybatis-Reflector源碼解析
- Mybatis-ObjectFactory,ObjectWrapperFactory源碼分析
- Mybatis-Mapper各類標(biāo)簽封裝類源碼解析
- Mybatis-XMLMapperBuilder,XMLStatmentBuilder源碼分析
- Mybatis-MapperAnnotationBuilder源碼分析
- [Mybatis-MetaObject,MetaClass源碼解析]http://www.itdecent.cn/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<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
/**
*
* @param config Mybatis全局配置信息
*/
public MapperRegistry(Configuration config) {
this.config = config;
}
/**
* 獲取映射器
* @return
*/
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//獲取type對應(yīng)的映射代理器工廠
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
//如果映射代理器工廠為null
if (mapperProxyFactory == null) {
//拋出異常
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//創(chuàng)建type代理對象
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
//拋出異常
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
/**
* 判斷是否已經(jīng)進(jìn)行注冊該類
* <p>
* 從{@link #knownMappers}進(jìn)行判斷。
* </p>
* @param type Mapper.xml對應(yīng)的接口類
* @return 如果{@code type} 存在knownMappers中,返回true;否則返回false
*/
public <T> boolean hasMapper(Class<T> type) {
return knownMappers.containsKey(type);
}
/**
* 將 Mapper.xml對應(yīng)的接口類 加入到knownMappper中
* @param type Mapper.xml對應(yīng)的接口類
*/
public <T> void addMapper(Class<T> type) {
//只有接口才會進(jìn)行注冊...
if (type.isInterface()) {
//如果在knownMappers已經(jīng)找到了對應(yīng)的type,將拋出異常
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
//已加載完成標(biāo)記
boolean loadCompleted = false;
try {
//將type和對應(yīng)的映射代理器工廠添加到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在解析器運(yùn)行之前添加到knownMappers這個很重要。
* 否則,映射分析器可能會自動嘗試綁定。如果type的已經(jīng)添加到knownMappers后,它就不會嘗試。
*/
//新建一個 映射器注解構(gòu)建器
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
/**
* 解析映射接口類,解析mapper標(biāo)簽下的所有方法和注解,并對解析出來的信息加以封裝,
* 然后添加到Mybatis全局配置信息中。然后重新解析Mybatis全局配置信息中未能完成解析的
* Method重新解析
*/
parser.parse();
//設(shè)置已加載完成標(biāo)記為true
loadCompleted = true;
} finally {
//如果未能完成加載的接口映射類
if (!loadCompleted) {
//從knownMappers中移除接口映射類
knownMappers.remove(type);
}
}
}
}
/**
* 獲取已注冊的映射接口類集合
* @since 3.2.2
*/
public Collection<Class<?>> getMappers() {
//返回不能修改的Set
return Collections.unmodifiableCollection(knownMappers.keySet());
}
/**
* 添加映射器
* <p>
* 從{@link @packageName}中獲取{@link @superType}的子類,并將這些子類傳入{@link #addMapper(Class)}進(jìn)行注冊
* </p>
* @since 3.2.2
*/
public void addMappers(String packageName, Class<?> superType) {
//ResolverUtil:用于查找在類路徑可用并滿足任意條件的類。
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
//查找在packageName下的superType的子類
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
//獲取匹配的類集合
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
//遍歷映射類集合
for (Class<?> mapperClass : mapperSet) {
//將 映射接口類 加入到knownMappper中
addMapper(mapperClass);
}
}
/**
* 添加映射器
* <p>
* 將{@link #Object}傳入{@link #addMappers(String, Class)},傳入{@link #Object}也就意味著
* 獲取包中所有的類。
* </p>
* @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<T> {
/**
* 映射器接口類
*/
private final Class<T> mapperInterface;
/**
* 方法緩存Map,映射接口類方法對象-映射方法類對象
*/
private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>();
/**
*
* @param mapperInterface 映射接口類
*/
public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}
/**
* 獲取映射接口類
* @return {@link #mapperInterface}
*/
public Class<T> getMapperInterface() {
return mapperInterface;
}
/**
* 方法緩存Map,映射接口類方法對象-映射方法類對象
* @return {@link #mapperInterface}
*/
public Map<Method, MapperMethod> getMethodCache() {
return methodCache;
}
/**
* 創(chuàng)建mapperInterface代理對象
* @param mapperProxy 映射代理器對象
* @return mapperInterface代理對象
*/
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
/**
* 創(chuàng)建mapperInterface代理對象
* @param sqlSession 數(shù)據(jù)庫對話對象
* @return
*/
public T newInstance(SqlSession sqlSession) {
//新建mapperInterface的 映射代理器對象
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
//創(chuàng)建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<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
/**
* 數(shù)據(jù)庫對話對象
*/
private final SqlSession sqlSession;
/**
* 映射器接口類
*/
private final Class<T> mapperInterface;
/**
* 方法緩存Map,映射接口類方法對象-映射方法類對象
*/
private final Map<Method, MapperMethod> methodCache;
/**
*
* @param sqlSession 數(shù)據(jù)庫對話對象
* @param mapperInterface 映射器接口類
* @param methodCache 方法緩存Map,映射接口類方法對象-映射方法類對象
*/
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
/**
* 代理方法回調(diào)
* @param proxy 代理后的實(shí)例對象
* @param method 對象被調(diào)用方法
* @param args 調(diào)用時的參數(shù)
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
//Method.getDeclaringClass:返回聲明的該方法的類
//如果聲明method的類是Object
if (Object.class.equals(method.getDeclaringClass())) {
//直接執(zhí)行
return method.invoke(this, args);
//Method.isDefault:如果此方法是默認(rèn)方法,則返回true ; 返回false其他 默認(rèn)方法是公共非抽象實(shí)例方法,即具有主體的非靜態(tài)方法,以接口類型聲明
//如果method是默認(rèn)方法
} else if (method.isDefault()) {
//執(zhí)行默認(rèn)方法
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
//當(dāng)(參數(shù)wrapped)屬于InvocationTargetException和UndeclaredThrowableException的類型時,會從中 獲取更加精確的異常拋出
throw ExceptionUtil.unwrapThrowable(t);
}
//從緩存Map中獲取MapperMethod對象
final MapperMethod mapperMethod = cachedMapperMethod(method);
//執(zhí)行SQL腳本,得到結(jié)果對象,然后轉(zhuǎn)換成對應(yīng)method返回類型的對象
return mapperMethod.execute(sqlSession, args);
}
/**
* /從緩存Map中獲取method對應(yīng)的MapperMethod對象
* @param method 方法對象
* @return method對應(yīng)的MapperMethod對象
*/
private MapperMethod cachedMapperMethod(Method method) {
//從緩存Map中獲取method對應(yīng)的MapperMethod對象,如果沒有,就新建一個MapperMethod對象
return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
}
/**
* 執(zhí)行默認(rèn)方法
* @param proxy 代理后的實(shí)例對象
* @param method 對象被調(diào)用方法
* @param args 調(diào)用時的參數(shù)
*/
private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
throws Throwable {
//獲取MethodHandles.Lookup的接收Class和int的構(gòu)造方法
final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
.getDeclaredConstructor(Class.class, int.class);
//如果構(gòu)造函數(shù)不能訪問
if (!constructor.isAccessible()) {
//設(shè)置構(gòu)造函數(shù)為可訪問
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構(gòu)建MethodHandles.Lookup實(shí)例對象
* MethodHandles.Lookup.unreflectSpecial:生成可以調(diào)用反射方法【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,封裝著方法的返回類型的各個情況描述,指定類型的參數(shù)索引位置,以及參數(shù)名解析器
//新建一個方法簽名對象
this.method = new MethodSignature(config, mapperInterface, method);
}
/**
* 執(zhí)行SQL腳本,得到結(jié)果對象,然后轉(zhuǎn)換成對應(yīng)method返回類型的對象
* @param sqlSession 數(shù)據(jù)庫會話對象
* @param args 參數(shù)對象數(shù)組
* @return
*/
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
//判斷Sql指令類型
switch (command.getType()) {
//插入指令
case INSERT: {
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//執(zhí)行插入Sql腳本,然后將更新記錄數(shù)轉(zhuǎn)換成method所需要的返回類型對象
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
//修改指令
case UPDATE: {
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//執(zhí)行修改Sql腳本,然后將更新記錄數(shù)轉(zhuǎn)換成method所需要的返回類型對象
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
//刪除指令
case DELETE: {
///構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合;
Object param = method.convertArgsToSqlCommandParam(args);
//執(zhí)行刪除Sql腳本,然后將更新記錄數(shù)轉(zhuǎn)換成method所需要的返回類型對象
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
//查詢指令
case SELECT:
//如果method返回類型為void 而且 method有ResultHandler類型參數(shù)
if (method.returnsVoid() && method.hasResultHandler()) {
//用ResultHandler執(zhí)行查詢sql
executeWithResultHandler(sqlSession, args);
//結(jié)果對象賦值為null
result = null;
//如果method返回類型為Collection的子類或者返回類型是數(shù)組
} else if (method.returnsMany()) {
/**
* 執(zhí)行查詢SQL,默認(rèn)返回結(jié)果對象集合,但如果method返回類型為數(shù)組,該方法就會自動將結(jié)果對象集合
* 轉(zhuǎn)換成數(shù)組;如果method返回類型為自定義的Collection實(shí)現(xiàn)類,該方法也會將結(jié)果對象集合的元素添加到
* 自定義的Collection實(shí)現(xiàn)類對象中
*/
result = executeForMany(sqlSession, args);
//如果method的返回類型是Map
} else if (method.returnsMap()) {
//執(zhí)行查詢SQL,返回結(jié)果對象映射賦值給result
result = executeForMap(sqlSession, args);
//如果method返回類型是Cursor
} else if (method.returnsCursor()) {
//執(zhí)行查詢SQL,返回結(jié)果對象游標(biāo)賦值給result
result = executeForCursor(sqlSession, args);
} else {
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//執(zhí)行查詢SQL,返回結(jié)果對象賦值給result
result = sqlSession.selectOne(command.getName(), param);
//如果method返回類型為Optional 且 結(jié)果對象為null 或者 method返回類型不等于結(jié)果對象類型
if (method.returnsOptional()
&& (result == null || !method.getReturnType().equals(result.getClass()))) {
//如果對象即可能是 null 也可能是非 null,使用 ofNullable() 方法可以防止空指針異常
result = Optional.ofNullable(result);
}
}
break;
//刷新指令
case FLUSH:
//執(zhí)行全部等待批處理語句,并將結(jié)果封裝到BatchResult集合中,然后賦值reuslt
result = sqlSession.flushStatements();
break;
//其他指令
default:
//拋出異常
throw new BindingException("Unknown execution method for: " + command.getName());
}
//如果結(jié)果對象為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} 轉(zhuǎn)換成method所需要的返回類型對象
* @param rowCount 更新記錄數(shù)
* @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())) {
//強(qiáng)轉(zhuǎn)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,并將判斷結(jié)果賦值到result
result = rowCount > 0;
//如果上面所支持的類型都不匹配
} else {
//拋出異常
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
}
return result;
}
/**
* 用ResultHandler執(zhí)行查詢sql
* @param sqlSession 數(shù)據(jù)庫會話對象
* @param args 參數(shù)對象
*/
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
//獲取command.name對應(yīng)的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.");
}
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定義RowBound類型參數(shù)
if (method.hasRowBounds()) {
//獲取RowBounds對象
RowBounds rowBounds = method.extractRowBounds(args);
//獲取ResultHandler對象,然后執(zhí)行查詢SQL腳本,并將結(jié)果對象賦值到ResultHandlerd對象中
sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
} else {
//獲取ResultHandler對象,然后執(zhí)行查詢SQL腳本,并將結(jié)果對象賦值到ResultHandlerd對象中
sqlSession.select(command.getName(), param, method.extractResultHandler(args));
}
}
/**
* 執(zhí)行查詢SQL,默認(rèn)返回結(jié)果對象集合,但如果method返回類型為數(shù)組,該方法就會自動將結(jié)果對象集合
* 轉(zhuǎn)換成數(shù)組;如果method返回類型為自定義的Collection實(shí)現(xiàn)類,該方法也會將結(jié)果對象集合的元素添加到
* 自定義的Collection實(shí)現(xiàn)類對象中
* @param sqlSession 數(shù)據(jù)庫會話對象
* @param args 參數(shù)對象數(shù)組
* @param <E> 結(jié)果對象類型
* @return 結(jié)果對象集合
*/
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
//定義一個結(jié)果對象集合
List<E> result;
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定義RowBound類型參數(shù)
if (method.hasRowBounds()) {
//獲取RowBounds對象
RowBounds rowBounds = method.extractRowBounds(args);
//傳入rowBound對象,執(zhí)行查詢SQL,返回結(jié)果對象集合賦值給result
result = sqlSession.selectList(command.getName(), param, rowBounds);
} else {
//執(zhí)行查詢SQL,返回結(jié)果對象集合賦值給result
result = sqlSession.selectList(command.getName(), param);
}
// issue #510 Collections & arrays support
//如果method的返回類型不是result的父類
if (!method.getReturnType().isAssignableFrom(result.getClass())) {
//如果method的返回類型是數(shù)組
if (method.getReturnType().isArray()) {
//將集合轉(zhuǎn)換成數(shù)組,然后返回出去
return convertToArray(result);
} else {
//將 list 轉(zhuǎn)換成 method返回類型,method返回類型必須是Collection的子類,否則會拋出異常。然后返回出去
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
}
//直接返回結(jié)果對象集合
return result;
}
/**
* 執(zhí)行查詢SQL,返回結(jié)果對象游標(biāo)
* @param sqlSession 數(shù)據(jù)庫會話
* @param args 參數(shù)對象數(shù)組
* @param <T> 結(jié)果對象類型
* @return 結(jié)果對象游標(biāo)
*/
private <T> Cursor<T> executeForCursor(SqlSession sqlSession, Object[] args) {
//定義一個結(jié)果對象游標(biāo)
Cursor<T> result;
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定義RowBound類型參數(shù)
if (method.hasRowBounds()) {
//獲取RowBounds對象
RowBounds rowBounds = method.extractRowBounds(args);
//傳入rowBound對象,執(zhí)行查詢SQL,返回結(jié)果對象游標(biāo)賦值給result
result = sqlSession.selectCursor(command.getName(), param, rowBounds);
} else {
//執(zhí)行查詢SQL,返回結(jié)果對象游標(biāo)賦值給result
result = sqlSession.selectCursor(command.getName(), param);
}
return result;
}
/**
* 將 {@code list} 轉(zhuǎn)換成 method返回類型,method返回類型必須是Collection的子類,否則
* 會拋出異常。
* @param config Mybatis全局配置項(xiàng)
* @param list 結(jié)果對象集合
* @param <E> 結(jié)果對象類型
* @return method返回類型對象
*/
private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
//使用對象工廠創(chuàng)建method返回類型的實(shí)例對象
Object collection = config.getObjectFactory().create(method.getReturnType());
//新建一個collection的元對象
MetaObject metaObject = config.newMetaObject(collection);
/**
* 將list的元素添加到collection里,addAll只有CollectionWrapper有實(shí)現(xiàn),也只有
* method的返回類型是繼承Collection才會引用CcllectionWrapper,而其他ObjectWrapper調(diào)用allAll都會拋出異常
*/
metaObject.addAll(list);
return collection;
}
/**
* 將集合轉(zhuǎn)換成數(shù)組。
* @param list 結(jié)果對象集合
* @param <E> 結(jié)果對象類型
* @return 數(shù)組
*/
@SuppressWarnings("unchecked")
private <E> Object convertToArray(List<E> list) {
//調(diào)用該方法的時候,method的返回類型已經(jīng)確定是數(shù)組類型
//獲取method返回類型數(shù)組的元素類型
Class<?> arrayComponentType = method.getReturnType().getComponentType();
//構(gòu)建一個元素類型為arrayComponentType,長度為list.size()的數(shù)組對象
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 {
//調(diào)用List.toArray方法進(jìn)行轉(zhuǎn)換成數(shù)組
return list.toArray((E[])array);
}
}
/**
* 執(zhí)行查詢SQL,返回結(jié)果對象映射。
* @param sqlSession 數(shù)據(jù)庫會話對象
* @param args 參數(shù)對象數(shù)組
* @param <K> method的MapKey注解定義的屬性對應(yīng)的值
* @param <V> 結(jié)果對象
* @return 結(jié)果對象映射
*/
private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
//定義一個Map類型的結(jié)果對象映射
Map<K, V> result;
//構(gòu)建sql腳本參數(shù)名-參數(shù)對象映射集合
Object param = method.convertArgsToSqlCommandParam(args);
//如果method有定義RowBound類型參數(shù)
if (method.hasRowBounds()) {
//獲取RowBounds對象
RowBounds rowBounds = method.extractRowBounds(args);
//傳入rowBound對象以及method里的MapKey注解定義的值,執(zhí)行查詢SQL,返回結(jié)果對象映射賦值給result
result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
//傳入method里的MapKey注解定義的值,執(zhí)行查詢SQL,返回結(jié)果對象映射賦值給result
result = sqlSession.selectMap(command.getName(), param, method.getMapKey());
}
return result;
}
/**
* 參數(shù)Map
* <p>
* HashMap的子類,對獲取集合沒有元素時會拋出異常。
* </p>
* @param <V> Object
*/
public static class ParamMap<V> extends HashMap<String, V> {
private static final long serialVersionUID = -2212268410512043556L;
@Override
public V get(Object key) {
//沒有找到相應(yīng)的key會拋出 BindingException
if (!super.containsKey(key)) {
throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + keySet());
}
return super.get(key);
}
}
/**
* SQL命令
* <p>
* 通過mapperInterface+ method 找到的 MapperStatement對象,保存著MapperStatement對象的Id和SqlCommandType
* </p>
*/
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 對應(yīng)的 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命令類型設(shè)置成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} 對應(yīng)的 MappedStatement對象
* @param mapperInterface 映射器接口類
* @param methodName 方法名
* @param declaringClass 聲明method的類
* @param configuration Mybatis全局配置信息
* @return {@code mapperInterface} + {@code methodName} 對應(yīng)的 MappedStatement對象
*/
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
//拼裝statementId,取映射器接口包名+類名+'.'+方法名
String statementId = mapperInterface.getName() + "." + methodName;
//如果存在statementId對應(yīng)的MappedStatement對象
if (configuration.hasStatement(statementId)) {
//取出對應(yīng)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 對應(yīng)的 MappedStatement對象
MappedStatement ms = resolveMappedStatement(superInterface, methodName,
declaringClass, configuration);
//如果ms不為null
if (ms != null) {
//返回ms
return ms;
}
}
}
//連從繼承的接口中去找都找不到時,就返回null
return null;
}
}
/**
* 方法簽名,封裝著方法的返回類型的各個情況描述,指定類型的參數(shù)索引位置,以及參數(shù)名解析器
*/
public static class MethodSignature {
/**
* 如果返回類型為Collection的子類或者返回類型是數(shù)組,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;
/**
* 只有當(dāng)method的返回類型為Map,且有加上MapKey注解,就返回MapKey注解所定義的值;否則返回null
*/
private final String mapKey;
/**
* resultHandler類型參數(shù)在方法中索引位置
*/
private final Integer resultHandlerIndex;
/**
* rowBounds類型參數(shù)在方法中索引位置
*/
private final Integer rowBoundsIndex;
/**
* 參數(shù)名解析器: 參數(shù)名稱解析器,用于構(gòu)建sql腳本參數(shù)名+參數(shù)對象映射集合
*/
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實(shí)例
if (resolvedReturnType instanceof Class<?>) {
//將返回類型強(qiáng)轉(zhuǎn)為Class類型,賦值給returnType
this.returnType = (Class<?>) resolvedReturnType;
//如果返回類型為參數(shù)化類型,參數(shù)化類型:https://blog.csdn.net/JustBeauty/article/details/81116144
} else if (resolvedReturnType instanceof ParameterizedType) {
//將返回類型強(qiáng)轉(zhuǎn)為ParameterizedType類型,然后取出聲明的類型,如List<T>,getRawType得到的就是List,然后強(qiáng)轉(zhuǎn)聲明類型為Class類型,賦值給returnType
this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
} else {
//取出方法的返回類型
this.returnType = method.getReturnType();
}
//如果返回類型為Void,returnsVoid為true
this.returnsVoid = void.class.equals(this.returnType);
//如果返回類型為Collection的子類或者返回類型是數(shù)組,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);
//只有當(dāng)method的返回類型為Map,且有加上MapKey注解,就返回MapKey注解所定義的值;否則返回null
this.mapKey = getMapKey(method);
//如果mapKey不為null,returnMap為true
this.returnsMap = this.mapKey != null;
//獲取RowBounds.class 在 method 的參數(shù)類型數(shù)組中的唯一位置,賦值給rowBoundsIndex
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
//獲取ResultHandler.class 在 method 的參數(shù)類型數(shù)組中的唯一位置,賦值給rowBoundsIndex
this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
//新建一個參數(shù)名解析器: 參數(shù)名稱解析器,用于構(gòu)建sql腳本參數(shù)名+參數(shù)對象映射集合
this.paramNameResolver = new ParamNameResolver(configuration, method);
}
/**
* 將參數(shù)對象轉(zhuǎn)換為sql腳本參數(shù),返回sql腳本參數(shù)名-參數(shù)對象映射集合;
* @param args 參數(shù)對象數(shù)值
* @return sql腳本參數(shù)名-參數(shù)對象映射集合
*/
public Object convertArgsToSqlCommandParam(Object[] args) {
return paramNameResolver.getNamedParams(args);
}
/**
* 是否存在RowRounds類型參數(shù)
* @return 存在返回true;否則返回false
*/
public boolean hasRowBounds() {
//如果rowBounds類型參數(shù)在方法中索引位置不為null
return rowBoundsIndex != null;
}
/**
* 提取RowBounds對象
* @param args 參數(shù)對象數(shù)組
* @return RowBounds對象
*/
public RowBounds extractRowBounds(Object[] args) {
//如果存在RowRounds類型參數(shù),返回rowBoundsIndex的參數(shù)對象;否則返回null
return hasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null;
}
/**
* 是否存在ResultHandler類型參數(shù)
* @return 存在返回true;否則返回false
*/
public boolean hasResultHandler() {
//如果ResultHandler類型參數(shù)在方法中索引位置不為null
return resultHandlerIndex != null;
}
/**
* 提取ResultHandler對象
* @param args 參數(shù)對象數(shù)組
* @return ResultHandler對象
*/
public ResultHandler extractResultHandler(Object[] args) {
//如果存在ResultHandler類型參數(shù),返回resultHandlerIndex的參數(shù)對象;否則返回null
return hasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null;
}
/**
* 只有當(dāng)method的返回類型為Map,且有加上MapKey注解,就返回MapKey注解所定義的值;否則返回null
*/
public String getMapKey() {
return mapKey;
}
/**
* 方法的返回類型
*/
public Class<?> getReturnType() {
return returnType;
}
/**
* 如果返回類型為Collection的子類或者返回類型是數(shù)組,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<br/>
* 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} 的參數(shù)類型數(shù)組中的唯一位置
* @param method 方法對象
* @param paramType 參數(shù)類型
* @return {@code paramType} 在 {@code method} 的參數(shù)類型數(shù)組中的唯一位置,如果出現(xiàn)多個paramType的位置,會拋出異常
*/
private Integer getUniqueParamIndex(Method method, Class<?> paramType) {
Integer index = null;
//獲取method的參數(shù)類型數(shù)組
final Class<?>[] argTypes = method.getParameterTypes();
//遍歷參數(shù)類型數(shù)組
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 {
//如果出現(xiàn)多個paramType的位置,拋出異常
throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");
}
}
}
return index;
}
/**
* 獲取MapKey
* @param method 方法對象
* @return 只有當(dāng)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();
}
}
//只要當(dāng)method的返回類型為Map,且有加上MapKey注解,mapKey才不為null
return mapKey;
}
}
}