TestMapper mapper1 = sqlSession.getMapper(TestMapper.class);
TestMapper mapper2 = sqlSession.getMapper(TestMapper.class);
System.out.println(mapper1 == mapper2);
读mybatis源码过程中,产生了一个想法,sqlSession作为一个会话,也可以说说一个门面,其中集成了很多的针对数据库的操作接口,也就是很多方法,以及在创建过程中,将Configuration传入了进来。
那么产生了这么一个疑问,在同一个会话中,如果我对同一个mapper接口获取两次mapper代理类对象,那么两次获取的代理类对象是同一个吗?
手动做了一下实验,发现为上图的运行结果为false,随即对mapper代理对象的构建过程进行了跟踪,其实过程也很简单:
public <T> T getMapper(Class<T> type) {
return configuration.<T>getMapper(type, this);
}
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
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);
}
}
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);
}
从mybatis的源码可以看出:
1、sqlsession调用了configuration的getMapper方法
2、configuration调用了MapperRegistry的getMapper方法
3、MapperRegistry中存放了MapperProxyFactory的缓存
4、最终通过MapperProxyFactory的newInstance方法来获取一个mapper代理对象,获取的最终过程还是通过java的动态代理,并且每次获取都是重新生成了一个新的代理对象。
5、sqlSession中并没有缓存生成的mapper代理对象,为什么不考虑在会话中设置一个缓存mapper代理对象的缓存呢?也许是觉得没有必要吧,毕竟一个会话中多次获取同一个mapper代理对象的机会也不多。