init commit
This commit is contained in:
192
README.md
192
README.md
@@ -1,10 +1,80 @@
|
||||
# tape-mybatis-generator-plugin
|
||||
> MyBatis 代码生成插件
|
||||
|
||||
## 功能特性
|
||||
|
||||
## Uses
|
||||
in `pom.xml`
|
||||
```
|
||||
本插件为 MyBatis Generator 提供了以下增强功能:
|
||||
|
||||
1. **TapeMybatisGeneratorPlugin** - 扩展 MyBatis Mapper,添加 `selectPrimaryKeyByExample` 方法,支持分页查询主键列表
|
||||
2. **TapeRepositoryGeneratorPlugin** - 为非视图表自动生成 Repository 接口和实现类,提供完整的 CRUD 和软删除功能
|
||||
3. **TapeRepoviewGeneratorPlugin** - 为视图表自动生成 RepoView 接口和实现类,提供查询功能
|
||||
4. **分页支持** - 为 Example 类自动添加分页相关字段和方法(offset、rows、usePage、limit 等)
|
||||
|
||||
## 插件说明
|
||||
|
||||
### TapeMybatisGeneratorPlugin
|
||||
扩展 MyBatis Mapper,添加以下功能:
|
||||
- 在 Mapper 接口中添加 `selectPrimaryKeyByExample` 方法
|
||||
- 在 Mapper XML 中生成对应的 SQL 查询语句
|
||||
- 支持分页查询(通过 `AbstractWithLimitPlugin` 提供)
|
||||
|
||||
### TapeRepositoryGeneratorPlugin
|
||||
为非视图表生成 Repository 层代码:
|
||||
- **接口位置**: `{facadeRepositoryPackage}.I{TableName}Repository`
|
||||
- **实现类位置**: `{domainRepositoryPackage}.{TableName}RepositoryImpl`
|
||||
- **视图表过滤**: 根据 `viewKeyWords` 配置自动识别并跳过视图表
|
||||
|
||||
**生成的方法**:
|
||||
- `trashById(long id)` - 移动到回收站(单个)
|
||||
- `trashAll({Example} example)` - 移动到回收站(批量)
|
||||
- `deleteById(long id, boolean release)` - 删除(单个,支持物理删除)
|
||||
- `deleteAll({Example} example, boolean release)` - 删除(批量,支持物理删除)
|
||||
- `recoverById(long id)` - 从回收站恢复(单个)
|
||||
- `recoverAll({Example} example)` - 从回收站恢复(批量)
|
||||
- `findNoWhereById(long id)` - 查找(不区分有效/回收站)
|
||||
- `findValidById(long id)` - 查找有效记录(单个)
|
||||
- `findTrashById(long id)` - 查找回收站记录(单个)
|
||||
- `findValidOne({Example} example)` - 查找有效记录(单个,支持条件)
|
||||
- `findTrashOne({Example} example)` - 查找回收站记录(单个,支持条件)
|
||||
- `getValidList({Example} example)` - 获取有效记录列表(支持分页)
|
||||
- `getTrashList({Example} example)` - 获取回收站记录列表(支持分页)
|
||||
- `countByValid({Example} example)` - 统计有效记录数
|
||||
- `countByTrash({Example} example)` - 统计回收站记录数
|
||||
- `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值)
|
||||
- `update({Model} record)` - 更新记录(支持乐观锁)
|
||||
|
||||
### TapeRepoviewGeneratorPlugin
|
||||
为视图表生成 RepoView 层代码:
|
||||
- **接口位置**: `{facadeRepoviewPackage}.I{TableName}Repo`
|
||||
- **实现类位置**: `{domainRepoviewPackage}.{TableName}RepoImpl`
|
||||
- **视图表识别**: 仅处理包含 `viewKeyWords` 关键字的表
|
||||
|
||||
**生成的方法**:
|
||||
- `findOne({Example} example)` - 查找单条记录
|
||||
- `getList({Example} example)` - 获取记录列表(支持分页)
|
||||
- `count({Example} example)` - 统计记录数
|
||||
|
||||
### 分页功能
|
||||
通过 `AbstractWithLimitPlugin` 为所有 Example 类添加分页支持:
|
||||
|
||||
**添加的字段**:
|
||||
- `offset` - 偏移量
|
||||
- `rows` - 每页数量
|
||||
- `minPageNum` - 最小页码(默认 1)
|
||||
- `defaultPageSize` - 默认每页数量(默认 20)
|
||||
- `maxPageSize` - 最大每页数量(默认 100)
|
||||
|
||||
**添加的方法**:
|
||||
- `limit(int rows)` - 设置每页数量
|
||||
- `limit(int offset, int rows)` - 设置偏移量和每页数量
|
||||
- `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset)
|
||||
- `getPageNum()` - 获取当前页码
|
||||
- `getPageSize()` - 获取当前每页数量
|
||||
|
||||
## 使用方法
|
||||
### 1. 在 `pom.xml` 中配置插件
|
||||
|
||||
```xml
|
||||
<build>
|
||||
<finalName>application</finalName>
|
||||
<plugins>
|
||||
@@ -37,9 +107,9 @@ in `pom.xml`
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.iqudoo.framework</groupId>
|
||||
<artifactId>tape-mybaits-generator-plugin</artifactId>
|
||||
<artifactId>tape-mybatis-generator-plugin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<systemPath>${project.basedir}/src/lib/tape-mybaits-generator-plugin-1.0-SNAPSHOT.jar</systemPath>
|
||||
<systemPath>${project.basedir}/src/lib/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar</systemPath>
|
||||
<scope>system</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -47,24 +117,58 @@ in `pom.xml`
|
||||
</plugins>
|
||||
</build>
|
||||
```
|
||||
in `mybatis.generator.xml`
|
||||
```
|
||||
<property name="viewKeyWords" value="VIEW_,V_"/>
|
||||
<property name="targetProject" value="src/main/java"/>
|
||||
<property name="modelPackage" value="com.iqudoo.platform.application.database.model"/>
|
||||
<property name="mapperPackage" value="com.iqudoo.platform.application.database.mapper"/>
|
||||
<property name="facadeRepositoryPackage" value="com.iqudoo.platform.application.facade.repository"/>
|
||||
<property name="domainRepositoryPackage" value="com.iqudoo.platform.application.domain.repository"/>
|
||||
<property name="facadeRepoviewPackage" value="com.iqudoo.platform.application.facade.repoview"/>
|
||||
<property name="domainRepoviewPackage" value="com.iqudoo.platform.application.domain.repoview"/>
|
||||
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeMybatisGeneratorPlugin"/>
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeRepositoryGeneratorPlugin"/>
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeRepoviewGeneratorPlugin"/>
|
||||
### 2. 在 `mybatis.generator.xml` 中配置插件
|
||||
|
||||
```xml
|
||||
<context id="Mysql" targetRuntime="MyBatis3">
|
||||
<!-- 配置属性 -->
|
||||
<property name="viewKeyWords" value="VIEW_,V_"/>
|
||||
<property name="targetProject" value="src/main/java"/>
|
||||
<property name="modelPackage" value="com.iqudoo.platform.application.database.model"/>
|
||||
<property name="mapperPackage" value="com.iqudoo.platform.application.database.mapper"/>
|
||||
<property name="facadeRepositoryPackage" value="com.iqudoo.platform.application.facade.repository"/>
|
||||
<property name="domainRepositoryPackage" value="com.iqudoo.platform.application.domain.repository"/>
|
||||
<property name="facadeRepoviewPackage" value="com.iqudoo.platform.application.facade.repoview"/>
|
||||
<property name="domainRepoviewPackage" value="com.iqudoo.platform.application.domain.repoview"/>
|
||||
<property name="snowflakeUtilClass" value="com.iqudoo.framework.tape.modules.utils.SnowflakeUtil"/>
|
||||
<property name="snowflakeUtilGenId" value="SnowflakeUtil.nextId()"/>
|
||||
|
||||
<!-- 添加插件 -->
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeMybatisGeneratorPlugin"/>
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeRepositoryGeneratorPlugin"/>
|
||||
<plugin type="com.iqudoo.framework.mybatis.TapeRepoviewGeneratorPlugin"/>
|
||||
|
||||
<!-- 其他配置... -->
|
||||
</context>
|
||||
```
|
||||
|
||||
## Table Template
|
||||
```
|
||||
### 3. 配置参数说明
|
||||
|
||||
| 参数名 | 说明 | 默认值 | 必需 |
|
||||
|--------|---------------------|--------------------------------------------------------|------|
|
||||
| `viewKeyWords` | 视图表关键字(逗号分隔,不区分大小写) | `VIEW_,V_` | 否 |
|
||||
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
|
||||
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
|
||||
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
|
||||
| `facadeRepositoryPackage` | Repository 接口的包路径 | `com.iqudoo.platform.application.facade.repository` | 否 |
|
||||
| `domainRepositoryPackage` | Repository 实现类的包路径 | `com.iqudoo.platform.application.domain.repository` | 否 |
|
||||
| `facadeRepoviewPackage` | RepoView 接口的包路径 | `com.iqudoo.platform.application.facade.repoview` | 否 |
|
||||
| `snowflakeUtilClass` | 雪花算法ID生成工具类 | `com.iqudoo.framework.tape.modules.utils.SnowflakeUtil` | 否 |
|
||||
| `snowflakeUtilGenId` | 雪花算法ID生成方法 | `SnowflakeUtil.nextId()` | 否 |
|
||||
|
||||
**视图表识别规则**:
|
||||
- 表名包含 `viewKeyWords` 中任一关键字的表将被识别为视图表,大小写不敏感
|
||||
- 视图表会生成 RepoView,不会生成 Repository
|
||||
- 非视图表会生成 Repository,不会生成 RepoView
|
||||
|
||||
## 数据库表结构要求
|
||||
|
||||
### 标准表结构模板
|
||||
|
||||
为了使用完整的 Repository 功能(软删除、回收站等),表结构需要包含以下标准字段:
|
||||
|
||||
```sql
|
||||
DROP TABLE IF EXISTS `your_table_name`;
|
||||
CREATE TABLE `your_table_name` (
|
||||
`guid` bigint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'GUID',
|
||||
@@ -80,9 +184,24 @@ CREATE TABLE `your_table_name` (
|
||||
PRIMARY KEY (`guid`) USING BTREE
|
||||
) ENGINE = InnoDB COMMENT = '你的表格备注' ROW_FORMAT = Dynamic;
|
||||
```
|
||||
in `mybatis.generator.xml`
|
||||
```
|
||||
<!-- your_table_name -->
|
||||
|
||||
**必需字段说明**:
|
||||
- `guid` - 主键,类型为 `bigint UNSIGNED`
|
||||
- `is_hidden` - 隐藏标志,用于回收站功能
|
||||
- `is_delete` - 删除标志,用于软删除功能
|
||||
- `delete_token` - 删除令牌,用于标识删除状态
|
||||
- `data_version` - 数据版本,用于乐观锁
|
||||
- `create_time` - 创建时间,自动设置
|
||||
- `update_time` - 更新时间,自动更新
|
||||
|
||||
### 视图表结构
|
||||
|
||||
视图表不需要上述标准字段,只需要包含业务字段即可。
|
||||
|
||||
### 在 `mybatis.generator.xml` 中配置表
|
||||
|
||||
```xml
|
||||
<!-- 标准表配置 -->
|
||||
<table tableName="your_table_name"
|
||||
domainObjectName="YourTableName"
|
||||
enableInsert="true"
|
||||
@@ -95,4 +214,31 @@ in `mybatis.generator.xml`
|
||||
selectByExampleQueryId="false">
|
||||
<property name="useActualColumnNames" value="false"/>
|
||||
</table>
|
||||
|
||||
<!-- 视图表配置(表名包含 VIEW_ 或 V_ 前缀,大小写不敏感) -->
|
||||
<table tableName="v_your_view_name"
|
||||
domainObjectName="ViewYourViewName"
|
||||
enableInsert="false"
|
||||
enableDeleteByPrimaryKey="false"
|
||||
enableUpdateByPrimaryKey="false"
|
||||
enableCountByExample="true"
|
||||
enableUpdateByExample="false"
|
||||
enableDeleteByExample="false"
|
||||
enableSelectByExample="true"
|
||||
selectByExampleQueryId="false">
|
||||
<property name="useActualColumnNames" value="false"/>
|
||||
<generatedKey column="guid" sqlStatement="JDBC" identity="false"/>
|
||||
</table>
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **主键要求**:表必须有一个主键字段,且字段名为 `guid`,类型为 `bigint UNSIGNED`
|
||||
2. **视图表识别**:视图表通过表名中的关键字识别,默认关键字为 `view_` 和 `v_` 不区分大小写
|
||||
3. **分页功能**:所有 Example 类都自动包含分页功能,可通过 `usePage()` 方法使用
|
||||
4. **乐观锁**:更新操作使用 `data_version` 字段实现乐观锁,更新时需要传入正确的版本号
|
||||
5. **软删除**:删除操作默认是软删除(设置 `is_delete` 标志),可通过 `release=true` 参数执行物理删除
|
||||
6. **回收站**:通过 `is_hidden` 字段实现回收站功能,`trash` 方法将记录移动到回收站,`recover` 方法恢复记录
|
||||
7. **自动字段**:插入记录时,插件会自动设置 `guid`(使用雪花算法)、`is_delete`、`is_hidden`、`delete_token`、`data_version`、`create_time`、`update_time` 等字段
|
||||
8. **BLOB 字段支持**:如果表包含 BLOB 字段,插件会自动使用 `selectByExampleWithBLOBs` 和 `updateByExampleWithBLOBs` 方法
|
||||
9. **MyBatis Generator 版本**:本插件基于 MyBatis Generator 1.4.1 开发,建议使用 1.4.0 或更高版本
|
||||
Reference in New Issue
Block a user