How baomidou/mybatis-plus works
This project is a multi-module framework designed to extend MyBatis functionality – the “MyBatis-Plus” project. Its purpose is to simplify development by providing utilities such as code generation, conditional query wrappers, pagination, auto-configuration for Spring Boot, and other “plus” enhancements that sit on top of MyBatis. The project is organized into numerous modules (for example: mybatis-plus-annotation, mybatis-plus-core, mybatis-plus-extension, mybatis-plus-generator, mybatis-plus-jsqlparser-support, mybatis-plus-spring, and several Spring Boot starter modules) that together form a cohesive ecosystem which can be integrated into applications using MyBatis.
Main Components
• MyBatis-Plus Core: The heart of the framework which includes core functionality (e.g., SQL session factory builder, mapper registry, query/condition wrappers, handlers and toolkits).
• Annotation Module: Contains annotations that mark database properties and configurations (such as @TableField, @TableId, etc.) to drive ORM behavior.
• Extension Module: Provides additional features like plugins (e.g., pagination, optimistic locker, interceptor-based SQL modifications), advanced type handlers, dynamic table name features, and active record support.
• Code Generator (mybatis-plus-generator): Automates the generation of entity classes, mapper interfaces, and XML mapping files. It is tightly coupled with the core but functions as its own utility module.
• JSQLParser Support: Modules (including both JSQLParser-4.9 and JSQLParser-5.0 versions) that enable advanced SQL parsing and manipulation, caching support, and integration with MyBatis-Plus functions.
• Spring Integration Components: Comprising the mybatis-plus-spring, spring-boot-starter (and its variations for Spring Boot 2 and 3) modules along with auto-configuration classes. These components allow seamless integration into Spring-based applications by providing auto-configuration, enhanced DI support, and configuration customization.
• Test and Example Components: The project also contains a vast test suite and sample applications that illustrate how each module works and integrates.
Relationships and Interactions
• MyBatis-Plus Core is central. Most other modules such as the Annotation, Extension, Code Generator, and JSQLParser modules depend on and extend its functionalities.
• The Extension module plugs into the Core to provide additional features. For example, its plugins and enhanced type handlers augment core SQL operations.
• The Code Generator uses configurations defined in the Core and Annotation modules to build out mappings and generate code.
• The Spring-specific modules (including auto-configuration and Spring Boot starter modules) wrap around the Core and Extension modules to ensure proper dependency injection, configuration, and integration with Spring’s lifecycle.
• The JSQLParser support modules sit as specialized services, assisting with SQL parsing and caching; they enable better performance (and enhanced features like dynamic table name handling) by enriching the data processing pipeline.
Architectural Patterns and Design Principles
• Modular Design – The project is clearly divided into discrete modules that each focus on different concerns. This separation of concerns simplifies maintenance and extension.
• Plugin and Extension Mechanism – Many of the modules (especially the Extension and JSQLParser ones) implement plugin-based designs that allow the framework to be highly extensible and customize behavior at runtime.
• Auto-Configuration (for Spring Boot integration) – Many Spring modules leverage Spring Boot’s auto-configuration pattern, enabling minimal manual configuration.
• Active Record Pattern – The framework supports a style where domain models are responsible for their own database operations.
• Layered Architecture – At the bottom, you have low-level database connection and MyBatis functions; on top layer, the framework activities (wrappers, interceptors, and auto configuration) build a more feature-rich toolkit.
Relevant Technologies and Frameworks
• MyBatis (as the underlying ORM)
• Spring/Spring Boot for auto-configuration and integration
• Gradle (the build system as seen from the build.gradle files)
• JSQLParser (both versions for SQL parsing support)
• Java and Kotlin (source directories indicate support for both languages)
• Testing frameworks (JUnit, etc.) indicated by the extensive test suite