Data Access Layer (MyBatis)
Relevant source files
The following files were used as context for generating this wiki page:- zheng-common/src/main/java/com/zheng/common/plugin/PaginationPlugin.java
- zheng-common/src/main/resources/mybatis-config.xml
- zheng-upms/zheng-upms-rpc-service/src/main/java/com/zheng/upms/dao/mapper/UpmsUserMapper.xml
- zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java
- zheng-common/pom.xml
- zheng-cms/zheng-cms-rpc-service/src/main/java/com/zheng/cms/dao/mapper/CmsArticleMapper.xml
- zheng-cms/zheng-cms-dao/src/main/java/com/zheng/cms/dao/model/CmsTopicExample.java
- zheng-upms/zheng-upms-dao/pom.xml
- zheng-cms/zheng-cms-rpc-service/src/main/java/com/zheng/cms/dao/mapper/CmsPageMapper.xml
The zheng project utilizes MyBatis as its primary data access framework to interact with the underlying relational database. This layer is responsible for mapping Java objects to database records and executing SQL statements. The architecture heavily relies on automated code generation via MyBatis Generator to create a consistent and maintainable set of DAOs (Data Access Objects), models, and mapper XML files.
Key features of this layer include integration with the Spring framework, a standardized approach to CRUD operations using generated "Example" classes for dynamic queries, and performance enhancements through pagination and second-level caching with Ehcache. The modular design ensures that each microservice (e.g., zheng-cms, zheng-upms) has its own dedicated data access module.
Core Dependencies and Configuration
The data access layer is built upon a set of core Maven dependencies defined primarily in the zheng-common module. These libraries provide the foundational functionality for MyBatis, its integration with Spring, and additional plugins.
Sources: zheng-common/pom.xml:29-49, 110-129
Key Dependencies
The following table summarizes the main libraries used for data persistence:
| Group ID | Artifact ID | Version | Description |
|---|---|---|---|
org.mybatis | mybatis | 3.4.2 | The core MyBatis SQL mapper framework. |
org.mybatis | mybatis-spring | 1.3.1 | Integrates MyBatis with the Spring Framework. |
com.github.pagehelper | pagehelper | 5.0.1 | A plugin for MyBatis that simplifies pagination. |
org.mybatis.generator | mybatis-generator-core | 1.3.5 | A tool for automatically generating MyBatis artifacts. |
org.mybatis | mybatis-ehcache | 1.0.0 | An adapter for using Ehcache as a second-level cache. |
mysql | mysql-connector-java | 5.1.34 | The JDBC driver for MySQL. |
com.alibaba | druid | 1.0.14 | A high-performance JDBC connection pool. |
Sources: zheng-common/pom.xml:29-49, 110-129
Caching Configuration
The system employs Ehcache for second-level caching to reduce database load and improve query performance for frequently accessed data. This is enabled directly within the mapper XML files.
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
This configuration line instructs MyBatis to use a logging-capable Ehcache adapter for the mapper namespace, caching the results of select statements.
Sources: zheng-cms/zheng-cms-rpc-service/src/main/java/com/zheng/cms/dao/mapper/CmsArticleMapper.xml:465, zheng-cms/zheng-cms-rpc-service/src/main/java/com/zheng/cms/dao/mapper/CmsPageMapper.xml:298
Automated Code Generation
A significant part of the data access layer is automatically generated using MyBatis Generator and custom Velocity templates. The MybatisGeneratorUtil class orchestrates this process, creating model classes, mapper interfaces, XML mapping files, and even basic service layer implementations.
Sources: zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java
Generation Process
The generation process is triggered by executing the MybatisGeneratorUtil.generator method. It follows a series of automated steps to scaffold the persistence and service layers.
The diagram below illustrates the code generation workflow.
MybatisGeneratorUtil
This utility class is the heart of the code generation system. Its main generator method accepts database and project configuration to produce the required files.
| Parameter | Description |
|---|---|
jdbcDriver | The JDBC driver class name. |
jdbcUrl | The database connection URL. |
jdbcUsername | The database username. |
jdbcPassword | The database password (AES encrypted). |
module | The target project module (e.g., "zheng-cms"). |
database | The name of the database schema. |
tablePrefix | The prefix of tables to generate code for (e.g., "cms_"). |
packageName | The base Java package name (e.g., "com.zheng.cms"). |
lastInsertIdTables | A map to handle tables that require selectKey for last insert ID. |
Sources: zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java:41-52
Maven Plugin Integration
The code generation can also be invoked via the mybatis-generator-maven-plugin. This is configured in the pom.xml of each dao module (e.g., zheng-upms-dao, zheng-cms-dao).
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>com.zheng</groupId>
<artifactId>zheng-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
This plugin configuration ensures that the generator can be run as part of the Maven build lifecycle, and it depends on zheng-common which contains custom generator plugins.
Sources: zheng-upms/zheng-upms-dao/pom.xml:34-48, zheng-cms/zheng-cms-dao/pom.xml:34-48
Mapper XML and Querying
The generated XML files contain the SQL statements and mappings between database columns and Java object properties.
Mapper XML Structure
A typical mapper XML file, such as CmsArticleMapper.xml, consists of several key parts:
resultMap: Defines the mapping from a table's columns to a model object's fields.sqlfragments: Reusable SQL snippets, likeBase_Column_Listfor all columns andExample_Where_Clausefor dynamic WHERE conditions.- CRUD Statements: Standard
select,insert,update, anddeletestatements that use the model and "Example" classes as parameters.
The diagram below shows the entity relationship for the cms_article table based on its mapper.
Dynamic Queries with "Example" Classes
MyBatis Generator creates "Example" classes (e.g., CmsTopicExample.java) to facilitate the creation of dynamic WHERE clauses in a type-safe manner. These classes allow developers to build complex queries programmatically without writing raw SQL.
An Example class contains an inner Criteria class, which provides methods to append conditions to the query (e.g., andTitleEqualTo(), andCtimeBetween()). Multiple Criteria objects can be combined with an OR operator.
The following class diagram shows the structure of a typical "Example" class.
The following sequence diagram illustrates a typical data request flow using the generated artifacts.
This flow demonstrates how a service implementation uses the generated mapper interface and an Example object to retrieve data from the database.