How tuguangquan/mybatis works
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).