diff --git a/README.md b/README.md index f2f8772..4f1c971 100644 --- a/README.md +++ b/README.md @@ -132,8 +132,8 @@ - + @@ -142,10 +142,10 @@ - + - + @@ -161,25 +161,26 @@ ### 3. 配置参数说明 -| 参数名 | 说明 | 默认值 | 必需 | -|----------------------------|-------------------------------|---------------------------------------------------------|------| -| `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()` | 否 | -| `changeLogContextClassPackage` | 变更日志上下文包路径 | `com.iqudoo.platform.application.domain.changeLog` | 否 | -| `changeLogContextClassName` | 变更日志上下文类 | `ChangeLogContext` | 否 | -| `changeLogEnable` | 变更日志监听开关 | `false` | 否 | -| `slowQueryLoggerTime` | 慢查询日志时间阈值 | `300` | 否 | -| `slowQueryLoggerLevel` | 慢查询日志类型:error,warn,debug,info | `error` | 否 | -| `priorityPrimaryKeyOffset` | 优先查询主键偏移阈值 | `0` | 否 | -| `ignorePageSize` | 忽略分页阈值 | `10000` | 否 | -| `startPageNum` | 分页开始页码 | `1` | 否 | -| `maxPageSize` | 最大每页数量 | `100` | 否 | +| 参数名 | 说明 | 默认值 | 必需 | +|--------------------------------|-------------------------------|---------------------------------------------------------|------| +| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 | +| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 | +| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 | +| `facadeRepositoryPackage` | Repository 接口的包路径 | `com.iqudoo.platform.application.facade.repository` | 否 | +| `domainRepositoryPackage` | Repository 实现类的包路径 | `com.iqudoo.platform.application.domain.repository` | 否 | +| `facadeViewRepositoryPackage` | RepoView 接口的包路径 | `com.iqudoo.platform.application.facade.repoview` | 否 | +| `domainViewRepositoryPackage` | RepoView 实现类的包路径 | `com.iqudoo.platform.application.domain.repoview` | 否 | +| `guidGeneratorClass` | GUID生成工具类 | `com.iqudoo.framework.tape.modules.utils.SnowflakeUtil` | 否 | +| `guidGeneratorCode` | GUID生成方法 | `SnowflakeUtil.nextId()` | 否 | +| `changeLogContextClassPackage` | 变更日志上下文包路径 | `com.iqudoo.platform.application.domain.changeLog` | 否 | +| `changeLogContextClassName` | 变更日志上下文类 | `ChangeLogContext` | 否 | +| `changeLogEnable` | 变更日志监听开关 | `false` | 否 | +| `slowQueryLoggerTime` | 慢查询日志时间阈值 | `300` | 否 | +| `slowQueryLoggerLevel` | 慢查询日志类型:error,warn,debug,info | `error` | 否 | +| `optimisticLockEnable` | 乐观锁开关 | `true` | 否 | +| `ignorePageSize` | 忽略分页阈值 | `10000` | 否 | +| `startPageNum` | 分页开始页码 | `1` | 否 | +| `maxPageSize` | 最大每页数量 | `100` | 否 | ## 变更日志监听 diff --git a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar index 4f1d0af..44a4e02 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 a615782..518e68c 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeRepositoryGeneratorPlugin.java @@ -19,7 +19,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { // 固定配置项 private String slowQueryLoggerTime = "300"; private String slowQueryLoggerLevel = "error"; - private String priorityPrimaryKeyOffset = "0"; private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository"; private String domainRepositoryPackage = "com.iqudoo.platform.application.domain.repository"; private String guidGeneratorClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil"; @@ -27,6 +26,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { private String changeLogContextClassPackage = "com.iqudoo.platform.application.domain.changeLog"; private String changeLogContextClassName = "ChangeLogContext"; private String changeLogEnable = "false"; + private String optimisticLockEnable = "true"; private String modelPackage = "com.iqudoo.platform.application.database.model"; private String mapperPackage = "com.iqudoo.platform.application.database.mapper"; private String targetProject = "src/main/java"; @@ -48,12 +48,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { } private void resolveConfiguration() { + targetProject = stringConfig("targetProject", targetProject); slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime); slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel); if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) { slowQueryLoggerLevel = "error"; } - priorityPrimaryKeyOffset = stringConfig("priorityPrimaryKeyOffset", priorityPrimaryKeyOffset); guidGeneratorClass = stringConfig("guidGeneratorClass", guidGeneratorClass); guidGeneratorCode = stringConfig("guidGeneratorCode", guidGeneratorCode); facadeRepositoryPackage = stringConfig("facadeRepositoryPackage", facadeRepositoryPackage); @@ -61,9 +61,9 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { changeLogContextClassPackage = stringConfig("changeLogContextClassPackage", changeLogContextClassPackage); changeLogContextClassName = stringConfig("changeLogContextClassName", changeLogContextClassName); changeLogEnable = stringConfig("changeLogEnable", changeLogEnable); - modelPackage = stringConfig("modelPackage", modelPackage); + optimisticLockEnable = stringConfig("optimisticLockEnable", optimisticLockEnable); mapperPackage = stringConfig("mapperPackage", mapperPackage); - targetProject = stringConfig("targetProject", targetProject); + modelPackage = stringConfig("modelPackage", modelPackage); } private boolean isChangeLogEnable() { @@ -77,6 +77,17 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { return false; } + private boolean isOptimisticLockEnable() { + try { + boolean b = Boolean.parseBoolean(optimisticLockEnable); + if (b) { + return true; + } + } catch (Throwable ignored) { + } + return false; + } + private String stringConfig(String key, String defaultValue) { String v = properties.getProperty(key); if (StringUtility.stringHasValue(v)) { @@ -397,11 +408,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { generateBatchInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable); generateUpdateMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns); generateUpdateByExampleSelectiveMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns); - generateDeleteByIdMethod(implClass, modelClassName, mapperFieldName); + generateDeleteByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateDeleteAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName); - generateTrashByIdMethod(implClass, modelClassName, mapperFieldName); + generateTrashByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateTrashAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName); - generateRecoverByIdMethod(implClass, modelClassName, mapperFieldName); + generateRecoverByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateRecoverAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName); generateFindValidOneMethod(implClass, modelClassName, exampleClassName); generateFindTrashOneMethod(implClass, modelClassName, exampleClassName); @@ -587,7 +598,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("aDo.setCreateTime(new Date());"); method.addBodyLine("aDo.setUpdateTime(new Date());"); + method.addBodyLine("long startTime = new Date().getTime();"); method.addBodyLine("int count = " + mapperFieldName + ".insert(aDo);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] insert " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); method.addBodyLine("if (count > 0) {"); method.addBodyLine("// optimistic locking with data version and guid"); method.addBodyLine("record.setGuid(aDo.getGuid());"); @@ -646,7 +665,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("batch.add(aDo);"); method.addBodyLine("}"); + method.addBodyLine("long startTime = new Date().getTime();"); method.addBodyLine("int count = " + mapperFieldName + ".batchInsert(batch);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] batch insert " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); method.addBodyLine("if (count == batch.size()) {"); if (isChangeLogEnable()) { method.addBodyLine("for (" + modelClassName + " aDo : batch) {"); @@ -697,29 +724,41 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("}"); } method.addBodyLine(exampleClassName + " updateWhere = new " + exampleClassName + "();"); - method.addBodyLine("Integer lockDataVersion = record.getDataVersion();"); - method.addBodyLine("if (lockDataVersion == null) {"); - method.addBodyLine("lockDataVersion = aDo.getDataVersion();"); - method.addBodyLine("}"); - method.addBodyLine("updateWhere.createCriteria()"); - method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())"); - method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);"); + if (isOptimisticLockEnable()) { + method.addBodyLine("Integer lockDataVersion = record.getDataVersion();"); + method.addBodyLine("if (lockDataVersion == null) {"); + method.addBodyLine("lockDataVersion = aDo.getDataVersion();"); + method.addBodyLine("}"); + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())"); + method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);"); + } else { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid());"); + } method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);"); method.addBodyLine("aDo.setUpdateTime(new Date());"); method.addBodyLine("// update data version"); method.addBodyLine("record.setDataVersion(aDo.getDataVersion());"); method.addBodyLine("record.setUpdateTime(aDo.getUpdateTime());"); + + method.addBodyLine("long startTime = new Date().getTime();"); String updateMethod = hasBLOBColumns ? "updateByExampleWithBLOBs" : "updateByExample"; + method.addBodyLine("int update = " + mapperFieldName + "." + updateMethod + "(aDo, updateWhere);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] update " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + "." + updateMethod + "(aDo, updateWhere);"); method.addBodyLine("if (update > 0 && !changeDiff.isEmpty()) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"update\", aDo.getGuid(), changeDiff);"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + "." + updateMethod + "(aDo, updateWhere);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } @@ -744,10 +783,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("return 0;"); method.addBodyLine("}"); method.addBodyLine("example = new " + exampleClassName + "();"); - method.addBodyLine("example.createCriteria()"); - method.addBodyLine(" .andIsDeleteEqualTo(0)"); - method.addBodyLine(" .andIsHiddenEqualTo(0)"); - method.addBodyLine(" .andGuidIn(guidList);"); + method.addBodyLine("example.createCriteria().andGuidIn(guidList);"); if (isChangeLogEnable()) { String selectByExampleMethod = hasBLOBColumns ? "selectByExampleWithBLOBs" : "selectByExample"; method.addBodyLine("List<" + modelClassName + "> recordList"); @@ -771,16 +807,32 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("diffGroup.put(aDo.getGuid(), changeDiff);"); method.addBodyLine("}"); } - method.addBodyLine("// reset data version to 100, with optimistic locking"); - method.addBodyLine("record.setDataVersion(100);"); + if (isOptimisticLockEnable()) { + method.addBodyLine("// reset data version, with optimistic locking"); + method.addBodyLine("record.setDataVersion((int) (new Date().getTime() % 1000));"); + } method.addBodyLine("// It is not supported to directly modify the following columns"); method.addBodyLine("record.setUpdateTime(new Date());"); method.addBodyLine("record.setIsHidden(null);"); method.addBodyLine("record.setIsDelete(null);"); method.addBodyLine("record.setDeleteToken(null);"); method.addBodyLine("record.setCreateTime(null);"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(record, example);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("String exampleString = \"\";"); + method.addBodyLine("if (example.getWhereString() != null) {"); + method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();"); + method.addBodyLine("}"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] updateByExampleSelective " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" exampleString +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(record, example);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine("for (Map.Entry> diffEntry : diffGroup.entrySet()) {"); method.addBodyLine("if (diffEntry.getValue() != null && !diffEntry.getValue().isEmpty()) {"); @@ -789,14 +841,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("}"); method.addBodyLine("}"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(record, example);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } - private void generateDeleteByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName) { + private void generateDeleteByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) { Method method = new Method("deleteById"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); @@ -811,9 +861,30 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (release) {"); method.addBodyLine("return " + mapperFieldName + ".deleteByPrimaryKey(aDo.getGuid());"); method.addBodyLine("}"); + method.addBodyLine(exampleClassName + " updateWhere"); + method.addBodyLine(" = new " + exampleClassName + "();"); + method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())"); + method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);"); + } else { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid());"); + } + method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);"); method.addBodyLine("aDo.setIsDelete(1);"); method.addBodyLine("aDo.setUpdateTime(new Date());"); - method.addBodyLine("int update = " + mapperFieldName + ".updateByPrimaryKey(aDo);"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] deleteById " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { method.addBodyLine("if (update > 0) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); @@ -824,7 +895,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } - private void generateTrashByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName) { + private void generateTrashByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) { Method method = new Method("trashById"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); @@ -836,23 +907,43 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (aDo == null) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + + method.addBodyLine(exampleClassName + " updateWhere"); + method.addBodyLine(" = new " + exampleClassName + "();"); + method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())"); + method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);"); + } else { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid());"); + } + method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);"); method.addBodyLine("aDo.setIsHidden(1);"); method.addBodyLine("aDo.setDeleteToken(aDo.getGuid() + \"\");"); method.addBodyLine("aDo.setUpdateTime(new Date());"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] trashById " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByPrimaryKey(aDo);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"trashById\", aDo.getGuid(), new HashMap<>());"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByPrimaryKey(aDo);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } - private void generateRecoverByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName) { + private void generateRecoverByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) { Method method = new Method("recoverById"); method.addAnnotation("@Override"); method.setVisibility(JavaVisibility.PUBLIC); @@ -867,19 +958,38 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (aDo.getIsDelete() == 1) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + + method.addBodyLine(exampleClassName + " updateWhere"); + method.addBodyLine(" = new " + exampleClassName + "();"); + method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())"); + method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);"); + } else { + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid());"); + } + method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);"); method.addBodyLine("aDo.setIsHidden(0);"); method.addBodyLine("aDo.setDeleteToken(\"VALID\");"); method.addBodyLine("aDo.setUpdateTime(new Date());"); + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] recoverById " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByPrimaryKey(aDo);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"recoverById\", aDo.getGuid(), new HashMap<>());"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByPrimaryKey(aDo);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } @@ -903,21 +1013,34 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (guidList.isEmpty()) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + method.addBodyLine("example = new " + exampleClassName + "();"); + method.addBodyLine("example.createCriteria().andGuidIn(guidList);"); method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("// reset data version, with optimistic locking"); + method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));"); + } method.addBodyLine(lowerFirst(modelClassName) + ".setIsDelete(1);"); method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] deleteAll " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine("for (Long guid : guidList) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"deleteAll\", guid, new HashMap<>());"); method.addBodyLine("}"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } @@ -937,22 +1060,35 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (guidList.isEmpty()) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + method.addBodyLine("example = new " + exampleClassName + "();"); + method.addBodyLine("example.createCriteria().andGuidIn(guidList);"); method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("// reset data version, with optimistic locking"); + method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));"); + } method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(1);"); method.addBodyLine(lowerFirst(modelClassName) + ".setDeleteToken(" + guidGeneratorCode + " + \"\");"); method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] trashAll " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine("for (Long guid : guidList) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"trashAll\", guid, new HashMap<>());"); method.addBodyLine("}"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } @@ -972,22 +1108,35 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (guidList.isEmpty()) {"); method.addBodyLine("return 0;"); method.addBodyLine("}"); + method.addBodyLine("example = new " + exampleClassName + "();"); + method.addBodyLine("example.createCriteria().andGuidIn(guidList);"); method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();"); + if (isOptimisticLockEnable()) { + method.addBodyLine("// reset data version, with optimistic locking"); + method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));"); + } method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(0);"); method.addBodyLine(lowerFirst(modelClassName) + ".setDeleteToken(\"VALID\");"); method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());"); + + method.addBodyLine("long startTime = new Date().getTime();"); + method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] recoverAll " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); if (isChangeLogEnable()) { - method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); method.addBodyLine("if (update > 0) {"); method.addBodyLine("for (Long guid : guidList) {"); method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\","); method.addBodyLine(" \"recoverAll\", guid, new HashMap<>());"); method.addBodyLine("}"); method.addBodyLine("}"); - method.addBodyLine("return update;"); - } else { - method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);"); } + method.addBodyLine("return update;"); implClass.addMethod(method); } @@ -1043,7 +1192,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("List<" + modelClassName + "> result = null;"); method.addBodyLine("long startTime = new Date().getTime();"); if (hasBLOBColumns) { - method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {"); + 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<>();"); @@ -1060,7 +1209,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list primary key use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list primary key long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + findPrimaryKeyTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -1095,7 +1244,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -1120,7 +1269,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter { method.addBodyLine("List<" + modelClassName + "> result = null;"); method.addBodyLine("long startTime = new Date().getTime();"); if (hasBLOBColumns) { - method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {"); + 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<>();"); @@ -1137,7 +1286,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list primary key use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list primary key long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + findPrimaryKeyTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -1172,7 +1321,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -1204,7 +1353,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid count use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid count long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -1254,7 +1403,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." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash count use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash count long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); diff --git a/src/main/java/com/iqudoo/framework/mybatis/TapeRepoviewGeneratorPlugin.java b/src/main/java/com/iqudoo/framework/mybatis/TapeRepoviewGeneratorPlugin.java index 214f505..4827c57 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeRepoviewGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeRepoviewGeneratorPlugin.java @@ -45,6 +45,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter { } private void resolveConfiguration() { + targetProject = stringConfig("targetProject", targetProject); slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime); slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel); if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) { @@ -52,9 +53,8 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter { } facadeViewRepositoryPackage = stringConfig("facadeRepoviewPackage", facadeViewRepositoryPackage); domainViewRepositoryPackage = stringConfig("domainRepoviewPackage", domainViewRepositoryPackage); - modelPackage = stringConfig("modelPackage", modelPackage); mapperPackage = stringConfig("mapperPackage", mapperPackage); - targetProject = stringConfig("targetProject", targetProject); + modelPackage = stringConfig("modelPackage", modelPackage); } private String stringConfig(String key, String defaultValue) { @@ -296,7 +296,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (example.getOrderByClause() != null) {"); method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); method.addBodyLine("}"); - method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view list use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view list long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\""); @@ -330,7 +330,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter { method.addBodyLine("if (example.getOrderByClause() != null) {"); method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();"); method.addBodyLine("}"); - method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view count use long time\" +"); + method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view count long time\" +"); method.addBodyLine(" \"\\n\\t|-> use time:\" + useTime + \"ms\" +"); method.addBodyLine(" exampleString +"); method.addBodyLine(" \"\\n\\t|-----------------------------------\"");