更新生成插件

This commit is contained in:
iqudoo
2026-04-09 16:41:32 +08:00
parent 8a5ff1c44c
commit caeaad6426
3 changed files with 42 additions and 2 deletions

View File

@@ -41,6 +41,7 @@
- `countByValid({Example} example)` - 统计有效记录数 - `countByValid({Example} example)` - 统计有效记录数
- `countByTrash({Example} example)` - 统计回收站记录数 - `countByTrash({Example} example)` - 统计回收站记录数
- `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值 - `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值
- `updateByExampleSelective({Model} record, {Example} example)` - 按条件更新记录
- `update({Model} record)` - 更新记录(支持乐观锁) - `update({Model} record)` - 更新记录(支持乐观锁)
### TapeRepoviewGeneratorPlugin ### TapeRepoviewGeneratorPlugin
@@ -54,8 +55,8 @@
- `getList({Example} example)` - 获取记录列表(支持分页) - `getList({Example} example)` - 获取记录列表(支持分页)
- `count({Example} example)` - 统计记录数 - `count({Example} example)` - 统计记录数
### 分页功能 ### 其他功能
通过 `AbstractWithLimitPlugin` 为所有 Example 类添加分页支持: 通过 `TapeMybatisGeneratorPlugin` 为所有 Example 类添加支持:
**添加的字段** **添加的字段**
- `offset` - 偏移量 - `offset` - 偏移量
@@ -64,6 +65,7 @@
- `ignorePageSize` - 忽略分页数量(默认 10000每页数量大于10000时忽略分页 - `ignorePageSize` - 忽略分页数量(默认 10000每页数量大于10000时忽略分页
- `defaultPageSize` - 默认每页数量(默认 20 - `defaultPageSize` - 默认每页数量(默认 20
- `maxPageSize` - 最大每页数量(默认 100 - `maxPageSize` - 最大每页数量(默认 100
- `withBLOBs` - 是否返回BLOBs列的数据
**添加的方法** **添加的方法**
- `limit(int rows)` - 设置每页数量 - `limit(int rows)` - 设置每页数量
@@ -71,6 +73,8 @@
- `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset - `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset
- `getPageNum()` - 获取当前页码 - `getPageNum()` - 获取当前页码
- `getPageSize()` - 获取当前每页数量 - `getPageSize()` - 获取当前每页数量
- `setWithBLOBs(boolean withBLOBs)` - 设置是否返回BLOBs列的数据
- `isWithBLOBs()` - 是否返回BLOBs列的数据
## 使用方法 ## 使用方法
### 1. 在 `pom.xml` 中配置插件 ### 1. 在 `pom.xml` 中配置插件

View File

@@ -312,6 +312,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
updateMethod.setAbstract(true); updateMethod.setAbstract(true);
repositoryInterface.addMethod(updateMethod); repositoryInterface.addMethod(updateMethod);
// 17. updateByExampleSelective
Method updateByExampleSelectiveMethod = new Method("updateByExampleSelective");
updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int"));
updateByExampleSelectiveMethod.addParameter(new Parameter(new FullyQualifiedJavaType(modelClassName), "record"));
updateByExampleSelectiveMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
updateByExampleSelectiveMethod.addException(new FullyQualifiedJavaType("Throwable"));
updateByExampleSelectiveMethod.setAbstract(true);
repositoryInterface.addMethod(updateByExampleSelectiveMethod);
return repositoryInterface; return repositoryInterface;
} }
@@ -349,6 +359,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
generateFindTrashByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName); generateFindTrashByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName);
generateInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable); generateInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable);
generateUpdateMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns); generateUpdateMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns);
generateUpdateByExampleSelectiveMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateDeleteByIdMethod(implClass, modelClassName, mapperFieldName); generateDeleteByIdMethod(implClass, modelClassName, mapperFieldName);
generateDeleteAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateDeleteAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateTrashByIdMethod(implClass, modelClassName, mapperFieldName); generateTrashByIdMethod(implClass, modelClassName, mapperFieldName);
@@ -637,6 +648,31 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method); implClass.addMethod(method);
} }
private void generateUpdateByExampleSelectiveMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("updateByExampleSelective");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(new FullyQualifiedJavaType("int"));
method.addParameter(new Parameter(new FullyQualifiedJavaType(modelClassName), "record"));
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
method.addException(new FullyQualifiedJavaType("Throwable"));
// 方法体
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
method.addBodyLine("}");
method.addBodyLine("record.setUpdateTime(new Date());");
method.addBodyLine("// It is not supported to directly modify the following columns");
method.addBodyLine("record.setIsHidden(null);");
method.addBodyLine("record.setIsDelete(null);");
method.addBodyLine("record.setDeleteToken(null);");
method.addBodyLine("record.setDataVersion(null);");
method.addBodyLine("record.setCreateTime(null);");
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(record, example);");
implClass.addMethod(method);
}
private void generateDeleteByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName) { private void generateDeleteByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName) {
Method method = new Method("deleteById"); Method method = new Method("deleteById");
method.addAnnotation("@Override"); method.addAnnotation("@Override");