Jeremy Liang 4c86f8dd1d init commit
2026-02-04 04:54:55 +08:00
2026-02-04 04:54:55 +08:00
2026-02-04 04:10:53 +08:00
2026-02-04 03:21:09 +08:00
2026-02-04 04:47:08 +08:00

tape-mybatis-generator-plugin

MyBatis 代码生成插件

功能特性

本插件为 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) - 从回收站恢复(批量)
  • findAnyById(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 中配置插件

<build>
    <finalName>application</finalName>
    <plugins>
        <!-- your other plugins -->
        <!-- add mybatis plugin -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <configurationFile>src/main/resources/mybatis.generator.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
                    <version>8.0.33</version>
                </dependency>
                <dependency>
                    <groupId>com.iqudoo.framework</groupId>
                    <artifactId>tape-mybatis-generator-plugin</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <systemPath>${project.basedir}/src/lib/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar</systemPath>
                    <scope>system</scope>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

2. 在 mybatis.generator.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>

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 功能(软删除、回收站等),表结构需要包含以下标准字段:

DROP TABLE IF EXISTS `your_table_name`;
CREATE TABLE `your_table_name`  (
  `guid` bigint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'GUID',
  -- ----------------------------
  -- add your table other table field
  -- ----------------------------
  `is_hidden` int(0) NOT NULL DEFAULT 0 COMMENT '隐藏标志',
  `is_delete` int(0) NOT NULL DEFAULT 0 COMMENT '删除标志',
  `delete_token` varchar(32) NULL DEFAULT '' COMMENT '删除令牌',
  `data_version` int(0) NOT NULL DEFAULT 0 COMMENT '数据版本',
  `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
  `update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
  PRIMARY KEY (`guid`) USING BTREE
) ENGINE = InnoDB COMMENT = '你的表格备注' ROW_FORMAT = Dynamic;

必需字段说明

  • guid - 主键,类型为 bigint UNSIGNED
  • is_hidden - 隐藏标志,用于回收站功能
  • is_delete - 删除标志,用于软删除功能
  • delete_token - 删除令牌,用于标识删除状态
  • data_version - 数据版本,用于乐观锁
  • create_time - 创建时间,自动设置
  • update_time - 更新时间,自动更新

视图表结构

视图表不需要上述标准字段,只需要包含业务字段即可。

mybatis.generator.xml 中配置表

<!-- 标准表配置 -->
<table tableName="your_table_name"
       domainObjectName="YourTableName"
       enableInsert="true"
       enableDeleteByPrimaryKey="true"
       enableUpdateByPrimaryKey="true"
       enableCountByExample="true"
       enableUpdateByExample="true"
       enableDeleteByExample="true"
       enableSelectByExample="true"
       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_deleteis_hiddendelete_tokendata_versioncreate_timeupdate_time 等字段
  8. BLOB 字段支持:如果表包含 BLOB 字段,插件会自动使用 selectByExampleWithBLOBsupdateByExampleWithBLOBs 方法
  9. MyBatis Generator 版本:本插件基于 MyBatis Generator 1.4.1 开发,建议使用 1.4.0 或更高版本
Description
Tape mybatis generator plugin
Readme 562 KiB
Languages
Java 100%