慢查询日志
This commit is contained in:
@@ -13,13 +13,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* MyBatis Generator 1.4.1
|
||||
*/
|
||||
@SuppressWarnings({"DuplicatedCode", "unused", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 固定配置项
|
||||
private String slowQueryLoggerTime = "300";
|
||||
private String snowflakeUtilClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil";
|
||||
private String snowflakeUtilGenId = "SnowflakeUtil.nextId()";
|
||||
private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository";
|
||||
@@ -48,6 +46,9 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
// 读取自定义配置
|
||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilClass"))) {
|
||||
snowflakeUtilClass = properties.getProperty("snowflakeUtilClass");
|
||||
}
|
||||
@@ -347,6 +348,14 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType superInterface = new FullyQualifiedJavaType(facadeRepositoryPackage + "." + interfaceName);
|
||||
implClass.addSuperInterface(superInterface);
|
||||
|
||||
// slow query logger
|
||||
Field loggerField = new Field("LOGGER", new FullyQualifiedJavaType("Logger"));
|
||||
loggerField.setVisibility(JavaVisibility.PRIVATE);
|
||||
loggerField.setStatic(true);
|
||||
loggerField.setFinal(true);
|
||||
loggerField.setInitializationString("LoggerFactory.getLogger(" + implClassName + ".class)");
|
||||
implClass.addField(loggerField);
|
||||
|
||||
String mapperFieldName = lowerFirst(mapperClassName);
|
||||
Field mapperField = new Field(mapperFieldName, new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
|
||||
mapperField.setVisibility(JavaVisibility.PRIVATE);
|
||||
@@ -391,7 +400,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("return " + mapperFieldName + ".deleteByExample(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsDelete(1);");
|
||||
@@ -492,6 +501,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(facadeRepositoryPackage + "." + interfaceName));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.Logger"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.LoggerFactory"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.ArrayList"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.Date"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
||||
@@ -634,8 +645,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("lockDataVersion = aDo.getDataVersion();");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("updateWhere.createCriteria()");
|
||||
method.addBodyLine(".andGuidEqualTo(aDo.getGuid())");
|
||||
method.addBodyLine(".andDataVersionEqualTo(lockDataVersion);");
|
||||
method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())");
|
||||
method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);");
|
||||
method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);");
|
||||
method.addBodyLine("aDo.setUpdateTime(new Date());");
|
||||
method.addBodyLine("// update data version");
|
||||
@@ -787,11 +798,17 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
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<>();");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"find valid list primary key use long time: \" + findPrimaryKeyTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
||||
@@ -812,6 +829,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"get valid list use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -827,11 +850,17 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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("try {");
|
||||
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<>();");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"find trash list primary key use long time: \" + findPrimaryKeyTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
||||
@@ -852,6 +881,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"get trash list use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -866,7 +901,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"count by valid use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -882,7 +925,15 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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("try {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"count by trash use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -16,13 +16,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* MyBatis Generator 1.4.1 适配版:视图表专用 RepoView 生成插件
|
||||
*/
|
||||
@SuppressWarnings({"DuplicatedCode", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||
public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 视图Repo包配置(可通过配置文件自定义)
|
||||
private String slowQueryLoggerTime = "300";
|
||||
private String facadeRepoviewPackage = "com.iqudoo.platform.application.facade.repoview";
|
||||
private String domainRepoviewPackage = "com.iqudoo.platform.application.domain.repoview";
|
||||
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
||||
@@ -51,6 +49,9 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
// 读取自定义配置
|
||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepoviewPackage"))) {
|
||||
facadeRepoviewPackage = properties.getProperty("facadeRepoviewPackage");
|
||||
}
|
||||
@@ -209,6 +210,14 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType superInterface = new FullyQualifiedJavaType(facadeRepoviewPackage + "." + interfaceName);
|
||||
implClass.addSuperInterface(superInterface);
|
||||
|
||||
// slow query logger
|
||||
Field loggerField = new Field("LOGGER", new FullyQualifiedJavaType("Logger"));
|
||||
loggerField.setVisibility(JavaVisibility.PRIVATE);
|
||||
loggerField.setStatic(true);
|
||||
loggerField.setFinal(true);
|
||||
loggerField.setInitializationString("LoggerFactory.getLogger(" + implClassName + ".class)");
|
||||
implClass.addField(loggerField);
|
||||
|
||||
// 添加Mapper字段
|
||||
String mapperFieldName = lowerFirst(mapperClassName);
|
||||
Field mapperField = new Field(mapperFieldName, new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
|
||||
@@ -240,6 +249,9 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
// 注解&工具类
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.Logger"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.LoggerFactory"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.ArrayList"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
||||
}
|
||||
|
||||
@@ -277,6 +289,8 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
// 参数名匹配示例(首字母小写)
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
@@ -285,6 +299,12 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"get view list use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -301,7 +321,15 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||
method.addBodyLine("LOGGER.error(\"count view use long time: \" + useTime + \"ms\");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("}");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user