# baomidou/mybatis-plus architecture

> Architecture diagram of the GitHub repository baomidou/mybatis-plus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/baomidou/mybatis-plus
- Repository: https://github.com/baomidou/mybatis-plus

## Overview

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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Framework Core Modules
    subgraph "Framework Core"
        MCore("MyBatis-Plus Core"):::core
        Annotation("Annotation Module"):::core
        Extension("Extension Module"):::extension
        CodeGen("Code Generator"):::core
        JSQLParser("JSQLParser Support"):::extension
    end

    %% Integration Modules
    subgraph "Integration Modules"
        Spring("MyBatis-Plus Spring"):::spring
        BootStarter("Spring Boot Starter"):::spring
        BootAutoconf("Spring Boot Autoconfigure"):::spring
        Boot3("Spring Boot 3 Starter"):::spring
    end

    %% Test and Example Components
    Test("Test & Examples"):::test

    %% Dependencies among Framework Core Modules
    Annotation -->|"dependsOn"| MCore
    Extension -->|"extends"| MCore
    CodeGen -->|"uses"| MCore
    CodeGen -->|"uses"| Annotation
    JSQLParser -->|"enhances"| MCore

    %% Integration relationships
    Spring -->|"integrates"| MCore
    Spring -->|"integrates"| Extension
    BootStarter -->|"integrates"| MCore
    BootAutoconf -->|"configures"| MCore
    Boot3 -->|"configures"| MCore

    %% Test relationships (dotted lines)
    MCore ---|"tests"| Test
    Annotation ---|"tests"| Test
    Extension ---|"tests"| Test
    CodeGen ---|"tests"| Test
    JSQLParser ---|"tests"| Test
    Spring ---|"tests"| Test
    BootStarter ---|"tests"| Test
    BootAutoconf ---|"tests"| Test
    Boot3 ---|"tests"| Test

    %% Click Events
    click MCore "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-core"
    click Annotation "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-annotation"
    click Extension "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-extension"
    click CodeGen "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-generator"
    click JSQLParser "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-jsqlparser-support"
    click Spring "https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-spring"
    click BootStarter "https://github.com/baomidou/mybatis-plus/tree/3.0/spring-boot-starter/mybatis-plus-boot-starter"
    click BootAutoconf "https://github.com/baomidou/mybatis-plus/tree/3.0/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure"
    click Boot3 "https://github.com/baomidou/mybatis-plus/tree/3.0/spring-boot-starter/mybatis-plus-spring-boot3-starter"

    %% Styles
    classDef core fill:#f9e79f,stroke:#d35400,stroke-width:2px,color:#000;
    classDef extension fill:#aed6f1,stroke:#3498db,stroke-width:2px,color:#000;
    classDef spring fill:#d5f5e3,stroke:#27ae60,stroke-width:2px,color:#000;
    classDef test fill:#fadbd8,stroke:#e74c3c,stroke-width:2px,color:#000;
```
