mybatis中session.getMapper方法源碼分析

 

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

1 DefaultSqlSession類

 

@Overridesql

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

//最後會去調用MapperRegistry.getMappersession

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

}ide

 

2 Configuration類

 

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

return mapperRegistry.getMapper(type, sqlSession);this

}spa

3 MapperRegistry類

 

//返回代理類.net

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> 類

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自帶的動態代理生成映射器

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

}

 

MapperProxy類

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;

}

 

@Override

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

//代理之後,全部Mapper的方法調用時,都會調用這個invoke方法

//並非任何一個方法都須要執行調用代理對象進行執行,若是這個方法是Object中通用的方法(toString、hashCode等)無需執行

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

try {

return method.invoke(this, args);

} catch (Throwable t) {

throw ExceptionUtil.unwrapThrowable(t);

}

}

//這裏優化了,去緩存中找MapperMethod

final MapperMethod mapperMethod = cachedMapperMethod(method);

//執行

return mapperMethod.execute(sqlSession, args);

}

MapperMethod類

 

 

//執行

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

Object result;

//能夠看到執行時就是4種狀況,insert|update|delete|select,分別調用SqlSession的4大類方法

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()) {

//若是有結果處理器

executeWithResultHandler(sqlSession, args);

result = null;

} else if (method.returnsMany()) {

//若是結果有多條記錄

result = executeForMany(sqlSession, args);

} else if (method.returnsMap()) {

//若是結果是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;

}

相關文章
相關標籤/搜索