文章目录
Mapper接口的注册过程
CachedAuthorMapper cachedAuthorMapper = sqlSession.getMapper(CachedAuthorMapper.class);
使用Mybatis框架的时候,我们调用sqlSession的getMapper方法可以获取到一个Mapper对象,但是我们定义的Mapper是接口,我们要调用方法必须是个实例对象,那么getMapper方法返回的到底是个什么呢?其实,getMapper方法返回的是一个我们定义的Mapper接口的动态代理对象。
Mapper接口的注册时序
SqlSession的getMapper方法
实际上SqlSession将功能委托给了Configuration的getMapper对象。Configuration对象又将方法委托给了MapperRegistry的getMapper对象。
@Override
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
MapperRegistry的getMapper方法
public