Unlock MyBatis: 10 Essential Design Patterns Powering Its Architecture
This article examines how MyBatis leverages around ten core design patterns—including Factory, Singleton, Builder, Adapter, Proxy, Composite, Decorator, Template, Strategy, and Iterator—to decouple complex scenarios, improve modularity, and streamline ORM operations within its extensive codebase.
MyBatis’s source code, spanning over twenty thousand lines, extensively applies design patterns to decouple complex scenarios, forming the framework’s core strength.
Creational Patterns
Factory Pattern
The SqlSessionFactory creates session instances, handling data source configuration, transaction factories, and executor creation. It follows a simple factory approach where a parent class provides a method to instantiate objects, allowing subclasses to determine the concrete type.
Related components: SqlSessionFactory , ObjectFactory , MapperProxyFactory , and DataSourceFactory .
Singleton Pattern
The Configuration class is a large singleton that persists throughout the session lifecycle, initializing mappings, caches, parameters, interceptors, and object factories. It ensures a single instance with a global access point.
Related components: ErrorContext , LogFactory , and Configuration .
Builder Pattern
MyBatis uses numerous Builder classes (e.g., SqlSessionFactoryBuilder , XMLConfigBuilder , XMLMapperBuilder , XMLStatementBuilder , CacheBuilder ) to construct complex objects step‑by‑step, isolating object construction from business logic.
Structural Patterns
Adapter Pattern
To unify various logging frameworks (Log4j, Log4j2, SLF4J), MyBatis defines a common logging interface and adapts each framework accordingly.
Related components: logging adapters for multiple frameworks.
Proxy Pattern
MyBatis’s MapperProxy implements DAO interfaces, intercepting method calls and delegating them to executors, enabling dynamic proxying of database operations.
Related components: DriverProxy , Plugin , Invoker , MapperProxy .
Composite Pattern
SQL dynamic tags (trim, where, set, foreach, if, choose, when, otherwise, bind) form a tree‑like structure via the SqlNode interface, allowing hierarchical composition of SQL fragments.
Related components: various SqlNode implementations.
Decorator Pattern
Second‑level caching wraps the simple executor using a decorator, adding caching behavior without altering the executor’s core logic.
Related components: caching executor, simple executor.
Behavioral Patterns
Template Method Pattern
The BaseExecutor defines a fixed algorithm framework for query and update operations, while subclasses customize specific steps.
Related components: BaseExecutor , SimpleExecutor , BaseTypeHandler .
Strategy Pattern
MyBatis employs the TypeHandler interface to encapsulate algorithms for handling different JDBC types, allowing interchangeable strategies for type conversion.
Related components: various TypeHandler implementations such as LongTypeHandler , StringTypeHandler , DateTypeHandler .
Iterator Pattern
The PropertyTokenizer iterates over property expressions in MyBatis’s meta‑object system, enabling traversal without exposing underlying collection structures.
Related component: PropertyTokenizer .
Conclusion
Through this analysis, MyBatis is found to employ roughly ten design patterns, illustrating how sophisticated ORM frameworks rely on proven architectural solutions to manage complexity. Studying such source code deepens understanding of design principles, expands coding mindset, and equips developers to become proficient architects.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
