第五篇文章中,我们获取到了SqlSession对象,然后我们在执行的时候是通过获取Mapper,然后通过执行mapper的方法来获取我们需要的数据,这篇我们就看一下这个通过SqlSession获取Mapper的过程。
DefaultSqlSession
public <T> T getMapper(Class<T> type) {
//这里调用的是configuration的getMapper方法
return configuration.<T>getMapper(type, this);
}
configuration
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//这里其实调用得失mapperRegistry的getMapper方法
return mapperRegistry.getMapper(type, sqlSession);
}
mapperRegistry
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//首先我们在缓存中拿到当前class对应的MapperProxyFactory
//MapperProxyFactory是在我们最开始解析Mapper的时候存入的
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
//这里必须是解析过的Mapper才能执行
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is n