NOTE
Introduction
Why MyBatis exists and its execution flow
This is a historical learning note and may contain outdated or incomplete understanding.
Why MyBatis
- JDBC
SQL is written directly in Java code, which causes severe hard-coding.
- Hibernate
A complete ORM that operates on the database in an object-oriented way.
However, it is not flexible enough.
- MyBatis
You can write code manually while putting SQL in configuration files.
Flow
- MyBatis configuration
SqlMapConfig.xml is MyBatis’s global configuration file and configures information such as the MyBatis runtime environment.
The mapper.xml file is the SQL-mapping file. SQL statements that operate on the database are configured in this file. This file needs to be loaded from SqlMapConfig.xml.
-
Build the
SqlSessionFactory, the session factory, from MyBatis environment and other configuration information. -
The session factory creates a
SqlSession. Database operations need to go through theSqlSession. -
At the bottom layer, MyBatis defines an
Executorinterface for operating on the database. TheExecutorinterface has two implementations: a basic executor and a caching executor. -
MappedStatementis also a low-level MyBatis wrapper object. It wraps MyBatis configuration information and SQL-mapping information. One SQL statement in amapper.xmlfile corresponds to oneMappedStatementobject, and the SQL id is the id of theMappedStatement. -
MappedStatementdefines the input parameters for SQL execution, includingHashMap, primitive types, and POJOs. Before executing SQL,Executormaps the input Java object into the SQL throughMappedStatement. This input-parameter mapping is equivalent to setting parameters on aPreparedStatementin JDBC programming. -
MappedStatementdefines the output result of SQL execution, includingHashMap, primitive types, and POJOs. After executing SQL,Executormaps the output result to Java objects throughMappedStatement. This output-result mapping process is equivalent to parsing and processing the result set in JDBC programming.

Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub