1. 简介
SQL查询执行流程图如下

通过这个流程图可以发现这里的JDK动态代理Mapper接口很有趣,那么接下来对这部分进行分析
2. JDK动态代理Mapper接口解析

- 由上面的流程图发现Mapper接口的代理对象是从SqlSession的getMapper获取得到的,如下代码:
@Override
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
通过代码发现,getMapper方法最终调用的是对Configuration对象的引用的getMapper方法完成的,然后继续往下看
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
MapperRegistry是存放所有Mapper接口代理对象的容器,属性knownMappers是一个Map<Class<?>, MapperProxyFactory<?>>对象的引用,用于存放所有mapper接口的代理工厂对象.
@SuppressWarnings("unchecked")
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中生成代理对象的代码如下
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
3. 调用查询方法具体解析
- 上面我们知道代理对象是由MapperProxyFactory的代理工厂通过JDK动态代理模式生成代理对象,代理类为MapperProxy。代理类的invoke执行部分如下
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else {
return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
cachedInvoker方法会先从Map<Method, MapperMethodInvoker> methodCache根据Method取基于MapperMethodInvoker接口的实现类PlainMethodInvoker,如果取不到就创建PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()))对象,并把对象put到methodCache集合中。
PlainMethodInvoker的invoke方法执行体如下
@Override
public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
return mapperMethod.execute(sqlSession, args);
}
MapperMethod的execute的执行体如下
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
if (method.returnsOptional()
&& (result == null || !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
default:
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;
}
MapperMethod类有两个比较重要的对象,SqlCommand和MethodSignature。这两个类的作用单独用一篇文章来讲吧。
网友评论