From d2d542d724141e71aa8194c2f4130cdd6fdaf339 Mon Sep 17 00:00:00 2001 From: gitee-bot Date: Fri, 22 Aug 2025 06:22:35 +0000 Subject: [PATCH] Update README.md --- README.en.md | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 README.en.md create mode 100644 README.md diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000..f63e641 --- /dev/null +++ b/README.en.md @@ -0,0 +1,98 @@ +# lzh-21-db-spring-boot-starter + +## Introduction +`lzh-21-db-spring-boot-starter` is a Spring Boot-based starter module designed to simplify database configuration and operations. This module provides the following features: + +- Auto-fill functionality: supports automatic filling of time fields during insert and update operations. +- Dynamic data source configuration: supports switching and configuring multiple data sources. +- ID generator: generates unique IDs based on millisecond timestamps. +- Transaction template: supports dynamic transaction management. + +## Features +- **Auto-fill**: Configure auto-fill functionality through `MybatisPlusAutoConfig`, using `DateMetaObjectHandler` and `PrimaryMetaObjectHandler` to implement the auto-fill logic. +- **Dynamic Data Source**: `LzhDynamicDataSourceAutoConfiguration` provides dynamic data source configuration and supports excluding specific auto-configuration classes. +- **Unique ID Generator**: `MillisecondIdGenerator` provides a unique ID generation strategy based on millisecond timestamps. +- **Transaction Template**: `DynamicTransactionTemplate` provides dynamic transaction execution methods, allowing transactions to be executed on different data sources. +- **Timestamp and Sequence Generation**: `TimeUniqueIdUtil` provides methods for generating timestamps and sequence numbers, supporting concurrency-safe time-based unique IDs. + +## Installation and Configuration + +### Maven Dependency +Ensure you include the following dependency in your `pom.xml` file: + +```xml + + com.lzh + lzh-21-db-spring-boot-starter + 1.0.0 + +``` + +### Configuration Instructions +Configure the following properties in the `application.yml` or `application.properties` file: + +#### Dynamic Data Source Configuration +```yaml +spring: + datasource: + dynamic: + enabled: true +``` + +#### Auto-fill Configuration +```yaml +mybatis-plus: + auto-fill: + enable: true + enableInsertFill: true + enableUpdateFill: true + createTimeField: createTime + updateTimeField: updateTime + createUidField: createUid + updateUidField: updateUid +``` + +## Usage Examples + +### Auto-fill Fields +Use auto-fill fields in your entity classes, for example: +```java +public class ExampleEntity { + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; +} +``` + +### Using Dynamic Data Sources +Use the `@DS` annotation to switch data sources: +```java +@DS("slave") +public List queryFromSlave() { + return exampleEntityMapper.selectList(null); +} +``` + +### Generating Unique IDs +Use `MillisecondIdGenerator` to generate unique IDs where needed: +```java +MillisecondIdGenerator idGenerator = new MillisecondIdGenerator(); +Long id = idGenerator.nextId(null); +``` + +### Dynamic Transaction Management +Use `DynamicTransactionTemplate` to execute transaction operations: +```java +dynamicTransactionTemplate.execute(status -> { + // Execute database operations + return null; +}, "dataSourceName"); +``` + +## Contributing +Contributions and improvement suggestions are welcome. Please fork the repository and submit a Pull Request. + +## Open Source License +This project is released under the [MIT License](https://opensource.org/licenses/MIT). \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7298dc --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ + + +# lzh-21-db-spring-boot-starter + +## 简介 +`lzh-21-db-spring-boot-starter` 是一个基于 Spring Boot 的 starter 模块,用于简化数据库配置和操作。该模块提供以下功能: + +- 自动填充功能:支持插入和更新操作时自动填充时间字段。 +- 动态数据源配置:支持多数据源切换与配置。 +- ID 生成器:基于毫秒时间戳生成唯一 ID。 +- 事务模板:支持动态事务管理。 + +## 功能特性 +- **自动填充**:通过 `MybatisPlusAutoConfig` 配置自动填充功能,使用 `DateMetaObjectHandler` 和 `PrimaryMetaObjectHandler` 实现自动填充逻辑。 +- **动态数据源**:`LzhDynamicDataSourceAutoConfiguration` 提供动态数据源配置,支持排除特定的自动配置类。 +- **唯一 ID 生成器**:`MillisecondIdGenerator` 提供基于毫秒时间戳的唯一 ID 生成策略。 +- **事务模板**:`DynamicTransactionTemplate` 提供动态事务执行方法,允许在不同数据源上执行事务。 +- **时间戳与序列生成**:`TimeUniqueIdUtil` 提供时间戳和序列号的生成方法,支持并发安全的时间唯一 ID。 + +## 安装配置 + +### Maven 依赖 +确保在你的 `pom.xml` 文件中引入以下依赖: + +```xml + + com.lzh + lzh-21-db-spring-boot-starter + 1.0.0 + +``` + +### 配置说明 +在 `application.yml` 或 `application.properties` 文件中配置以下属性: + +#### 动态数据源配置 +```yaml +spring: + datasource: + dynamic: + enabled: true +``` + +#### 自动填充配置 +```yaml +mybatis-plus: + auto-fill: + enable: true + enableInsertFill: true + enableUpdateFill: true + createTimeField: createTime + updateTimeField: updateTime + createUidField: createUid + updateUidField: updateUid +``` + +## 使用示例 + +### 自动填充字段 +在实体类中使用自动填充字段,例如: +```java +public class ExampleEntity { + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; +} +``` + +### 动态数据源使用 +使用 `@DS` 注解切换数据源: +```java +@DS("slave") +public List queryFromSlave() { + return exampleEntityMapper.selectList(null); +} +``` + +### 生成唯一 ID +在需要生成唯一 ID 的地方使用 `MillisecondIdGenerator`: +```java +MillisecondIdGenerator idGenerator = new MillisecondIdGenerator(); +Long id = idGenerator.nextId(null); +``` + +### 动态事务管理 +使用 `DynamicTransactionTemplate` 执行事务操作: +```java +dynamicTransactionTemplate.execute(status -> { + // 执行数据库操作 + return null; +}, "dataSourceName"); +``` + +## 贡献代码 +欢迎贡献代码和改进建议。请 fork 仓库并提交 Pull Request。 + +## 开源协议 +本项目基于 [MIT License](https://opensource.org/licenses/MIT) 协议发布。 \ No newline at end of file -- Gitee