标签:apt throw throwable catch invoke instance code mybatis dual
@Mapkey("id")指定返回类型为map时,将id作为key
我们写一个UserMapper接口,Mybatis会为改接口创建一个MapperProxy对象。
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
map以mapper接口类为key,value是接口类对应的代理工厂。void addMapper(Class<T> type)
方法简单的将type放进knownMappers里面。不过,这里面还有 MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
这样一个解析操作,暂不做分析。
<T> T getMapper(Class<T> type, SqlSession sqlSession)
该方法是本类最重要的一个方法了,获取Mapper接口的代理对象也正是从该方法获取的。mapperProxyFactory.newInstance(sqlSession)
。 public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
再进去看
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
发现这就是标准的JDK面向接口的动态代理啊。要问对应的InvocationHandler在哪里?mapperProxy就是啦。
至此,Mybatis如何获取Mapper接口的代理对象已经解决。
再梳理一下整个流程:
sqlsession.getMapper->configuration.getMapper->mapperRegistry.getMapper->获取Mapper代理对象
创建好MapperProxy对象之后,调用Mapper.xx方法时就会走MapperProxy的invoke方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {//如果方法是在Object里面的,放行
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
final MapperMethod mapperMethod = cachedMapperMethod(method);//缓存method方法
return mapperMethod.execute(sqlSession, args); // 执行
}
发现关键点在于mapperMethod.execute(sqlSession, args)
方法
标签:apt throw throwable catch invoke instance code mybatis dual
原文地址:https://www.cnblogs.com/studentytj/p/10428572.html