diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..751b30cbd5925479c9a5cec165596f7cd20235ea --- /dev/null +++ b/README.en.md @@ -0,0 +1,132 @@ +# lzh-1.8-db-spring-boot-starter + +This is a starter project based on Spring Boot, designed to provide convenient auto-configuration and utility classes for projects using MyBatis Plus and dynamic data sources. It is suitable for scenarios requiring multi-data source switching, automatic field filling, unique ID generation, and more. + +--- + +## Features + +- **Dynamic Data Source Support**: Provides annotation-based dynamic data source switching via `LzhDynamicDataSourceAutoConfiguration`. +- **Auto Fill Functionality**: Supports automatic filling of time, user ID, and other fields during insertions and updates. +- **Unique ID Generator**: Provides a millisecond-level unique ID generator based on timestamps. +- **Transaction Template**: Encapsulates a dynamic transaction management template, supporting transaction execution on a specified data source. +- **Flexible Configuration**: Allows flexible configuration of auto-fill fields, data sources, and other parameters via `application.yml` or `application.properties`. + +--- + +## Quick Start + +### 1. Add Dependency + +Add the following dependency to your `pom.xml` file: + +```xml + + com.lzh + lzh-1.8-db-spring-boot-starter + 1.0.0 + +``` + +### 2. Enable Dynamic Data Source (Optional) + +Add the following annotation to the main application class to enable dynamic data sources: + +```java +@EnableDynamicDataSource +``` + +### 3. Configure Auto Fill Fields (Optional) + +Configure auto-fill fields in `application.yml`: + +```yaml +mybatis-plus: + auto-fill: + enable: true + create-time-field: createTime + update-time-field: updateTime + create-uid-field: createUid + update-uid-field: updateUid +``` + +### 4. Use Unique ID Generator + +Inject and use `TimeUniqueIdUtil`: + +```java +@Autowired +private TimeUniqueIdUtil timeUniqueIdUtil; + +String id = timeUniqueIdUtil.currentTimestampString(true, true); +``` + +--- + +## Usage Examples + +### Dynamic Data Source Switching + +Use the `@DS` annotation to switch data sources: + +```java +@DS("slave1") +public List queryFromSlave() { + return userMapper.selectList(null); +} +``` + +### Auto Fill Time Fields + +Time fields do not need to be manually set in the entity class; the framework will automatically fill them: + +```java +public class User { + private String createTime; + private String updateTime; +} +``` + +### Execute Dynamic Transaction + +Use `DynamicTransactionTemplate` to execute a transaction on a specified data source: + +```java +@Autowired +private DynamicTransactionTemplate dynamicTransactionTemplate; + +public void doInTransaction() { + dynamicTransactionTemplate.execute(status -> { + // Database operations + return null; + }, "slave1"); +} +``` + +--- + +## Configuration Options + +| Configuration | Default Value | Description | +|---------------|---------------|-------------| +| `mybatis-plus.auto-fill.enable` | `true` | Whether to enable auto-fill functionality | +| `mybatis-plus.auto-fill.create-time-field` | `createTime` | Creation time field to auto-fill during insertions | +| `mybatis-plus.auto-fill.update-time-field` | `updateTime` | Update time field to auto-fill during updates | +| `mybatis-plus.auto-fill.create-uid-field` | `createUid` | Creation user ID field to auto-fill during insertions | +| `mybatis-plus.auto-fill.update-uid-field` | `updateUid` | Update user ID field to auto-fill during updates | + +--- + +## Contribution Guide + +Pull requests and issues are welcome. Please follow these guidelines: + +- Ensure all tests pass before submitting code. +- Provide clear reproduction steps and environment details when submitting issues. +- Maintain consistent code style and follow Java coding conventions. + +--- + +## License + +This project is open-sourced 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 0000000000000000000000000000000000000000..946fd0cdde2c38923f7a78174270f24cc3c9bb8b --- /dev/null +++ b/README.md @@ -0,0 +1,132 @@ +# lzh-1.8-db-spring-boot-starter + +这是一个基于 Spring Boot 的 starter 项目,旨在为使用 MyBatis Plus 和动态数据源的项目提供便捷的自动配置和工具类。适用于需要多数据源切换、自动填充字段、唯一 ID 生成等场景。 + +--- + +## 特性 + +- **动态数据源支持**:通过 `LzhDynamicDataSourceAutoConfiguration` 提供基于注解的动态数据源切换支持。 +- **自动填充功能**:支持在插入和更新时自动填充时间、用户 ID 等字段。 +- **唯一 ID 生成器**:提供基于时间戳的毫秒级唯一 ID 生成器。 +- **事务模板**:封装了动态事务管理模板,支持指定数据源的事务执行。 +- **灵活配置**:通过 `application.yml` 或 `application.properties` 可灵活配置自动填充字段、数据源等参数。 + +--- + +## 快速开始 + +### 1. 添加依赖 + +将以下依赖添加到你的 `pom.xml` 文件中: + +```xml + + com.lzh + lzh-1.8-db-spring-boot-starter + 1.0.0 + +``` + +### 2. 启用动态数据源(可选) + +在主应用类上添加以下注解以启用动态数据源: + +```java +@EnableDynamicDataSource +``` + +### 3. 配置自动填充字段(可选) + +在 `application.yml` 中配置自动填充字段: + +```yaml +mybatis-plus: + auto-fill: + enable: true + create-time-field: createTime + update-time-field: updateTime + create-uid-field: createUid + update-uid-field: updateUid +``` + +### 4. 使用唯一 ID 生成器 + +注入并使用 `TimeUniqueIdUtil`: + +```java +@Autowired +private TimeUniqueIdUtil timeUniqueIdUtil; + +String id = timeUniqueIdUtil.currentTimestampString(true, true); +``` + +--- + +## 使用示例 + +### 动态数据源切换 + +使用 `@DS` 注解切换数据源: + +```java +@DS("slave1") +public List queryFromSlave() { + return userMapper.selectList(null); +} +``` + +### 自动填充时间字段 + +实体类中无需手动设置时间字段,框架会自动填充: + +```java +public class User { + private String createTime; + private String updateTime; +} +``` + +### 执行动态事务 + +使用 `DynamicTransactionTemplate` 执行指定数据源的事务: + +```java +@Autowired +private DynamicTransactionTemplate dynamicTransactionTemplate; + +public void doInTransaction() { + dynamicTransactionTemplate.execute(status -> { + // 数据库操作 + return null; + }, "slave1"); +} +``` + +--- + +## 配置项说明 + +| 配置项 | 默认值 | 说明 | +|--------|--------|------| +| `mybatis-plus.auto-fill.enable` | `true` | 是否启用自动填充功能 | +| `mybatis-plus.auto-fill.create-time-field` | `createTime` | 插入时自动填充的创建时间字段 | +| `mybatis-plus.auto-fill.update-time-field` | `updateTime` | 更新时自动填充的更新时间字段 | +| `mybatis-plus.auto-fill.create-uid-field` | `createUid` | 插入时自动填充的创建用户 ID 字段 | +| `mybatis-plus.auto-fill.update-uid-field` | `updateUid` | 更新时自动填充的更新用户 ID 字段 | + +--- + +## 贡献指南 + +欢迎提交 Pull Request 和 Issue。请遵循以下规范: + +- 提交代码前请确保通过所有测试。 +- 提交 Issue 时请提供清晰的复现步骤和环境信息。 +- 保持代码风格统一,遵循 Java 编码规范。 + +--- + +## 许可证 + +本项目基于 [MIT License](https://opensource.org/licenses/MIT) 开源。 \ No newline at end of file