成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來(lái)到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁(yè)mybatis中session.getMapper方法源碼分析

mybatis中session.getMapper方法源碼分析

來(lái)源:九壹網(wǎng)

?

0開(kāi)始代碼AuthorMapper mapper = session.getMapper(AuthorMapper.class);

1 DefaultSqlSession類(lèi)

?

public <T> T getMapper(Class<T> type) {

//最后會(huì)去調(diào)用MapperRegistry.getMapper

return configuration.<T>getMapper(type, this);

}

?

2 Configuration類(lèi)

?

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

return mapperRegistry.getMapper(type, sqlSession);

}

3 MapperRegistry類(lèi)

?

//返回代理類(lèi)

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);

if (mapperProxyFactory == null) {

throw new BindingException("Type " + type + " is not known to the MapperRegistry.");

}

try {

return mapperProxyFactory.newInstance(sqlSession);

} catch (Exception e) {

throw new BindingException("Error getting mapper instance. Cause: " + e, e);

}

}

MapperProxyFactory<T> 類(lèi)

public T newInstance(SqlSession sqlSession) {

final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);

return newInstance(mapperProxy);

}

?

protected T newInstance(MapperProxy<T> mapperProxy) {

//用JDK自帶的動(dòng)態(tài)代理生成映射器

return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);

}

?

MapperProxy類(lèi)

public class MapperProxy<T> implements InvocationHandler, Serializable{

?

public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {

this.sqlSession = sqlSession;

this.mapperInterface = mapperInterface;

this.methodCache = methodCache;

}

}

?

?

?

//去緩存中找MapperMethod

private MapperMethod cachedMapperMethod(Method method) {

MapperMethod mapperMethod = methodCache.get(method);

if (mapperMethod == null) {

//找不到才去new

mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());

methodCache.put(method, mapperMethod);

}

return mapperMethod;

}

?

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//代理以后,所有Mapper的方法調(diào)用時(shí),都會(huì)調(diào)用這個(gè)invoke方法

//并不是任何一個(gè)方法都需要執(zhí)行調(diào)用代理對(duì)象進(jìn)行執(zhí)行,如果這個(gè)方法是Object中通用的方法(toString、hashCode等)無(wú)需執(zhí)行

if (Object.class.equals(method.getDeclaringClass())) {

try {

return method.invoke(this, args);

} catch (Throwable t) {

throw ExceptionUtil.unwrapThrowable(t);

}

}

//這里優(yōu)化了,去緩存中找MapperMethod

final MapperMethod mapperMethod = cachedMapperMethod(method);

//執(zhí)行

return mapperMethod.execute(sqlSession, args);

}

MapperMethod類(lèi)

?

?

//執(zhí)行

public Object execute(SqlSession sqlSession, Object[] args) {

Object result;

//可以看到執(zhí)行時(shí)就是4種情況,insert|update|delete|select,分別調(diào)用SqlSession的4大類(lèi)方法

if (SqlCommandType.INSERT == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.insert(command.getName(), param));

} else if (SqlCommandType.UPDATE == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.update(command.getName(), param));

} else if (SqlCommandType.DELETE == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.delete(command.getName(), param));

} else if (SqlCommandType.SELECT == command.getType()) {

if (method.returnsVoid() && method.hasResultHandler()) {

//如果有結(jié)果處理器

executeWithResultHandler(sqlSession, args);

result = null;

} else if (method.returnsMany()) {

//如果結(jié)果有多條記錄

result = executeForMany(sqlSession, args);

} else if (method.returnsMap()) {

//如果結(jié)果是map

result = executeForMap(sqlSession, args);

} else {

//否則就是一條記錄

Object param = method.convertArgsToSqlCommandParam(args);

result = sqlSession.selectOne(command.getName(), param);

}

} else {

throw new BindingException("Unknown execution method for: " + command.getName());

}

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;

}

?

?

?

?

?

轉(zhuǎn)載于:https://my.oschina.net/iioschina/blog/1860215

因篇幅問(wèn)題不能全部顯示,請(qǐng)點(diǎn)此查看更多更全內(nèi)容

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2

違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)