Step 1,

General use of Mybatis has the following 4 steps.

Select * from sqlSessionFactory;

  • Each information of the parse file is saved in Configuration. Return DefaultSqlSession containing Configuration.
  • Note: [MappedStatement] : represents a add, delete, change, check details

2. Obtain the sqlSession object

  • Return a DefaultSQlSession object containing Executor and Configuration;
  • This step creates an Executor object;

3. Get the proxy object of the interface (MapperProxy)

  • GetMapper: creates a proxy object for MapperProxy using MapperProxyFactory

  • The proxy object contains DefaultSqlSession (Executor)

4. Implement the method of increase, deletion, change and check

Code sample

@Test
	public void test01(a) throws IOException {
		// get the sqlSessionFactory object
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		// 2. Obtain the sqlSession object
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			// get the implementation object of the interface
			// A proxy object is automatically created for the interface. The proxy object performs the add, delete, change and query methods
			EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
			Employee employee = mapper.getEmpById(1);
			System.out.println(mapper);
			System.out.println(employee);
		} finally{ openSession.close(); }}Copy the code

2. Create the SqlSessionFactory from the configuration file

Basically, the master Configuration file is parsed and saved in the global Configuration object. Then, every attribute of the add, delete, change and check tag in the mapping file is parsed and encapsulated into an MappedStatement

3. Obtain the sqlSession object

Returns an implementation class DefaultSqlSession object for SqlSession. It contains Executor and Configuration

4. Return the interface proxy object

Basically also uses JDK dynamic proxy.

5. Query process

Complete the process

Query summary

6. Complete summary (master)

1. Initialize the Configuration object based on the Configuration file (global, SQL mapping)

Create a DefaultSqlSession object containing the Configuration and Executor (create the corresponding Executor from defaultExecutorType in the global Configuration file).

3, DefaultSqlSession. GetMapper () : to get the corresponding MapperProxy Mapper interfaces;

DefaultSqlSession (MapperProxy);

5. Method of adding, deleting, modifying and checking:

1) Call Executor for DefaultSqlSession Create a StatementHandler object. ParameterHandler and ResultSetHandler are also created for ParameterHandler and ResultSetHandler. ParameterHandler is used to set parameters for SQL parameters. StatementHandler; 5) ResultSetHandler encapsulates the resultCopy the code

Note:

Each created four object has a interceptorChain pluginAll (parameterHandler);

7, Mybatis overview

Finally, an overview of Mybatis is attached to help understand