12 KiB
12 KiB
tape-mybatis-generator-plugin
MyBatis 代码生成插件
功能特性
本插件为 MyBatis Generator 提供了以下增强功能:
- TapeMybatisGeneratorPlugin - 扩展 MyBatis Mapper,添加
selectPrimaryKeyByExample方法,支持分页查询主键列表 - TapeRepositoryGeneratorPlugin - 为非视图表自动生成 Repository 接口和实现类,提供完整的 CRUD 和软删除功能
- TapeRepoviewGeneratorPlugin - 为视图表自动生成 RepoView 接口和实现类,提供查询功能
- 分页支持 - 为 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 UNSIGNEDis_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>
注意事项
- 主键要求:表必须有一个主键字段,且字段名为
guid,类型为bigint UNSIGNED - 视图表识别:视图表通过表名中的关键字识别,默认关键字为
view_和v_不区分大小写 - 分页功能:所有 Example 类都自动包含分页功能,可通过
usePage()方法使用 - 乐观锁:更新操作使用
data_version字段实现乐观锁,更新时需要传入正确的版本号 - 软删除:删除操作默认是软删除(设置
is_delete标志),可通过release=true参数执行物理删除 - 回收站:通过
is_hidden字段实现回收站功能,trash方法将记录移动到回收站,recover方法恢复记录 - 自动字段:插入记录时,插件会自动设置
guid(使用雪花算法)、is_delete、is_hidden、delete_token、data_version、create_time、update_time等字段 - BLOB 字段支持:如果表包含 BLOB 字段,插件会自动使用
selectByExampleWithBLOBs和updateByExampleWithBLOBs方法 - MyBatis Generator 版本:本插件基于 MyBatis Generator 1.4.1 开发,建议使用 1.4.0 或更高版本