Design Patterns Used in the MyBatis Framework
This article examines the various design patterns—such as Factory, Singleton, Builder, Adapter, Proxy, Composite, Decorator, Template, Strategy, and Iterator—employed within the MyBatis source code, explaining their structures, usage scenarios, and related components to illustrate how the framework achieves modularity and flexibility.
MyBatis's over 20,000 lines of source code heavily employ design patterns to decouple complex scenarios in the framework architecture.
Creational Patterns
Factory Pattern – The SqlSessionFactory structure (see Figure 2) creates session objects; each session opens a new database connection and assembles configuration, transaction factories, and SQL executors.
In this context, the factory method allows subclasses to decide which concrete SqlSessionFactory implementation to instantiate.
Singleton Pattern – The Configuration class is a large singleton that holds all configuration objects (mappers, caches, type handlers, etc.) throughout the session lifecycle.
It guarantees a single instance and provides a global access point.
Builder Pattern – MyBatis uses many *Builder classes (e.g., SqlSessionFactoryBuilder, XMLConfigBuilder) to incrementally construct complex objects such as ResultMap.
The builder isolates object construction from business logic.
Structural Patterns
Adapter Pattern – Provides a unified logging interface for disparate frameworks (Log4j, Log4j2, SLF4J) used by MyBatis.
It enables incompatible logging APIs to cooperate.
Proxy Pattern – MapperProxy implements DAO interfaces, intercepting method calls and delegating to executors.
It supplies a placeholder that adds pre‑processing before actual execution.
Composite Pattern – SqlNode implementations form a tree structure for dynamic SQL tags (trim, where, set, foreach, etc.).
It represents part‑whole hierarchies of SQL fragments.
Decorator Pattern – Second‑level cache decorates the simple executor, adding caching behavior without modifying the executor.
It wraps objects to extend functionality.
Behavioral Patterns
Template Method Pattern – BaseExecutor defines the skeleton of SQL execution; subclasses override specific steps.
It standardizes the workflow while allowing customization.
Strategy Pattern – Different TypeHandler implementations provide algorithms for handling various Java types.
It enables interchangeable processing strategies.
Iterator Pattern – PropertyTokenizer iterates over property tokens for MyBatis meta‑object reflection.
It traverses collections without exposing internal structures.
Conclusion
Through this review, we see that MyBatis leverages roughly ten design patterns to manage complexity, demonstrating how a well‑designed ORM framework applies classic software engineering principles to achieve extensibility and maintainability.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
