优化:

乐观锁支持配置,数据字段级日志,慢SQL日志
This commit is contained in:
iqudoo
2026-04-17 03:54:01 +08:00
parent c048da1faf
commit e547f645a6
4 changed files with 236 additions and 86 deletions

View File

@@ -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<Long, Map<String, Object[]>> 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<Long> 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<Long> 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|-----------------------------------\"");

View File

@@ -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|-----------------------------------\"");