MyBatis may be used by many people, but the SQL execution process of MyBatis may not be clear to all people, so since it is in, read through this article you will gain as follows:

  • 1. How are Mapper interfaces and mapping files bound

  • 2. Execution process of SQL statement in MyBatis

  • 3. Customize the parameter setting processor typeHandler in MyBatis

  • 4. Customize the result set processor typeHandler in MyBatis

PS: This article is based on MyBatis3.5.5 version of the source code.

The profile

In MyBatis, the use of programmatic data query, is mainly the following lines of code:

SqlSession session = sqlSessionFactory.openSession(); UserMapper userMapper = session.getMapper(UserMapper.class); A List < LwUser > userList = userMapper. ListUserByUserName (" lone Wolf 1 ");Copy the code

The first line is to obtain a SqlSession object, which was analyzed in the previous article. If you want to learn more about it, you can click here. The second line is to obtain the UserMapper interface, and the third line is a line of code that implements the entire query process.

GetMapper interface (getMapper)

GetMapper (); getMapper (); getMapper (); getMapper (); getMapper ();

1. After getMapper is called, the Mapper object will be retrieved from the Configuration object, because the Mapper interface will be loaded and parsed into the Configuration object when the project starts

2. Continue to call the getMapper method through the MapperRegistry object property in the Configuration object

3. According to type type, the proxy factory class corresponding to the current type is retrieved from knownMappers in the MapperRegistry object, and then the proxy class corresponding to Mapper is generated using the proxy factory class

4. Finally obtain the MapperProxy object corresponding to our interface

MapperProxy, on the other hand, implements InvocationHandler, using the JDK dynamic proxy.