上篇博客我们讲到sqlSession是如何产生的。这篇博客我们讲讲mapper.java和mapper.xml是如何映射起来的。
程序中我们只有mapper.java接口,没有mapper实现类,那么是如何调用方法的呢?
mybatis里所有mapper接口的实现类都可以看做是mapperProxy,mapper代理类,然后调用MapperProxy.invoke()方法,invoke()方法会执行相应sql语句,并将结果返回。
public class MapperProxy implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;
private final Class mapperInterface;
private final Map methodCache;
public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {
this.sqlSession = sqlSession;
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 if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
final MapperMethod mapperMethod = cachedMapperMethod(method);
//开始执行sql语句
return mapperMethod.execute(sqlSession, args);
}
}
那么是如何调用的mapperProxy代理类呢?且看一下分解:
1、程序在sqlSession中会调用configuration的getMapper方法
@Override
public T getMapper(Class type) {
return getConfiguration().getMapper(type, this);
}
2、当时使用configuration的getMapper方法时,会调用mapperRegistry的getMapper方法
public T getMapper(Class type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
3、当使用mapperRegistry的getMapper方法时,会调用mapperProxyFactory.newInstance(sqlSession)方法,得到一个MapperProxy对象
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 {
// 转移到mapperProxyFactory
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
4、在这里,先通过T newInstance(SqlSession sqlSession)方法会得到一个MapperProxy对象,然后调用T newInstance(MapperProxy
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy mapperProxy) {
//所有动态代理类的方法调用,都会交由InvocationHandler接口实现类里的invoke()方法去处理。这是动态代理的关键所在。
//动态代理我们写的dao接口,第一个参数是类加载器,第二个参数是mapper接口,第三个参数是mapper代理类
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);
}
5、而查看MapperProxy的代码,可以看到如下内容:
public class MapperProxy implements InvocationHandler, Serializable {
//mapper动态代理实现InvocationHandler接口,重写invoke方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
//此处表示InvocationHandler的invoke方法,动态代理最后都会调用InvocationHandler的invoke方法
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);
//sqlSession我们在上一篇博客以获取,现在有得到了mapper接口的代理类,所以此处开始执行sql
return mapperMethod.execute(sqlSession, args);
}
}
该MapperProxy类实现了InvocationHandler接口,并且实现了该接口的invoke方法。
通过这种方式,我们只需要编写Mapper.java接口类,当真正执行一个Mapper接口的时候,就会转发给MapperProxy.invoke方法,而该方法则会调用后续的sqlSession.cud>executor.execute>prepareStatement等一系列方法,完成SQL的执行和返回。
通过以上的mapperProxy代理类,我们就可以方便的使用mapper.java和mapper.xml了。