插件更新
This commit is contained in:
@@ -9,61 +9,60 @@ import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.Document;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
private final static int DEFAULT_START_PAGE = 1;
|
||||
private final static int DEFAULT_PAGE_SIZE = 20;
|
||||
private final static int DEFAULT_IGNORE_PAGE_SIZE = 10000;
|
||||
private int startPage = DEFAULT_START_PAGE;
|
||||
private int defaultPageSize = DEFAULT_PAGE_SIZE;
|
||||
private int ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
if (StringUtility.stringHasValue(properties.getProperty("startPage"))) {
|
||||
try {
|
||||
startPage = Integer.parseInt(properties.getProperty("startPage"));
|
||||
} catch (Throwable ignored) {
|
||||
startPage = DEFAULT_START_PAGE;
|
||||
}
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("defaultPageSize"))) {
|
||||
try {
|
||||
defaultPageSize = Integer.parseInt(properties.getProperty("defaultPageSize"));
|
||||
} catch (Throwable ignored) {
|
||||
defaultPageSize = DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("ignorePageSize"))) {
|
||||
try {
|
||||
ignorePageSize = Integer.parseInt(properties.getProperty("ignorePageSize"));
|
||||
} catch (Throwable ignored) {
|
||||
ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
private int startPageNum = 1;
|
||||
private int maxPageSize = 100;
|
||||
private int ignorePageSize = 10000;
|
||||
|
||||
@Override
|
||||
public boolean validate(List<String> list) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
resolveConfiguration();
|
||||
}
|
||||
|
||||
private void resolveConfiguration() {
|
||||
startPageNum = intConfig("startPageNum", startPageNum);
|
||||
ignorePageSize = intConfig("ignorePageSize", ignorePageSize);
|
||||
maxPageSize = intConfig("maxPageSize", maxPageSize);
|
||||
}
|
||||
|
||||
private int intConfig(String key, int defaultValue) {
|
||||
String v = properties.getProperty(key);
|
||||
if (!StringUtility.stringHasValue(v) && context != null) {
|
||||
v = context.getProperty(key);
|
||||
}
|
||||
if (!StringUtility.stringHasValue(v)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(v.trim());
|
||||
} catch (Throwable ignored) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
@Override
|
||||
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
|
||||
// 添加 minPageNum、defaultPageSize、maxPageSize、ignorePageSize 字段
|
||||
// 添加 startPageNum、maxPageSize、ignorePageSize 字段
|
||||
Field maxPageSizeField = ElementTools.generateField(
|
||||
"maxPageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"100"
|
||||
this.maxPageSize + ""
|
||||
);
|
||||
topLevelClass.addField(maxPageSizeField);
|
||||
|
||||
@@ -75,21 +74,13 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
);
|
||||
topLevelClass.addField(ignorePageSizeField);
|
||||
|
||||
Field defaultPageSizeField = ElementTools.generateField(
|
||||
"defaultPageSize",
|
||||
Field startPageNumField = ElementTools.generateField(
|
||||
"startPageNum",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
defaultPageSize + ""
|
||||
startPageNum + ""
|
||||
);
|
||||
topLevelClass.addField(defaultPageSizeField);
|
||||
|
||||
Field minPageNumField = ElementTools.generateField(
|
||||
"minPageNum",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
startPage + ""
|
||||
);
|
||||
topLevelClass.addField(minPageNumField);
|
||||
topLevelClass.addField(startPageNumField);
|
||||
|
||||
// 添加offset和rows字段
|
||||
Field offsetField = ElementTools.generateField(
|
||||
@@ -127,7 +118,6 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, isWithBLOBsMethod);
|
||||
}
|
||||
|
||||
|
||||
// 增加getter && setter 方法
|
||||
Method mSetMaxPageSize = ElementTools.generateSetterMethod(maxPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMaxPageSize);
|
||||
@@ -141,59 +131,41 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
Method mGetIgnorePageSize = ElementTools.generateGetterMethod(ignorePageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetIgnorePageSize);
|
||||
|
||||
Method mSetDefaultPageSize = ElementTools.generateSetterMethod(defaultPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetDefaultPageSize);
|
||||
Method mSetStartPageNum = ElementTools.generateSetterMethod(startPageNumField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetStartPageNum);
|
||||
|
||||
Method mGetDefaultPageSize = ElementTools.generateGetterMethod(defaultPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetDefaultPageSize);
|
||||
|
||||
Method mSetMinPageNum = ElementTools.generateSetterMethod(minPageNumField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMinPageNum);
|
||||
|
||||
Method mGetMinPageNum = ElementTools.generateGetterMethod(minPageNumField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetMinPageNum);
|
||||
|
||||
Method mSetOffset = ElementTools.generateSetterMethod(offsetField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetOffset);
|
||||
|
||||
Method mGetOffset = ElementTools.generateGetterMethod(offsetField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetOffset);
|
||||
|
||||
Method mSetRows = ElementTools.generateSetterMethod(rowsField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetRows);
|
||||
|
||||
Method mGetRows = ElementTools.generateGetterMethod(rowsField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetRows);
|
||||
Method mGetStartPageNum = ElementTools.generateGetterMethod(startPageNumField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetStartPageNum);
|
||||
|
||||
// 提供几个快捷方法
|
||||
Method setLimit = ElementTools.generateMethod(
|
||||
Method setLimitByRows = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit = ElementTools.generateMethodBody(
|
||||
setLimit,
|
||||
setLimitByRows = ElementTools.generateMethodBody(
|
||||
setLimitByRows,
|
||||
"this.offset = null;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByRows);
|
||||
|
||||
Method setLimit2 = ElementTools.generateMethod(
|
||||
Method setLimitByOffsetRows = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "offset"),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit2 = ElementTools.generateMethodBody(
|
||||
setLimit2,
|
||||
setLimitByOffsetRows = ElementTools.generateMethodBody(
|
||||
setLimitByOffsetRows,
|
||||
"this.offset = offset;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit2);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetRows);
|
||||
|
||||
Method usePage = ElementTools.generateMethod(
|
||||
"usePage",
|
||||
@@ -204,15 +176,15 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
);
|
||||
usePage = ElementTools.generateMethodBody(
|
||||
usePage,
|
||||
"pageSize = pageSize == null || pageSize <= 0 ? this.defaultPageSize : pageSize;",
|
||||
"pageNum = pageNum == null || pageNum < this.minPageNum ? this.minPageNum : pageNum;",
|
||||
"pageSize = pageSize == null || pageSize <= 0 ? 1 : pageSize;",
|
||||
"pageNum = pageNum == null || pageNum < this.startPageNum ? this.startPageNum : pageNum;",
|
||||
"if (pageSize >= this.ignorePageSize) {",
|
||||
"this.rows = null;",
|
||||
"this.offset = null;",
|
||||
"return this;",
|
||||
"}",
|
||||
"int cPageSize = pageSize > this.maxPageSize ? this.maxPageSize: pageSize;",
|
||||
"this.offset = (pageNum - this.minPageNum) * cPageSize;",
|
||||
"this.offset = (pageNum - this.startPageNum) * cPageSize;",
|
||||
"this.rows = cPageSize;",
|
||||
"return this;"
|
||||
);
|
||||
@@ -227,9 +199,9 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
getPageNum = ElementTools.generateMethodBody(
|
||||
getPageNum,
|
||||
"if (this.rows == null || this.offset == null || this.rows == 0) {",
|
||||
"return this.minPageNum;",
|
||||
"return this.startPageNum;",
|
||||
"}",
|
||||
"return this.offset / this.rows + this.minPageNum;"
|
||||
"return this.offset / this.rows + this.startPageNum;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum);
|
||||
// 计算获取当前每页数量
|
||||
@@ -247,6 +219,28 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize);
|
||||
|
||||
Method getRows = ElementTools.generateMethod(
|
||||
"getRows",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getRows = ElementTools.generateMethodBody(
|
||||
getRows,
|
||||
"return this.rows;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getRows);
|
||||
|
||||
Method getOffset = ElementTools.generateMethod(
|
||||
"getOffset",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getOffset = ElementTools.generateMethodBody(
|
||||
getOffset,
|
||||
"return this.offset;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getOffset);
|
||||
|
||||
// !!! clear 方法增加 offset 和 rows的清理
|
||||
List<Method> methodList = topLevelClass.getMethods();
|
||||
for (Method method : methodList) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.iqudoo.framework.mybatis;
|
||||
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import com.iqudoo.framework.mybatis.utils.UtilTools;
|
||||
import org.mybatis.generator.api.*;
|
||||
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -11,13 +12,14 @@ import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@SuppressWarnings({"DuplicatedCode", "unused", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 固定配置项
|
||||
private String slowQueryLoggerTime = "300";
|
||||
private String priorityPrimaryKeyOffset = "0";
|
||||
private String slowQueryLoggerLevel = "error";
|
||||
private String snowflakeUtilClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil";
|
||||
private String snowflakeUtilGenId = "SnowflakeUtil.nextId()";
|
||||
private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository";
|
||||
@@ -30,49 +32,51 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
// 1.4.1版本专用:Java格式化器
|
||||
private JavaFormatter javaFormatter;
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
this.javaFormatter = new DefaultJavaFormatter();
|
||||
this.javaFormatter.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
// 读取自定义配置
|
||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
this.javaFormatter = new DefaultJavaFormatter();
|
||||
this.javaFormatter.setContext(context);
|
||||
resolveConfiguration();
|
||||
}
|
||||
|
||||
private void resolveConfiguration() {
|
||||
slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime);
|
||||
slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel);
|
||||
priorityPrimaryKeyOffset = stringConfig("priorityPrimaryKeyOffset", priorityPrimaryKeyOffset);
|
||||
if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) {
|
||||
slowQueryLoggerLevel = "error";
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilClass"))) {
|
||||
snowflakeUtilClass = properties.getProperty("snowflakeUtilClass");
|
||||
snowflakeUtilClass = stringConfig("snowflakeUtilClass", snowflakeUtilClass);
|
||||
snowflakeUtilGenId = stringConfig("snowflakeUtilGenId", snowflakeUtilGenId);
|
||||
facadeRepositoryPackage = stringConfig("facadeRepositoryPackage", facadeRepositoryPackage);
|
||||
domainRepositoryPackage = stringConfig("domainRepositoryPackage", domainRepositoryPackage);
|
||||
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||
targetProject = stringConfig("targetProject", targetProject);
|
||||
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||
viewKeyWords = viewKeyWords.toUpperCase();
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilGenId"))) {
|
||||
snowflakeUtilGenId = properties.getProperty("snowflakeUtilGenId");
|
||||
}
|
||||
|
||||
private String stringConfig(String key, String defaultValue) {
|
||||
String v = properties.getProperty(key);
|
||||
if (StringUtility.stringHasValue(v)) {
|
||||
return v;
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepositoryPackage"))) {
|
||||
facadeRepositoryPackage = properties.getProperty("facadeRepositoryPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("domainRepositoryPackage"))) {
|
||||
domainRepositoryPackage = properties.getProperty("domainRepositoryPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("modelPackage"))) {
|
||||
modelPackage = properties.getProperty("modelPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("mapperPackage"))) {
|
||||
mapperPackage = properties.getProperty("mapperPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("targetProject"))) {
|
||||
targetProject = properties.getProperty("targetProject");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("viewKeyWords"))) {
|
||||
viewKeyWords = properties.getProperty("viewKeyWords").toUpperCase();
|
||||
if (context != null) {
|
||||
v = context.getProperty(key);
|
||||
if (StringUtility.stringHasValue(v)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,7 +290,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
countByValidMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(countByValidMethod);
|
||||
|
||||
// 15. countByTrash
|
||||
// 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"));
|
||||
@@ -295,7 +308,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
countByTrashMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(countByTrashMethod);
|
||||
|
||||
// 16. insert
|
||||
// 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
|
||||
Method insertMethod = new Method("insert");
|
||||
insertMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
insertMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
@@ -304,7 +326,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
insertMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(insertMethod);
|
||||
|
||||
// 17. update
|
||||
// 19. update
|
||||
Method updateMethod = new Method("update");
|
||||
updateMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
updateMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
@@ -313,7 +335,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
updateMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(updateMethod);
|
||||
|
||||
// 17. updateByExampleSelective
|
||||
// 20. updateByExampleSelective
|
||||
Method updateByExampleSelectiveMethod = new Method("updateByExampleSelective");
|
||||
updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
@@ -379,8 +401,10 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
generateFindTrashOneMethod(implClass, modelClassName, exampleClassName);
|
||||
generateGetValidListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||
generateGetTrashListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||
generateCountByValidMethod(implClass, exampleClassName, mapperFieldName);
|
||||
generateCountByTrashMethod(implClass, exampleClassName, mapperFieldName);
|
||||
generateCountByValidMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
generateCountByValidWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
generateCountByTrashMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
generateCountByTrashWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
|
||||
return implClass;
|
||||
}
|
||||
@@ -798,43 +822,48 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("List<" + modelClassName + "> result = null;");
|
||||
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();");
|
||||
}
|
||||
method.addBodyLine("example = new " + exampleClassName + "();");
|
||||
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
||||
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
||||
}
|
||||
method.addBodyLine("}");
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {");
|
||||
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." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid list primary key use long time: \" + findPrimaryKeyTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
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("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
method.addBodyLine("result = " + 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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid list use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return result;");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -850,47 +879,52 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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();");
|
||||
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();");
|
||||
}
|
||||
method.addBodyLine("example = new " + exampleClassName + "();");
|
||||
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
||||
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
||||
}
|
||||
method.addBodyLine("}");
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {");
|
||||
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." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash list primary key use long time: \" + findPrimaryKeyTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
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("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
method.addBodyLine("result = " + 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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash list use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return result;");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
private void generateCountByValidMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
||||
private void generateCountByValidMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
Method method = new Method("countByValid");
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -902,19 +936,38 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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 count = " + mapperFieldName + ".countByExample(example);");
|
||||
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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid count use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return count;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
private void generateCountByTrashMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
||||
private void generateCountByValidWithPageMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
Method method = new Method("countByValidWithPage");
|
||||
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 countByValid(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return 0L;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
private void generateCountByTrashMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
Method method = new Method("countByTrash");
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -926,14 +979,33 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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 count = " + mapperFieldName + ".countByExample(example);");
|
||||
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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash count use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return count;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
private void generateCountByTrashWithPageMethod(TopLevelClass implClass, 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("}");
|
||||
method.addBodyLine("return 0L;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.iqudoo.framework.mybatis;
|
||||
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import com.iqudoo.framework.mybatis.utils.UtilTools;
|
||||
import org.mybatis.generator.api.GeneratedJavaFile;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.JavaFormatter;
|
||||
@@ -14,62 +15,65 @@ import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@SuppressWarnings({"DuplicatedCode", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||
public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 视图Repo包配置(可通过配置文件自定义)
|
||||
private String slowQueryLoggerTime = "300";
|
||||
private String slowQueryLoggerLevel = "error";
|
||||
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";
|
||||
private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
|
||||
private String targetProject = "src/main/java";
|
||||
|
||||
// 视图名称关键字(可配置)
|
||||
private String viewKeyWords = "VIEW_,V_";
|
||||
|
||||
// 1.4.1版本专用格式化器
|
||||
private JavaFormatter javaFormatter;
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
this.javaFormatter = new DefaultJavaFormatter();
|
||||
this.javaFormatter.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
// 读取自定义配置
|
||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
this.javaFormatter = new DefaultJavaFormatter();
|
||||
this.javaFormatter.setContext(context);
|
||||
resolveConfiguration();
|
||||
}
|
||||
|
||||
private void resolveConfiguration() {
|
||||
slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime);
|
||||
slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel);
|
||||
if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) {
|
||||
slowQueryLoggerLevel = "error";
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepoviewPackage"))) {
|
||||
facadeRepoviewPackage = properties.getProperty("facadeRepoviewPackage");
|
||||
facadeRepoviewPackage = stringConfig("facadeRepoviewPackage", facadeRepoviewPackage);
|
||||
domainRepoviewPackage = stringConfig("domainRepoviewPackage", domainRepoviewPackage);
|
||||
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||
targetProject = stringConfig("targetProject", targetProject);
|
||||
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||
viewKeyWords = viewKeyWords.toUpperCase();
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("domainRepoviewPackage"))) {
|
||||
domainRepoviewPackage = properties.getProperty("domainRepoviewPackage");
|
||||
}
|
||||
|
||||
private String stringConfig(String key, String defaultValue) {
|
||||
String v = properties.getProperty(key);
|
||||
if (StringUtility.stringHasValue(v)) {
|
||||
return v;
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("modelPackage"))) {
|
||||
modelPackage = properties.getProperty("modelPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("mapperPackage"))) {
|
||||
mapperPackage = properties.getProperty("mapperPackage");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("targetProject"))) {
|
||||
targetProject = properties.getProperty("targetProject");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("viewKeyWords"))) {
|
||||
viewKeyWords = properties.getProperty("viewKeyWords").toUpperCase();
|
||||
if (context != null) {
|
||||
v = context.getProperty(key);
|
||||
if (StringUtility.stringHasValue(v)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,6 +187,15 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
countMethod.setAbstract(true);
|
||||
repoInterface.addMethod(countMethod);
|
||||
|
||||
// 4. 添加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);
|
||||
repoInterface.addMethod(countWithPageMethod);
|
||||
|
||||
return repoInterface;
|
||||
}
|
||||
|
||||
@@ -225,12 +238,10 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
mapperField.addAnnotation("@Resource");
|
||||
implClass.addField(mapperField);
|
||||
|
||||
// 生成findOne方法
|
||||
generateFindOneMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
// 生成getList方法
|
||||
generateFindOneMethod(implClass, modelClassName, exampleClassName);
|
||||
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||
// 生成count方法
|
||||
generateCountMethod(implClass, exampleClassName, mapperFieldName);
|
||||
generateCountMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
generateCountWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
|
||||
return implClass;
|
||||
}
|
||||
@@ -251,14 +262,14 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
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"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成findOne方法
|
||||
*/
|
||||
private void generateFindOneMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
private void generateFindOneMethod(TopLevelClass implClass, String modelClassName, String exampleClassName) {
|
||||
Method method = new Method("findOne");
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -289,29 +300,33 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
// 参数名匹配示例(首字母小写)
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
method.addBodyLine("List<" + modelClassName + "> result = null;");
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("result = " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("} else {");
|
||||
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
method.addBodyLine("result = " + 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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " view list use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return result;");
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成count方法
|
||||
*/
|
||||
private void generateCountMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
||||
private void generateCountMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
Method method = new Method("count");
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -322,14 +337,38 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("long startTime = new Date().getTime();");
|
||||
method.addBodyLine("try {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
method.addBodyLine("} finally {");
|
||||
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
|
||||
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("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " view count use long time: \" + useTime + \"ms\" +");
|
||||
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||
method.addBodyLine(");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return count;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成count方法
|
||||
*/
|
||||
private void generateCountWithPageMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
Method method = new Method("countWithPage");
|
||||
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 count(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return 0L;");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.iqudoo.framework.mybatis.utils;
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
public class UtilTools {
|
||||
|
||||
public static <T> boolean inArray(T[] array, T target) {
|
||||
for (T val : array) {
|
||||
if (val != null && val.equals(target)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user