Module: Common Utilities
Relevant source files
The following files were used as context for generating this wiki page:- zheng-common/src/main/java/com/zheng/common/base/BaseServiceImpl.java
- zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java
- zheng-common/src/main/java/com/zheng/common/db/DynamicDataSource.java
- zheng-common/pom.xml
- zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java
- zheng-upms/zheng-upms-common/pom.xml
- zheng-cms/zheng-cms-common/pom.xml
The zheng-common module serves as the foundational library for the entire zheng project. It encapsulates a wide range of shared components, including base classes for services, database utilities, caching mechanisms, and core framework dependencies. Its primary purpose is to promote code reuse, establish a consistent architectural pattern, and manage the project's core technology stack in a centralized location.
This module is a critical dependency for almost all other modules within the zheng ecosystem, such as zheng-upms, zheng-cms, and zheng-api. By providing a common set of tools and abstractions, it significantly reduces boilerplate code and simplifies development across the different sub-projects.
Core Dependencies
The zheng-common module defines and manages the versions of all major frameworks and libraries used throughout the application. This ensures version consistency and simplifies dependency management in downstream modules.
The following diagram illustrates how other common modules depend on zheng-common.
Key Libraries and Frameworks
The table below lists the primary technologies integrated into the zheng project via the zheng-common module.
| Group ID | Artifact ID | Version | Description |
|---|---|---|---|
org.springframework | spring-webmvc, spring-jdbc, etc. | 4.3.20.RELEASE | Core application framework for dependency injection, web MVC, and data access. |
org.mybatis | mybatis, mybatis-spring | 3.4.2, 1.3.1 | Persistence framework for mapping Java objects to SQL statements. |
com.github.pagehelper | pagehelper | 5.0.1 | A MyBatis pagination plugin. |
org.apache.shiro | shiro-spring, shiro-web | 1.3.2 | Security framework for authentication and authorization. |
com.alibaba | dubbo | 2.5.6 | A high-performance RPC framework. |
redis.clients | jedis | 2.9.0 | Java client for Redis. |
org.springframework.data | spring-data-redis | 1.7.5.RELEASE | Spring Data integration for Redis. |
org.springframework.session | spring-session-data-redis | 1.3.0.RELEASE | Provides Redis-backed HttpSession implementation. |
com.alibaba | druid | 1.0.14 | A high-performance JDBC connection pool. |
io.springfox | springfox-swagger2 | 2.4.0 | API documentation generation with Swagger. |
org.apache.velocity | velocity | 1.7 | A Java-based template engine used for code generation. |
Sources: zheng-common/pom.xml:22-261
Data Persistence Layer
The common module provides a robust and reusable data access layer built on Spring and MyBatis. It features a generic base service implementation, a dynamic data source router, and a code generation utility.
Generic Base Service (BaseServiceImpl)
To reduce boilerplate code for standard CRUD (Create, Read, Update, Delete) operations, the module provides a generic BaseServiceImpl. Services across the application can extend this class to inherit common data manipulation methods.
This diagram illustrates the class structure of BaseServiceImpl.
Sources: [zheng-common/src/main/java/com/zheng/common/base/BaseServiceImpl.java]
The implementation uses Java generics (<Mapper, Model, Example>) to work with any MyBatis-generated mapper and model. The specific mapper is injected via @Autowired, allowing the base methods to delegate calls to the appropriate MyBatis mapper interface.
Sources: zheng-common/src/main/java/com/zheng/common/base/BaseServiceImpl.java:23-74
Dynamic Data Source (DynamicDataSource)
The DynamicDataSource class enables the application to switch between multiple databases at runtime. It extends Spring's AbstractRoutingDataSource and uses a ThreadLocal variable to determine which data source to use for the current thread. This is particularly useful for implementing read/write splitting or multi-tenant architectures.
The data source selection flow is as follows:
The key for the data source is managed by the DynamicDataSourceHolder class, which provides static methods to set, get, and clear the data source key from the ThreadLocal.
Sources: zheng-common/src/main/java/com/zheng/common/db/DynamicDataSource.java:28-44
MyBatis Code Generator (MybatisGeneratorUtil)
The module includes a powerful utility for scaffolding the persistence layer. The MybatisGeneratorUtil class automates the generation of MyBatis model classes, mapper interfaces, XML mapping files, and even service-layer interfaces and implementations.
The generation process follows these steps:
This utility uses a Velocity template (generatorConfig.vm) to create the generatorConfig.xml file dynamically based on the tables found in the specified database schema. It then invokes the MyBatis Generator library to create the DAO and model files. Finally, it uses additional Velocity templates (Service.vm, ServiceImpl.vm) to generate the corresponding service layer.
Sources: [zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java]
The main generator method accepts the following parameters:
| 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 name of the Maven 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 configure selectKey for tables that need it. |
Sources: zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java:41-52
Caching Utilities
Redis Utility (RedisUtil)
RedisUtil is a static utility class that provides a simple interface for interacting with a Redis server. It abstracts away the complexities of the underlying spring-data-redis library. The class must be initialized by the Spring Framework by calling the static setRedisTemplate method, which is typically done in a configuration class.
The following sequence diagram shows a typical cache-aside pattern where a service uses RedisUtil to retrieve data.
Sources: [zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java]
Key Methods
The utility provides basic Redis operations:
| Method | Description |
|---|---|
get(key) | Retrieves a value from Redis by its key. |
set(key, value) | Sets a key-value pair with no expiration. |
set(key, value, expire) | Sets a key-value pair with a specified expiration time in seconds. |
delete(key) | Deletes a key from Redis. |
Sources: zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java:23-74
Conclusion
The zheng-common module is the cornerstone of the zheng project. It provides a standardized and efficient foundation for building microservices by offering reusable components for data persistence with MyBatis, dynamic data source routing, Redis-based caching, and automated code generation. This centralization of common functionality and dependencies ensures consistency, reduces development overhead, and establishes a clear architectural pattern for all other modules in the system.