diff --git a/README.md b/README.md index 51619f9..4da2cb1 100644 --- a/README.md +++ b/README.md @@ -35,17 +35,11 @@ - `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)` - 统计有效记录数 -- `countByValidWithPage({Example} example)` - 统计分页有效记录数 -- `countByTrash({Example} example)` - 统计回收站记录数 -- `countByTrashWithPage({Example} example)` - 统计分页回收站记录数 +- `findById(long id)` - 根据主键查询数据 +- `findOne({Example} example)` - 查找单条数据 +- `getList({Example} example)` - 获取多条数据 +- `count({Example} example)` - 统计记录数 +- `countByWithPage({Example} example)` - 统计分页记录数 - `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值) - `batchInsert(List<{Model}> records)` - 批量插入记录(自动生成 GUID、设置默认值) - `updateByExampleSelective({Model} record, {Example} example)` - 按条件更新记录 @@ -55,9 +49,8 @@ 为视图表生成 RepoView 层代码: -- **接口位置**: `{facadeRepoviewPackage}.I{TableName}Repo` -- **实现类位置**: `{domainRepoviewPackage}.{TableName}RepoImpl` -- **视图表识别**: 仅处理包含 `viewKeyWords` 关键字的表 +- **接口位置**: `{facadeViewRepositoryPackage}.I{TableName}Repo` +- **实现类位置**: `{domainViewRepositoryPackage}.{TableName}RepoImpl` **生成的方法**: @@ -77,6 +70,7 @@ - `ignorePageSize` - 忽略分页数量(默认 10000)每页数量大于10000时,忽略分页 - `maxPageSize` - 最大每页数量(默认 100) - `withBLOBs` - 是否返回BLOBs列的数据 +- `resultType` - 返回数据格式:any, trash, valid(默认) **添加的方法**: @@ -85,6 +79,13 @@ - `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset) - `setWithBLOBs(boolean withBLOBs)` - 设置是否返回BLOBs列的数据 - `isWithBLOBs()` - 是否返回BLOBs列的数据 +- `setResultType(String type)` - 设置返回值的类型 +- `resultAny()` - 不区分 +- `isResultAny()` - 查询不区分 +- `resultTrash()` - 回收站 +- `isResultTrash()` - 查询回收站 +- `resultValid()` - 有效数据 +- `isResultValid()` - 查询有效数据 - `getPageNum()` - 获取当前页码 - `getPageSize()` - 获取当前每页数量 - `getOffset()` - 获取当前分页limit的offset diff --git a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar index 779f5bb..e693dd1 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/TapeMybatisGeneratorPlugin.java b/src/main/java/com/iqudoo/framework/mybatis/TapeMybatisGeneratorPlugin.java index 6631edf..307ed27 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeMybatisGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeMybatisGeneratorPlugin.java @@ -101,6 +101,8 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { @SuppressWarnings("DuplicatedCode") @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.Objects")); + FullyQualifiedJavaType integerType = new FullyQualifiedJavaType("java.lang.Integer"); FullyQualifiedJavaType booleanType = new FullyQualifiedJavaType("java.lang.Boolean"); FullyQualifiedJavaType stringType = new FullyQualifiedJavaType("java.lang.String"); @@ -108,6 +110,18 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName(); String exampleClassName = domainObjectName + "Example"; + + if (!ElementTools.isViewTable(introspectedTable)) { + // any, trash, valid + Field resultType = ElementTools.generateField( + "resultType", + JavaVisibility.PROTECTED, + stringType, + "null" + ); + topLevelClass.addField(resultType); + } + // 添加 startPageNum、maxPageSize、ignorePageSize 字段 Field maxPageSizeField = ElementTools.generateField( "maxPageSize", @@ -188,6 +202,99 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { Method mGetStartPageNum = ElementTools.generateGetterMethod(startPageNumField); FormatTools.addMethodWithBestPosition(topLevelClass, mGetStartPageNum); + + if (!ElementTools.isViewTable(introspectedTable)) { + + // setResultType + Method setResultType = ElementTools.generateMethod( + "setResultType", + JavaVisibility.PUBLIC, + topLevelClass.getType(), + new Parameter(stringType, "type") + ); + setResultType = ElementTools.generateMethodBody( + setResultType, + "this.resultType = type;", + "return this;" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, setResultType); + + // resultValid + Method resultValid = ElementTools.generateMethod( + "resultValid", + JavaVisibility.PUBLIC, + topLevelClass.getType() + ); + resultValid = ElementTools.generateMethodBody( + resultValid, + "this.resultType = \"valid\";", + "return this;" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, resultValid); + + // isResultValid + Method isResultValid = ElementTools.generateMethod( + "isResultValid", + JavaVisibility.PUBLIC, + booleanType + ); + isResultValid = ElementTools.generateMethodBody( + isResultValid, + "return !this.isResultAny() && !this.isResultTrash();" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, isResultValid); + + // resultTrash + Method resultTrash = ElementTools.generateMethod( + "resultTrash", + JavaVisibility.PUBLIC, + topLevelClass.getType() + ); + resultTrash = ElementTools.generateMethodBody( + resultTrash, + "this.resultType = \"trash\";", + "return this;" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, resultTrash); + + // isResultTrash + Method isResultTrash = ElementTools.generateMethod( + "isResultTrash", + JavaVisibility.PUBLIC, + booleanType + ); + isResultTrash = ElementTools.generateMethodBody( + isResultTrash, + "return Objects.equals(this.resultType, \"trash\");" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, isResultTrash); + + // resultAny + Method resultAny = ElementTools.generateMethod( + "resultAny", + JavaVisibility.PUBLIC, + topLevelClass.getType() + ); + resultAny = ElementTools.generateMethodBody( + resultAny, + "this.resultType = \"any\";", + "return this;" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, resultAny); + + // isResultAny + Method isResultAny = ElementTools.generateMethod( + "isResultAny", + JavaVisibility.PUBLIC, + booleanType + ); + isResultAny = ElementTools.generateMethodBody( + isResultAny, + "return Objects.equals(this.resultType, \"any\");" + ); + FormatTools.addMethodWithBestPosition(topLevelClass, isResultAny); + } + // 提供几个快捷方法 Method setLimitByRows = ElementTools.generateMethod( "limit", @@ -203,6 +310,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByRows); + // setLimitByOffsetRows Method setLimitByOffsetRows = ElementTools.generateMethod( "limit", JavaVisibility.PUBLIC, @@ -218,6 +326,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetRows); + // limitOffset Method setLimitByOffsetOnly = ElementTools.generateMethod( "limitOffset", JavaVisibility.PUBLIC, @@ -233,6 +342,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetOnly); + // usePage Method usePage = ElementTools.generateMethod( "usePage", JavaVisibility.PUBLIC, @@ -256,7 +366,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, usePage); - // 计算获取当前页码 + // getPageNum 计算获取当前页码 Method getPageNum = ElementTools.generateMethod( "getPageNum", JavaVisibility.PUBLIC, @@ -270,7 +380,8 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { "return this.offset / this.rows + this.startPageNum;" ); FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum); - // 计算获取当前每页数量 + + // getPageSize 计算获取当前每页数量 Method getPageSize = ElementTools.generateMethod( "getPageSize", JavaVisibility.PUBLIC, @@ -285,6 +396,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize); + // getRows Method getRows = ElementTools.generateMethod( "getRows", JavaVisibility.PUBLIC, @@ -296,6 +408,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, getRows); + // getOffset Method getOffset = ElementTools.generateMethod( "getOffset", JavaVisibility.PUBLIC, @@ -307,6 +420,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, getOffset); + // getLimitString Method getLimitString = ElementTools.generateMethod( "getLimitString", JavaVisibility.PUBLIC, @@ -325,6 +439,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, getLimitString); + // getWhereString Method getWhereString = ElementTools.generateMethod( "getWhereString", JavaVisibility.PUBLIC, @@ -360,6 +475,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { ); FormatTools.addMethodWithBestPosition(topLevelClass, getWhereString); + // cloneExample Method cloneExample = ElementTools.generateMethod( "cloneExample", JavaVisibility.PUBLIC, @@ -372,7 +488,8 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { "newExample.offset = this.offset;", "newExample.maxPageSize = this.maxPageSize;", "newExample.ignorePageSize = this.ignorePageSize;", - "newExample.startPageNum = startPageNum;", + "newExample.startPageNum = this.startPageNum;", + !ElementTools.isViewTable(introspectedTable) ? "newExample.resultType = this.resultType;" : "", hasBLOBColumns ? "newExample.withBLOBs = withBLOBs;" : "", "if (this.getOredCriteria() != null && !this.getOredCriteria().isEmpty()) {", "for (" + exampleClassName + ".Criteria oldCriteria : this.getOredCriteria()) {", @@ -392,6 +509,9 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { if (method.getName().equals("clear")) { method.addBodyLine("rows = null;"); method.addBodyLine("offset = null;"); + if (!ElementTools.isViewTable(introspectedTable)) { + method.addBodyLine("resultType = null;"); + } hasClear = true; } } @@ -404,7 +524,8 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { clear = ElementTools.generateMethodBody( clear, "rows = null;", - "offset = null;" + "offset = null;", + !ElementTools.isViewTable(introspectedTable) ? "resultType = null;" : "" ); FormatTools.addMethodWithBestPosition(topLevelClass, clear); } @@ -464,9 +585,6 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { batchInsert.setVisibility(JavaVisibility.PUBLIC); batchInsert.setReturnType(FullyQualifiedJavaType.getIntInstance()); batchInsert.addParameter(recordsParam); - - interfaceObj.addImportedType(new FullyQualifiedJavaType("java.util.List")); - interfaceObj.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param")); interfaceObj.addMethod(batchInsert); } @@ -512,8 +630,12 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter { if (introspectedTable.getTargetRuntime() != IntrospectedTable.TargetRuntime.MYBATIS3) { return super.clientGenerated(interfaceObj, introspectedTable); } + interfaceObj.addImportedType(new FullyQualifiedJavaType("java.util.List")); + interfaceObj.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param")); + addBatchInsertClientMethod(interfaceObj, introspectedTable); - if (introspectedTable.getPrimaryKeyColumns().size() <= 0) { + + if (introspectedTable.getPrimaryKeyColumns().isEmpty()) { return super.clientGenerated(interfaceObj, introspectedTable); } // 获取主键列类型 diff --git a/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java b/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java index 03e0be4..fb67a59 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java @@ -254,7 +254,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { findAnyByIdMethod.setAbstract(true); repositoryInterface.addMethod(findAnyByIdMethod); - // 8. findValidById + // 7. findValidById Method findValidByIdMethod = new Method("findValidById"); findValidByIdMethod.setVisibility(JavaVisibility.PUBLIC); findValidByIdMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); @@ -263,7 +263,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { findValidByIdMethod.setAbstract(true); repositoryInterface.addMethod(findValidByIdMethod); - // 9. findTrashById + // 7. findTrashById Method findTrashByIdMethod = new Method("findTrashById"); findTrashByIdMethod.setVisibility(JavaVisibility.PUBLIC); findTrashByIdMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); @@ -272,79 +272,43 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { findTrashByIdMethod.setAbstract(true); repositoryInterface.addMethod(findTrashByIdMethod); - // 10. findValidOne - Method findValidOneMethod = new Method("findValidOne"); - findValidOneMethod.setVisibility(JavaVisibility.PUBLIC); - findValidOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); - findValidOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - findValidOneMethod.addException(new FullyQualifiedJavaType("Throwable")); - findValidOneMethod.setAbstract(true); - repositoryInterface.addMethod(findValidOneMethod); + // 8. findOne + Method findOneMethod = new Method("findOne"); + findOneMethod.setVisibility(JavaVisibility.PUBLIC); + findOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); + findOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); + findOneMethod.addException(new FullyQualifiedJavaType("Throwable")); + findOneMethod.setAbstract(true); + repositoryInterface.addMethod(findOneMethod); - // 11. findTrashOne - Method findTrashOneMethod = new Method("findTrashOne"); - findTrashOneMethod.setVisibility(JavaVisibility.PUBLIC); - findTrashOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); - findTrashOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - findTrashOneMethod.addException(new FullyQualifiedJavaType("Throwable")); - findTrashOneMethod.setAbstract(true); - repositoryInterface.addMethod(findTrashOneMethod); + // 9. getList + Method getListMethod = new Method("getList"); + getListMethod.setVisibility(JavaVisibility.PUBLIC); + getListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); + getListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); + getListMethod.addException(new FullyQualifiedJavaType("Throwable")); + getListMethod.setAbstract(true); + repositoryInterface.addMethod(getListMethod); - // 12. getValidList - Method getValidListMethod = new Method("getValidList"); - getValidListMethod.setVisibility(JavaVisibility.PUBLIC); - getValidListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); - getValidListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - getValidListMethod.addException(new FullyQualifiedJavaType("Throwable")); - getValidListMethod.setAbstract(true); - repositoryInterface.addMethod(getValidListMethod); + // 10. count + Method countMethod = new Method("count"); + countMethod.setVisibility(JavaVisibility.PUBLIC); + countMethod.setReturnType(new FullyQualifiedJavaType("long")); + countMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); + countMethod.addException(new FullyQualifiedJavaType("Throwable")); + countMethod.setAbstract(true); + repositoryInterface.addMethod(countMethod); - // 13. getTrashList - Method getTrashListMethod = new Method("getTrashList"); - getTrashListMethod.setVisibility(JavaVisibility.PUBLIC); - getTrashListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); - getTrashListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - getTrashListMethod.addException(new FullyQualifiedJavaType("Throwable")); - getTrashListMethod.setAbstract(true); - repositoryInterface.addMethod(getTrashListMethod); + // 11. countWithPage + Method countWithPageMethod = new Method("countWithPage"); + countWithPageMethod.setVisibility(JavaVisibility.PUBLIC); + countWithPageMethod.setReturnType(new FullyQualifiedJavaType("long")); + countWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); + countWithPageMethod.addException(new FullyQualifiedJavaType("Throwable")); + countWithPageMethod.setAbstract(true); + repositoryInterface.addMethod(countWithPageMethod); - // 14. countByValid - Method countByValidMethod = new Method("countByValid"); - countByValidMethod.setVisibility(JavaVisibility.PUBLIC); - countByValidMethod.setReturnType(new FullyQualifiedJavaType("long")); - countByValidMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - countByValidMethod.addException(new FullyQualifiedJavaType("Throwable")); - countByValidMethod.setAbstract(true); - repositoryInterface.addMethod(countByValidMethod); - - // 15. countByValidWithPage - Method countByValidWithPageMethod = new Method("countByValidWithPage"); - countByValidWithPageMethod.setVisibility(JavaVisibility.PUBLIC); - countByValidWithPageMethod.setReturnType(new FullyQualifiedJavaType("long")); - countByValidWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - countByValidWithPageMethod.addException(new FullyQualifiedJavaType("Throwable")); - countByValidWithPageMethod.setAbstract(true); - repositoryInterface.addMethod(countByValidWithPageMethod); - - // 16. countByTrash - Method countByTrashMethod = new Method("countByTrash"); - countByTrashMethod.setVisibility(JavaVisibility.PUBLIC); - countByTrashMethod.setReturnType(new FullyQualifiedJavaType("long")); - countByTrashMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - countByTrashMethod.addException(new FullyQualifiedJavaType("Throwable")); - countByTrashMethod.setAbstract(true); - repositoryInterface.addMethod(countByTrashMethod); - - // 17. countByTrashWithPage - Method countByTrashWithPageMethod = new Method("countByTrashWithPage"); - countByTrashWithPageMethod.setVisibility(JavaVisibility.PUBLIC); - countByTrashWithPageMethod.setReturnType(new FullyQualifiedJavaType("long")); - countByTrashWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - countByTrashWithPageMethod.addException(new FullyQualifiedJavaType("Throwable")); - countByTrashWithPageMethod.setAbstract(true); - repositoryInterface.addMethod(countByTrashWithPageMethod); - - // 18. insert + // 12. insert Method insertMethod = new Method("insert"); insertMethod.setVisibility(JavaVisibility.PUBLIC); insertMethod.setReturnType(new FullyQualifiedJavaType(modelClassName)); @@ -353,7 +317,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { insertMethod.setAbstract(true); repositoryInterface.addMethod(insertMethod); - // 19. batchInsert + // 13. batchInsert Method batchInsertMethod = new Method("batchInsert"); batchInsertMethod.setVisibility(JavaVisibility.PUBLIC); batchInsertMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); @@ -362,7 +326,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { batchInsertMethod.setAbstract(true); repositoryInterface.addMethod(batchInsertMethod); - // 20. update + // 14. update Method updateMethod = new Method("update"); updateMethod.setVisibility(JavaVisibility.PUBLIC); updateMethod.setReturnType(new FullyQualifiedJavaType("int")); @@ -371,7 +335,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { updateMethod.setAbstract(true); repositoryInterface.addMethod(updateMethod); - // 21. updateByExampleSelective + // 15. updateByExampleSelective Method updateByExampleSelectiveMethod = new Method("updateByExampleSelective"); updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC); updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int")); @@ -436,14 +400,10 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { generateTrashAllMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); generateRecoverByIdMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); generateRecoverAllMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); - generateFindValidOneMethod(implClass, tableName, modelClassName, exampleClassName); - generateFindTrashOneMethod(implClass, tableName, modelClassName, exampleClassName); - generateGetValidListMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); - generateGetTrashListMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); - generateCountByValidMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); - generateCountByValidWithPageMethod(implClass, tableName, modelClassName, exampleClassName, mapperFieldName); - generateCountByTrashMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); - generateCountByTrashWithPageMethod(implClass, tableName, modelClassName, exampleClassName, mapperFieldName); + generateFindOneMethod(implClass, tableName, modelClassName, exampleClassName); + generateGetListMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); + generateCountMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); + generateCountWithPageMethod(implClass, tableName, modelClassName, exampleClassName, mapperFieldName); return implClass; } @@ -877,7 +837,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id")); method.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "release")); method.addException(new FullyQualifiedJavaType("Throwable")); - method.addBodyLine(modelClassName + " aDo = findValidById(id);"); + method.addBodyLine(modelClassName + " aDo = findAnyById(id);"); method.addBodyLine("if (aDo == null) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); @@ -927,10 +887,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id")); method.addException(new FullyQualifiedJavaType("Throwable")); - method.addBodyLine(modelClassName + " aDo = findValidById(id);"); + method.addBodyLine(modelClassName + " aDo = findAnyById(id);"); method.addBodyLine("if (aDo == null) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + method.addBodyLine("if (aDo.getIsHidden() == 1) {"); + method.addBodyLine("return 0;"); + method.addBodyLine("}"); method.addBodyLine(exampleClassName + " updateWhere"); method.addBodyLine(" = new " + exampleClassName + "();"); @@ -976,11 +939,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id")); method.addException(new FullyQualifiedJavaType("Throwable")); - method.addBodyLine(modelClassName + " aDo = findTrashById(id);"); + method.addBodyLine(modelClassName + " aDo = findAnyById(id);"); method.addBodyLine("if (aDo == null) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); - method.addBodyLine("if (aDo.getIsDelete() == 1) {"); + method.addBodyLine("if (aDo.getIsHidden() == 0) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); @@ -1176,8 +1139,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } - private void generateFindValidOneMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName) { - Method method = new Method("findValidOne"); + private void generateFindOneMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName) { + Method method = new Method("findOne"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType(modelClassName)); @@ -1187,7 +1150,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("// clone new example"); method.addBodyLine("example = example.cloneExample();"); method.addBodyLine("example.usePage(1, 1);"); - method.addBodyLine("List<" + modelClassName + "> dataList = getValidList(example);"); + method.addBodyLine("List<" + modelClassName + "> dataList = getList(example);"); method.addBodyLine("if (dataList != null && !dataList.isEmpty()) {"); method.addBodyLine("return dataList.get(0);"); method.addBodyLine("}"); @@ -1196,29 +1159,9 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } - private void generateFindTrashOneMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName) { - Method method = new Method("findTrashOne"); - method.addAnnotation("@Override"); - method.setVisibility(JavaVisibility.PUBLIC); - method.setReturnType(new FullyQualifiedJavaType(modelClassName)); - method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - method.addException(new FullyQualifiedJavaType("Throwable")); - - method.addBodyLine("// clone new example"); - method.addBodyLine("example = example.cloneExample();"); - method.addBodyLine("example.usePage(1, 1);"); - method.addBodyLine("List<" + modelClassName + "> dataList = getTrashList(example);"); - method.addBodyLine("if (dataList != null && !dataList.isEmpty()) {"); - method.addBodyLine("return dataList.get(0);"); - method.addBodyLine("}"); - method.addBodyLine("return null;"); - - implClass.addMethod(method); - } - - private void generateGetValidListMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, - String mapperFieldName, boolean hasBLOBColumns) { - Method method = new Method("getValidList"); + private void generateGetListMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, + String mapperFieldName, boolean hasBLOBColumns) { + Method method = new Method("getList"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); @@ -1228,7 +1171,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("// clone new example"); method.addBodyLine("example = example.cloneExample();"); method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {"); - method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);"); + method.addBodyLine("if (example.isResultTrash()) {"); + method.addBodyLine("criteria.andIsHiddenEqualTo(1)"); + method.addBodyLine(" .andIsDeleteEqualTo(0);"); + method.addBodyLine("} else if (example.isResultValid()) {"); + method.addBodyLine("criteria.andIsHiddenEqualTo(0)"); + method.addBodyLine(" .andIsDeleteEqualTo(0);"); + method.addBodyLine("} else {"); + method.addBodyLine("criteria.andIsDeleteEqualTo(0);"); + method.addBodyLine("}"); method.addBodyLine("}"); method.addBodyLine("List<" + modelClassName + "> result = null;"); method.addBodyLine("long startTime = new Date().getTime();"); @@ -1250,7 +1201,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (example.getLimitString() != null) {"); method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();"); method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid list primary key long time\" +"); + method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " list primary key long time\" +"); method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); method.addBodyLine(" \"\\n\\t|-> use time: \" + findPrimaryKeyTime + \"ms\" +"); method.addBodyLine(" exampleString +"); @@ -1286,7 +1237,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (example.getLimitString() != null) {"); method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();"); method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid list long time\" +"); + method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " list long time\" +"); method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); method.addBodyLine(" \"\\n\\t|-> use time: \" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); @@ -1297,89 +1248,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } - private void generateGetTrashListMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, - String mapperFieldName, boolean hasBLOBColumns) { - Method method = new Method("getTrashList"); - method.addAnnotation("@Override"); - method.setVisibility(JavaVisibility.PUBLIC); - method.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">")); - method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - method.addException(new FullyQualifiedJavaType("Throwable")); - - method.addBodyLine("// clone new example"); - method.addBodyLine("example = example.cloneExample();"); - method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {"); - method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);"); - method.addBodyLine("}"); - method.addBodyLine("List<" + modelClassName + "> result = null;"); - method.addBodyLine("long startTime = new Date().getTime();"); - if (hasBLOBColumns) { - method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {"); - method.addBodyLine("List primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);"); - method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {"); - method.addBodyLine("return new ArrayList<>();"); - method.addBodyLine("}"); - method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;"); - method.addBodyLine("if (findPrimaryKeyTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {"); - method.addBodyLine("String exampleString = \"\";"); - method.addBodyLine("if (example.getWhereString() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();"); - method.addBodyLine("}"); - method.addBodyLine("if (example.getOrderByClause() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); - method.addBodyLine("}"); - method.addBodyLine("if (example.getLimitString() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();"); - method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash list primary key long time\" +"); - method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); - method.addBodyLine(" \"\\n\\t|-> use time: \" + findPrimaryKeyTime + \"ms\" +"); - method.addBodyLine(" exampleString +"); - method.addBodyLine(" \"\\n\\t|-----------------------------------\""); - method.addBodyLine(");"); - method.addBodyLine("}"); - method.addBodyLine("// reset start time"); - method.addBodyLine("startTime = new Date().getTime();"); - method.addBodyLine("String oldOrderByClause = example.getOrderByClause();"); - method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();"); - method.addBodyLine("example = new " + exampleClassName + "();"); - method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);"); - method.addBodyLine("example.setOrderByClause(oldOrderByClause);"); - method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);"); - method.addBodyLine("}"); - method.addBodyLine("if (example.isWithBLOBs()) {"); - method.addBodyLine("result = " + mapperFieldName + ".selectByExampleWithBLOBs(example);"); - method.addBodyLine("} else {"); - method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);"); - method.addBodyLine("}"); - } else { - method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);"); - } - method.addBodyLine("long useTime = new Date().getTime() - startTime;"); - method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {"); - method.addBodyLine("String exampleString = \"\";"); - method.addBodyLine("if (example.getWhereString() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();"); - method.addBodyLine("}"); - method.addBodyLine("if (example.getOrderByClause() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); - method.addBodyLine("}"); - method.addBodyLine("if (example.getLimitString() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();"); - method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash list long time\" +"); - method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); - method.addBodyLine(" \"\\n\\t|-> use time: \" + useTime + \"ms\" +"); - method.addBodyLine(" exampleString +"); - method.addBodyLine(" \"\\n\\t|-----------------------------------\""); - method.addBodyLine(");"); - method.addBodyLine("}"); - method.addBodyLine("return result;"); - implClass.addMethod(method); - } - - private void generateCountByValidMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) { - Method method = new Method("countByValid"); + private void generateCountMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) { + Method method = new Method("count"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType("long")); @@ -1389,7 +1259,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("// clone new example"); method.addBodyLine("example = example.cloneExample();"); method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {"); - method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);"); + method.addBodyLine("if (example.isResultTrash()) {"); + method.addBodyLine("criteria.andIsHiddenEqualTo(1)"); + method.addBodyLine(" .andIsDeleteEqualTo(0);"); + method.addBodyLine("} else if (example.isResultValid()) {"); + method.addBodyLine("criteria.andIsHiddenEqualTo(0)"); + method.addBodyLine(" .andIsDeleteEqualTo(0);"); + method.addBodyLine("} else {"); + method.addBodyLine("criteria.andIsDeleteEqualTo(0);"); + method.addBodyLine("}"); method.addBodyLine("}"); method.addBodyLine("long startTime = new Date().getTime();"); method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);"); @@ -1402,7 +1280,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (example.getOrderByClause() != null) {"); method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid count long time\" +"); + method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " count long time\" +"); method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); method.addBodyLine(" \"\\n\\t|-> use time: \" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); @@ -1414,8 +1292,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } - private void generateCountByValidWithPageMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName, String mapperFieldName) { - Method method = new Method("countByValidWithPage"); + private void generateCountWithPageMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName, String mapperFieldName) { + Method method = new Method("countWithPage"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType("long")); @@ -1424,60 +1302,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("// When not paginated, the count query returns 0 to avoid unnecessary queries"); method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {"); - method.addBodyLine("return countByValid(example);"); - method.addBodyLine("}"); - method.addBodyLine("return 0L;"); - - implClass.addMethod(method); - } - - private void generateCountByTrashMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) { - Method method = new Method("countByTrash"); - method.addAnnotation("@Override"); - method.setVisibility(JavaVisibility.PUBLIC); - method.setReturnType(new FullyQualifiedJavaType("long")); - method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - method.addException(new FullyQualifiedJavaType("Throwable")); - - method.addBodyLine("// clone new example"); - method.addBodyLine("example = example.cloneExample();"); - method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {"); - method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);"); - method.addBodyLine("}"); - method.addBodyLine("long startTime = new Date().getTime();"); - method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);"); - method.addBodyLine("long useTime = new Date().getTime() - startTime;"); - method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {"); - method.addBodyLine("String exampleString = \"\";"); - method.addBodyLine("if (example.getWhereString() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();"); - method.addBodyLine("}"); - method.addBodyLine("if (example.getOrderByClause() != null) {"); - method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); - method.addBodyLine("}"); - method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash count long time\" +"); - method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); - method.addBodyLine(" \"\\n\\t|-> use time: \" + useTime + \"ms\" +"); - method.addBodyLine(" exampleString +"); - method.addBodyLine(" \"\\n\\t|-----------------------------------\""); - method.addBodyLine(");"); - method.addBodyLine("}"); - method.addBodyLine("return count;"); - - implClass.addMethod(method); - } - - private void generateCountByTrashWithPageMethod(TopLevelClass implClass, String tableName, String modelClassName, String exampleClassName, String mapperFieldName) { - Method method = new Method("countByTrashWithPage"); - method.addAnnotation("@Override"); - method.setVisibility(JavaVisibility.PUBLIC); - method.setReturnType(new FullyQualifiedJavaType("long")); - method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example")); - method.addException(new FullyQualifiedJavaType("Throwable")); - - method.addBodyLine("// When not paginated, the count query returns 0 to avoid unnecessary queries"); - method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {"); - method.addBodyLine("return countByTrash(example);"); + method.addBodyLine("return count(example);"); method.addBodyLine("}"); method.addBodyLine("return 0L;");