# tuguangquan/mybatis architecture

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

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

## Overview

##### Main Components (Modules)
- Logging Component:
– Packages: org.apache.ibatis.logging, along with subpackages such as commons, jdbc, jdk14, log4j, log4j2, slf4j, nologging, and stdout.
– Role: Adapts various logging frameworks using the adapter design pattern.

- Exception Management:
– Package: org.apache.ibatis.exceptions.
– Role: Centralizes exception handling across the framework.

- Caching:
– Packages: org.apache.ibatis.cache (core), org.apache.ibatis.cache.decorators, and org.apache.ibatis.cache.impl.
– Role: Provides caching implementations and decorators to improve performance.

- Parsing:
– Package: org.apache.ibatis.parsing.
– Role: Handles xml and token parsing for configuration strings (e.g., resolving ${} expressions). This is critical for reading XML configurations and mapping files.

- Type Handling:
– Package: org.apache.ibatis.type.
– Role: Provides conversion utilities between Java types and JDBC types.

- IO:
– Package: org.apache.ibatis.io.
– Role: Uses class loaders and VFS (virtual file system) to load resources from the classpath or JAR files.

- Reflection:
– Packages: org.apache.ibatis.reflection, and its related subpackages (factory, invoker, property, wrapper).
– Role: Facilitates mapping configuration to Java objects via reflection.

- Data Source Management:
– Packages: org.apache.ibatis.datasource with subpackages jndi, pooled, and unpooled.
– Role: Handles creating and managing database connections.

- Transaction Management:
– Packages: org.apache.ibatis.transaction (with jdbc and managed subpackages).
– Role: Manages transaction boundaries, providing both JDBC and managed transaction strategies.

- Session Management (Core Persistence Layer):
– Package: org.apache.ibatis.session plus subpackages such as defaults.
– Role: Provides the key abstractions for sessions (e.g., SqlSessionFactory, SqlSession) that enable interaction with the database.

- JDBC Utilities:
– Package: org.apache.ibatis.jdbc.
– Role: Supplies helper classes for executing raw JDBC tasks and facilitating testing.

- Builder/Configuration:
– Packages: org.apache.ibatis.builder, including annotation support and XML parsing.
– Role: Assembles and configures the framework by parsing configuration files (mybatis-3-config.dtd, XML mappers), setting up the environment, and establishing connections between modules.

- Mapping:
– Package: org.apache.ibatis.mapping.
– Role: Defines mappings between SQL statements and Java methods/objects (like MappedStatement, ResultMap, etc.).

- Scripting:
– Packages: org.apache.ibatis.scripting along with defaults and xmltags.
– Role: Processes dynamic SQL statements and XML-based scripting for SQL construction.

- Annotations:
– Package: org.apache.ibatis.annotations.
– Role: Provides annotation-based configuration for SQL mappings.

- Binding:
– Package: org.apache.ibatis.binding.
– Role: Dynamically binds mapper interfaces to SQL execution mechanisms.

- Executor:
– Package: org.apache.ibatis.executor along with keygen, loader (and its cglib/javassist subpackages), parameter, result, resultset, and statement subpackages.
– Role: Core of executing SQL statements within the framework. It manages batching, caching at the execution level, and statement routing.

- Plugin:
– Package: org.apache.ibatis.plugin.
– Role: Provides an interceptor mechanism that lets users plug in custom behavior (for example, logging, performance monitoring, etc.) across other components.

##### Relationships and Data Flow
- Configuration and Builders:
– Start by showing a “Configuration Builder” component that uses IO, parsing, and reflection to read and process XML configuration and mapping files.
– This builder creates the SqlSessionFactory which serves as the gateway to most of the framework.

- Session Management and Execution Flow:
– The Executor, in turn, leverages “Mapping” definitions and “Type Handlers” to convert database rows into domain objects.

- Data Source and Transaction:
– Represent the “Data Source” and “Transaction” management modules as separate components feeding into the session layer. They ensure that connection creation, pooling, and commit/rollback operations are managed.

- Cross-Cutting Concerns:
– Show the “Logging” and “Plugin” modules as peripheral or cross-cutting components that interpose on nearly all major interactions (for example, logging SQL execution, intercepting method calls).
– “Caching” is used by both the Executor and at the session level to optimize access to frequently used SQL commands and results.

