Unveiling MyBatis: A Deep Dive into Its Architecture and Execution Flow
This article provides a comprehensive, step‑by‑step analysis of MyBatis’s overall architecture, covering its core features, three‑layer design, configuration parsing, proxy generation, SQL execution process, and the underlying components that enable seamless Java‑to‑SQL mapping.
1 Introduction
MyBatis is a lightweight ORM framework that maps Java objects to relational databases using XML or annotation configurations. It provides SQL mapping, dynamic SQL, parameter mapping, result set mapping, transaction management, connection‑pool integration, and second‑level caching.
2 Architecture Overview
The MyBatis architecture consists of three logical layers:
Foundation layer – handles resource loading, configuration parsing, and abstracts DataSource and Transaction management.
Core processing layer – parses mapper XML, builds MappedStatement objects, and executes SQL.
Interface layer – exposes mapper interfaces to application code via dynamic proxies.
Typical usage flow:
Define a mapper interface and its XML configuration.
Load and parse the configuration files.
Generate a JDK dynamic proxy for the interface and invoke the associated SQL.
3 Configuration File Parsing
Parsing proceeds in two main steps:
Load configuration file : MyBatis reads mybatis-config.xml and creates a Configuration object.
Parse main configuration : An XML parser extracts global settings (data source, plugins, type aliases, cache) and stores them in the Configuration object.
All parsers inherit from BaseBuilder. Key builder classes are: XMLStatementBuilder – parses <select>, <insert>, <update>, <delete> tags and builds MappedStatement objects. XMLMapperBuilder – parses mapper XML files ( Mapper.xml) to construct SQL, parameter, result mappings, and cache configuration. XMLConfigBuilder – parses the main configuration file to set up data source, type aliases, plugins, and cache.
After parsing, SqlSessionFactoryBuilder.build loads the populated Configuration into a SqlSessionFactory for later use.
4 Proxy Construction
When configuration parsing is complete, MyBatis creates a dynamic JDK proxy for each mapper interface via SqlSession.getMapper. The process involves: MapperProxyFactory – creates MapperProxy instances. MapperRegistry – registers mapper interfaces and their proxy factories. MapperProxy – implements InvocationHandler to intercept method calls, locate the corresponding MappedStatement, and delegate execution to the core processing layer.
The resulting proxy implements the mapper interface, allowing application code to call methods such as selectOne() or insert() directly.
Reference: https://juejin.cn/post/7273434821807947811
5 SQL Execution
After the proxy is built, MyBatis executes SQL through the following steps:
Create SQL statement : instantiate Statement, PreparedStatement, or CallableStatement as required.
Execute query : invoke the appropriate execute method on the statement object.
Process results : retrieve data via a ResultSet and map rows to Java objects using the result mappings defined in the mapper XML.
Dynamic SQL, parameter substitution, and result mapping are handled by the core processing components before the JDBC call is made.
6 End‑to‑End Workflow
The complete MyBatis workflow can be summarized as:
Create SqlSessionFactory : read mybatis-config.xml and build a SqlSessionFactory via SqlSessionFactoryBuilder.
Create SqlSession : obtain a session representing a single database interaction.
Obtain Mapper Proxy : call sqlSession.getMapper(YourMapper.class) to receive a dynamic proxy.
Execute SQL : invoke mapper methods ( selectOne(), selectList(), insert(), update(), delete()) which trigger SQL parsing, dynamic SQL handling, and parameter mapping.
Send SQL to JDBC : the parsed statement is executed via JDBC ( Statement / PreparedStatement / CallableStatement).
Map Results : MyBatis converts the ResultSet into Java objects according to the result mappings.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
