事情要从MapperFactoryBean开始说起
![](https://img.haomeiwen.com/i4165335/717bcacc7630fe1c.png)
![](https://img.haomeiwen.com/i4165335/f70a8b20bdfdcd1a.png)
所以MapperFactoryBean对象绑定一个SqlSessionTemplate对象;
![](https://img.haomeiwen.com/i4165335/afa70d94a3c0169c.png)
getObject方法获取mapper接口的代理对象
![](https://img.haomeiwen.com/i4165335/1fa3c6802f9044dd.png)
![](https://img.haomeiwen.com/i4165335/0e6ebfebff62bdcd.png)
![](https://img.haomeiwen.com/i4165335/990fd65004e379b4.png)
![](https://img.haomeiwen.com/i4165335/ad5b05fa96a2894d.png)
![](https://img.haomeiwen.com/i4165335/32c8533ce1069c19.png)
![](https://img.haomeiwen.com/i4165335/ebc99801551e31b4.png)
关于mapper接口的注册、解析
MapperRegistry
![](https://img.haomeiwen.com/i4165335/9c0617cc69330e38.png)
MapperProxy实现了InvocationHandler接口
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// 1、 如果是Object类定义的方法, 且子类没有重写, 比如hashCode,toString,equals等方法, 则直接走Object类的方法
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
//2、 如果是接口默认方法, 因为接口允许实现静态方法、默认方法; 非抽象方法的调用说明用户已经自己实现了代码逻辑, 所以不用代理非抽象方法
} else if (isDefaultMethod(method)) {
// 直接调用接口默认方法
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
// 3、 代理接口抽象方法, 用MapperMethod类来实现
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
private MapperMethod cachedMapperMethod(Method method) {
// MapperProxyFactory.methodCache保存了方法对应的MapperMethod实例,
// 如果方法对应的MapperMethod实例已经存在,则复用
// 否则, 创建一个新的MapperMethod实例,并缓存到MapperProxyFactory.methodCache
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}
网友评论