##### Component Interaction Patterns
– The Builder sends configuration data to create the SqlSessionFactory.
– Rpc-style calls flow from mapper interfaces (bound via the Binding component) to the Executor.
– From Executor → Data Source → Transaction Management for database interaction.
- Dependency directions and intermediary calls (such as Plugin interceptors wrapping calls across session and executor layers).

## Diagram (Mermaid)

```mermaid
flowchart TD
    IO["IO (org.apache.ibatis.io)"]:::support
    Parsing["Parsing (org.apache.ibatis.parsing)"]:::support
    Reflection["Reflection (org.apache.ibatis.reflection)"]:::support
    Annotations["Annotations (org.apache.ibatis.annotations)"]:::support
    Builder["Builder/Configuration (org.apache.ibatis.builder)"]:::support
    SqlSessionFactory["SqlSessionFactory"]:::core
    SqlSession["SqlSession (org.apache.ibatis.session)"]:::core
    Executor["Executor (org.apache.ibatis.executor)"]:::core
    Mapping["Mapping (org.apache.ibatis.mapping)"]:::mapping
    TypeHandling["Type Handling (org.apache.ibatis.type)"]:::mapping
    Scripting["Scripting (org.apache.ibatis.scripting)"]:::mapping
    Caching["Caching (org.apache.ibatis.cache)"]:::cross
    DataSource["Data Source Management (org.apache.ibatis.datasource)"]:::core
    Transaction["Transaction Management (org.apache.ibatis.transaction)"]:::core
    Binding["Binding (org.apache.ibatis.binding)"]:::support
    JDBC["JDBC Utilities (org.apache.ibatis/jdbc)"]:::support
    Logging["Logging (org.apache.ibatis.logging)"]:::cross
    ExceptionManagement["Exception Management (org.apache.ibatis.exceptions)"]:::support
    Plugin["Plugin (org.apache.ibatis.plugin)"]:::cross

    %% Configuration and Builder Layer
    IO -->|"uses"| Builder
    Parsing -->|"uses"| Builder
    Reflection -->|"uses"| Builder
    Annotations -->|"supports"| Builder
    Builder -->|"creates"| SqlSessionFactory
    SqlSessionFactory -->|"instantiates"| SqlSession

    %% Session and Execution Flow
    SqlSession -->|"delegates to"| Executor
    SqlSession -->|"binds mapper"| Binding
    SqlSession -->|"manages"| Transaction
    SqlSession -->|"assists"| JDBC
    Executor -->|"maps queries via"| Mapping
    Executor -->|"converts types using"| TypeHandling
    Executor -->|"processes dynamic SQL via"| Scripting
    Executor -->|"obtains connections from"| DataSource
    DataSource -->|"supplies to"| Transaction

    %% Cross-Cutting Components
    SqlSession -.->|"caching"| Caching
    Executor -.->|"caching"| Caching
    Logging ---|"intercepts"| Builder
    Logging ---|"logs"| SqlSession
    Logging ---|"monitors"| Executor
    Plugin ---|"modifies"| Executor
    ExceptionManagement ---|"handles errors from"| Builder
    ExceptionManagement ---|"handles errors from"| SqlSession
    ExceptionManagement ---|"handles errors from"| Executor

    %% Click Events
    click IO "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/io"
    click Parsing "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/parsing"
    click Reflection "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/reflection"
    click Annotations "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/annotations"
    click Builder "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/builder"
    click SqlSession "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/session"
    click Executor "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/executor"
    click Mapping "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/mapping"
    click TypeHandling "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/type"
    click Scripting "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/scripting"
    click Caching "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/cache"
    click DataSource "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/datasource"
    click Transaction "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/transaction"
    click Binding "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/binding"
    click JDBC "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/jdbc"
    click Logging "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/logging"
    click ExceptionManagement "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/exceptions"
    click Plugin "https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis/plugin"

    %% Styling
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef support fill:#D4EFDF,stroke:#1D8348,stroke-width:2px;
    classDef cross fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef mapping fill:#FADBD8,stroke:#943126,stroke-width:2px;
```
