[TOC]

1. Review the SQL execution process of Mybatis

In the previous code, we redefined our running process. First, we executed Test, calling the DAO interface method

Interface definition:

Call the implementation class method of the interface:

Finally, call the real SQL:

The above code is in the interface implementation class itself to execute the ID, find and execute the SQL file in mapper, so we want to save a step?

If we don’t need to implement the interface, we just need to match the interface name with the namespace of mapper file, and match the interface method name with the ID of SQL statement tag.

In fact, Mybatis provides such a practice, which is mapper dynamic proxy.

Mapper dynamic proxy 2.

First, the main configuration file (Mybatis. XML) is used to configure database connection information and register mapper files that need to be scanned:

Mapper = mapper; mapper = mapper; mapper = mapper;

definemapperFile (A namespace is the fully qualified class name of an interface) :

Sqlsession.getmapper (); sqlsession.getmapper (); sqlsession.getmapper (); The DAO and mapper.xml files are associated, and the ID of each SQL entry corresponds to the name of our interface method.

SelectStudentMap () : SelectList() : SelectList() : SelectList() : SelectList() : SelectList(); The underlying method will only call SelectOne() or SelectList(), so this method will report an error, and the framework will automatically use SelectList() if the type is accepted, otherwise it will select SelectOne().

Here we are using a map to return, so the selectOne() method is automatically selected and an error is reported. Mybatis mapper returns the result set of a map. If we need to use the automatic return map, we can make a map, or return the list after processing, this knowledge will be described later

3. How does mapper dynamic proxy do?

Create a breakpoint on sqlsession.getmapper () :

We can see that the following interface methods (methods that interface to SqlSession) are executed

<T> T getMapper(Class<T> var1);
Copy the code

DefaultSqlSession (SqlSessionManager); DefaultSqlSession (SqlSessionManager);

 public <T> T getMapper(Class<T> type) {
    return this.configuration.getMapper(type, this);
  }
Copy the code

We know that the configuration object confiiguration is created when sqlSession is created. This uses the getMapper() method of the Mapper registry object to pass in the current sqlSession object:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }
Copy the code

KnownMappers. Get (type) is used to get the mapper proxy factory. KonwnMappers is a hashMap, where the mapperProxyFactory object is initialized. After obtaining the factory object, use sqlSession to instantiate it:

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); }}Copy the code

When instantiated, mapper dynamic proxy is used:

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);
  }
Copy the code

As you can see from the following debug result, this is the result of the dynamic proxy. We see the DAO, but the dynamic proxy enhances the DAO, which is actually a mapperProxy.

[Author profile] : Qin Huai, public number [Qin Huai Grocery store] author, the road of technology is not at that time, mountain high water long, even slow, chi and not stop. The world wants everything to be fast and faster, but I hope I can take every step, write every article, and look forward to communicating with you.

This article only represents their own (this rookie) learning accumulation record, or learning notes, if there is infringement, please contact the author to verify deletion. No one is perfect, the article is the same, the writing style is immature, I am not talented, do not spray, if there is a mistake, also hope to point out, grateful ~