The process of Mapper acquisition by Mybatis

An overview of

  • Mybatis loads mapper code
  • The process of instantiating the Mapper
  • conclusion

Mybatis loads mapper code

SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); / / get DefaultSqlSession SqlSession session. = sqlSessionFactory openSession (); UserMapper = session.getMapper(usermapper.class);Copy the code

The process of instantiating the Mapper

UserMapper = session.getmapper (usermapper.class); So we go directly to the corresponding getMapper method of DefaultSqlSession:

//org.apache.ibatis.session.defaults.DefaultSqlSession#getMapper(Class<T> type)
@Override
public <T> T getMapper(Class<T> type) {
  return configuration.<T>getMapper(type, this);
}
Copy the code

We can see from the code, in fact, get the org. Apache. Ibatis. Session. Configuration# getMapper (Class < T > type, SqlSession SqlSession) this method, So now let’s go to the corresponding method and see:

//org.apache.ibatis.session.Configuration#getMapper(Class<T> type, SqlSession sqlSession)
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
   return mapperRegistry.getMapper(type, sqlSession);
 }
Copy the code

Can clearly see that this method is actually call org. Apache. Ibatis. Binding. MapperRegistry# getMapper (Class < T > type, SqlSession SqlSession) this method, Continue to look at the corresponding method content

//org.apache.ibatis.binding.MapperRegistry#getMapper(Class<T> type, SqlSession sqlSession)
@SuppressWarnings("unchecked")
  public <T> T getMapper(Class<T> type. Final MapperProxyFactory<T> MapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type); // There is no direct exception thrownif (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry."); } try {// Proxy factories exist to generate proxy objectsreturn mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: "+ e, e); }}Copy the code

We can see, the key code implementation is in mapperProxyFactory newInstance (sqlSession); So we are now into the org. Apache. Ibatis. Binding. MapperProxyFactory# newInstance (org. Apache. Ibatis. Session. SqlSession) this method:

//org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.session.SqlSession)
public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }
Copy the code

Through the newInstance (mapperProxy), Found another method in the class of org. Apache. Ibatis. Binding. MapperProxyFactory# newInstance (org. Apache. Ibatis. Binding. MapperProxy < T >), the code is as follows:

//org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.binding.MapperProxy<T>)
 @SuppressWarnings("unchecked") protected T newInstance(MapperProxy<T> MapperProxy) {// Proxy generation processreturn (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }
Copy the code

Specific org. Apache. Ibatis. Binding. MapperProxy class content, this time don’t look at first, next time in process to view the specific call to tell them.

conclusion

Through the call process of source code, we know in Mybatis to obtain the corresponding Mapper, in fact, to obtain a proxy (proxy is not quite understand, you can find the relevant blog on the Internet, it is not said in detail), but after getting this proxy, Mybatis to do what, the next article continues, Mybatis SqlSessionFactory initialization process, let’s wait and talk about later.