两个重要的类 MapperProxyorg.mybatis mybatis3.5.4
mybatis的mapper是通过jdk动态代理实现的,而 InvocationHandler 的实现类是org.apache.ibatis.binding.MapperProxy。
MapperMethodMapper类的方法,实际时调用了 MapperMethod 的 execute 方法。
生成代理对象的调用时序-
SqlSession 的 getMapper(Class type) 为起点,返回的是 a mapper bound to this SqlSession。
-
调用 Configuration 的 getMapper(Class type, SqlSession sqlSession)。
-
调用 MapperRegistry 的 getMapper(Class type, SqlSession sqlSession):
根据 type 在 knownMappers(Map类型) 中获取 MapperProxyFactory 实例对象。
knownMappers 需要先完成初始化,要调用addMapper;knownMappers key为Class>,value为 MapperProxyFactory>。 -
调用 MapperProxyFactory 的 newInstance(SqlSession sqlSession) 生成并返回Mapper对象。具体如下:
调用 new MapperProxy(sqlSession, mapperInterface, methodCache) 获取 InvocationHandler 的实现类 MapperProxy;
调用 Proxy.newProxyInstance 返回jdk动态代理对象。
补充:MapperProxy实现了InvocationHandler接口,调用invoke方法时,如果 methodCache 没有缓存,则先缓存。
MapperProxy#methodCache类型为Map
methodCache是被保存在MapperProxy对应的MapperProxyFactory中。
- 需要代理的方法
- 如果是default方法,返回的是 DefaultMethodInvoker 类型对象,其构造器参数是 MethodHandle。
- 如果不是default方法,返回的是 PlainMethodInvoker 类型对象,其构造器参数是 MapperMethod。
- 如果是 PlainMethodInvoker,最终调用的是 MapperMethod 的 execute 方法。
处理参数后,调用 sqlSession 中的 select 方法,有三个实现类,
接下来可以了解,SqlSessionTemplate、SqlSessionManager有用到了装饰者模式,还有 DefaultSqlSession。
- 通过 configuration 获取 MappedStatement。
- 调用 Executor 的 query。
public class MapperProxy实例的创建在 MapperProxyFactory#newInstanceimplements InvocationHandler, Serializable { private final SqlSession sqlSession; private final Class mapperInterface; private final Map methodCache; public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) { this.sqlSession = sqlSession; //MapperProxyFactory中只有这两个字段,methodCache 在调用本类的 cachedInvoker 时缓存的。 this.mapperInterface = mapperInterface; this.methodCache = methodCache; } @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 { //获取缓存方法(没有则生成),然后调用。cachedInvoker返回的是 MapperMethodInvoker 对象。 return cachedInvoker(method).invoke(proxy, method, args, sqlSession); } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } interface MapperMethodInvoker { Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable; } }
public class MapperProxyFactory{ private final Class mapperInterface; private final Map methodCache = new ConcurrentHashMap<>(); public MapperProxyFactory(Class mapperInterface) { this.mapperInterface = mapperInterface; } public Class getMapperInterface() { return mapperInterface; } public Map getMethodCache() { return methodCache; } protected T newInstance(MapperProxy mapperProxy) { return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); } public T newInstance(SqlSession sqlSession) { final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); } }
public class MapperRegistry {
private final Configuration config;
private final Map, MapperProxyFactory>> knownMappers = new HashMap<>();
public MapperRegistry(Configuration config) {
this.config = config;
}
public T getMapper(Class type, SqlSession sqlSession) {
final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) 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);
}
}
}
参考:
MyBatis 学习记录3 MapperMethod类



