diff --git a/README.md b/README.md index e81a0ac..ed80025 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ - `countByValid({Example} example)` - 统计有效记录数 - `countByTrash({Example} example)` - 统计回收站记录数 - `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值) +- `updateByExampleSelective({Model} record, {Example} example)` - 按条件更新记录 - `update({Model} record)` - 更新记录(支持乐观锁) ### TapeRepoviewGeneratorPlugin @@ -54,8 +55,8 @@ - `getList({Example} example)` - 获取记录列表(支持分页) - `count({Example} example)` - 统计记录数 -### 分页功能 -通过 `AbstractWithLimitPlugin` 为所有 Example 类添加分页支持: +### 其他功能 +通过 `TapeMybatisGeneratorPlugin` 为所有 Example 类添加支持: **添加的字段**: - `offset` - 偏移量 @@ -64,6 +65,7 @@ - `ignorePageSize` - 忽略分页数量(默认 10000)每页数量大于10000时,忽略分页 - `defaultPageSize` - 默认每页数量(默认 20) - `maxPageSize` - 最大每页数量(默认 100) +- `withBLOBs` - 是否返回BLOBs列的数据 **添加的方法**: - `limit(int rows)` - 设置每页数量 @@ -71,6 +73,8 @@ - `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset) - `getPageNum()` - 获取当前页码 - `getPageSize()` - 获取当前每页数量 +- `setWithBLOBs(boolean withBLOBs)` - 设置是否返回BLOBs列的数据 +- `isWithBLOBs()` - 是否返回BLOBs列的数据 ## 使用方法 ### 1. 在 `pom.xml` 中配置插件 diff --git a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar index d33ecb3..5c4df26 100644 Binary files a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar and b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar differ diff --git a/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java b/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java index adbc613..7d4a367 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java @@ -312,6 +312,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { updateMethod.setAbstract(true); 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; } @@ -349,6 +359,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { generateFindTrashByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName); generateInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable); generateUpdateMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns); + generateUpdateByExampleSelectiveMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateDeleteByIdMethod(implClass, modelClassName, mapperFieldName); generateDeleteAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateTrashByIdMethod(implClass, modelClassName, mapperFieldName); @@ -637,6 +648,31 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { 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) { Method method = new Method("deleteById"); method.addAnnotation("@Override");