生成的代码支持在exmaple里面设置setWithBOLBs控制查询结果是否返回长文本内容
This commit is contained in:
13
pom.xml
13
pom.xml
@@ -55,19 +55,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
|
||||
Binary file not shown.
@@ -1,15 +1,296 @@
|
||||
package com.iqudoo.framework.mybatis;
|
||||
|
||||
import com.iqudoo.framework.mybatis.abstracts.AbstractWithLimitPlugin;
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import com.iqudoo.framework.mybatis.utils.FormatTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
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.internal.util.StringUtility;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TapeMybatisGeneratorPlugin extends AbstractWithLimitPlugin {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(List<String> list) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
|
||||
// 添加 minPageNum、defaultPageSize、maxPageSize、ignorePageSize 字段
|
||||
Field maxPageSizeField = ElementTools.generateField(
|
||||
"maxPageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"100"
|
||||
);
|
||||
topLevelClass.addField(maxPageSizeField);
|
||||
|
||||
Field ignorePageSizeField = ElementTools.generateField(
|
||||
"ignorePageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
this.ignorePageSize + ""
|
||||
);
|
||||
topLevelClass.addField(ignorePageSizeField);
|
||||
|
||||
Field defaultPageSizeField = ElementTools.generateField(
|
||||
"defaultPageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
defaultPageSize + ""
|
||||
);
|
||||
topLevelClass.addField(defaultPageSizeField);
|
||||
|
||||
Field minPageNumField = ElementTools.generateField(
|
||||
"minPageNum",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
startPage + ""
|
||||
);
|
||||
topLevelClass.addField(minPageNumField);
|
||||
|
||||
// 添加offset和rows字段
|
||||
Field offsetField = ElementTools.generateField(
|
||||
"offset",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"null"
|
||||
);
|
||||
topLevelClass.addField(offsetField);
|
||||
|
||||
Field rowsField = ElementTools.generateField(
|
||||
"rows",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"null"
|
||||
);
|
||||
topLevelClass.addField(rowsField);
|
||||
|
||||
boolean hasBLOBColumns = ElementTools.hasBLOBColumns(introspectedTable);
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
Field withBLOBsField = ElementTools.generateField(
|
||||
"withBLOBs",
|
||||
JavaVisibility.PROTECTED,
|
||||
new FullyQualifiedJavaType("java.lang.Boolean"),
|
||||
"true"
|
||||
);
|
||||
topLevelClass.addField(withBLOBsField);
|
||||
Method mSetWithBLOBs = ElementTools.generateSetterMethod(withBLOBsField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetWithBLOBs);
|
||||
Method isWithBLOBsMethod = new Method("isWithBLOBs");
|
||||
isWithBLOBsMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
isWithBLOBsMethod.setReturnType(new FullyQualifiedJavaType("boolean"));
|
||||
isWithBLOBsMethod.addBodyLine("return this.withBLOBs != null && this.withBLOBs;");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, isWithBLOBsMethod);
|
||||
}
|
||||
|
||||
|
||||
// 增加getter && setter 方法
|
||||
Method mSetMaxPageSize = ElementTools.generateSetterMethod(maxPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMaxPageSize);
|
||||
|
||||
Method mGetMaxPageSize = ElementTools.generateGetterMethod(maxPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetMaxPageSize);
|
||||
|
||||
Method mSetIgnorePageSize = ElementTools.generateSetterMethod(ignorePageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetIgnorePageSize);
|
||||
|
||||
Method mGetIgnorePageSize = ElementTools.generateGetterMethod(ignorePageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetIgnorePageSize);
|
||||
|
||||
Method mSetDefaultPageSize = ElementTools.generateSetterMethod(defaultPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetDefaultPageSize);
|
||||
|
||||
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 setLimit = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit = ElementTools.generateMethodBody(
|
||||
setLimit,
|
||||
"this.offset = null;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit);
|
||||
|
||||
Method setLimit2 = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "offset"),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit2 = ElementTools.generateMethodBody(
|
||||
setLimit2,
|
||||
"this.offset = offset;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit2);
|
||||
|
||||
Method usePage = ElementTools.generateMethod(
|
||||
"usePage",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "pageNum"),
|
||||
new Parameter(integerWrapper, "pageSize")
|
||||
);
|
||||
usePage = ElementTools.generateMethodBody(
|
||||
usePage,
|
||||
"pageSize = pageSize == null || pageSize <= 0 ? this.defaultPageSize : pageSize;",
|
||||
"pageNum = pageNum == null || pageNum < this.minPageNum ? this.minPageNum : 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.rows = cPageSize;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, usePage);
|
||||
|
||||
// 计算获取当前页码
|
||||
Method getPageNum = ElementTools.generateMethod(
|
||||
"getPageNum",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getPageNum = ElementTools.generateMethodBody(
|
||||
getPageNum,
|
||||
"if (this.rows == null || this.offset == null || this.rows == 0) {",
|
||||
"return this.minPageNum;",
|
||||
"}",
|
||||
"return this.offset / this.rows + this.minPageNum;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum);
|
||||
// 计算获取当前每页数量
|
||||
Method getPageSize = ElementTools.generateMethod(
|
||||
"getPageSize",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getPageSize = ElementTools.generateMethodBody(
|
||||
getPageSize,
|
||||
"if (this.rows == null) {",
|
||||
"return this.ignorePageSize;",
|
||||
"}",
|
||||
"return this.rows;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize);
|
||||
|
||||
// !!! clear 方法增加 offset 和 rows的清理
|
||||
List<Method> methodList = topLevelClass.getMethods();
|
||||
for (Method method : methodList) {
|
||||
if (method.getName().equals("clear")) {
|
||||
method.addBodyLine("rows = null;");
|
||||
method.addBodyLine("offset = null;");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectAllElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectAllElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
public void generateLimitElement(XmlElement element) {
|
||||
XmlElement ifLimitNotNullElement = new XmlElement("if");
|
||||
ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null"));
|
||||
// rows
|
||||
XmlElement ifOffsetNotNullElement = new XmlElement("if");
|
||||
ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
|
||||
ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${rows}"));
|
||||
ifLimitNotNullElement.addElement(ifOffsetNotNullElement);
|
||||
// offset
|
||||
XmlElement ifOffsetNullElement = new XmlElement("if");
|
||||
ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
|
||||
ifOffsetNullElement.addElement(new TextElement("limit ${rows}"));
|
||||
ifLimitNotNullElement.addElement(ifOffsetNullElement);
|
||||
element.addElement(ifLimitNotNullElement);
|
||||
}
|
||||
|
||||
// ============================================ selectPrimaryKeyByExample ==========================================
|
||||
|
||||
@@ -87,5 +368,4 @@ public class TapeMybatisGeneratorPlugin extends AbstractWithLimitPlugin {
|
||||
return super.sqlMapDocumentGenerated(document, introspectedTable);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.iqudoo.framework.mybatis;
|
||||
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import org.mybatis.generator.api.*;
|
||||
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -15,7 +16,7 @@ import java.util.Properties;
|
||||
/**
|
||||
* MyBatis Generator 1.4.1
|
||||
*/
|
||||
@SuppressWarnings({"DuplicatedCode", "unused"})
|
||||
@SuppressWarnings({"DuplicatedCode", "unused", "SpellCheckingInspection"})
|
||||
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 固定配置项
|
||||
@@ -92,7 +93,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
String mapperClassName = domainObjectName + "Mapper";
|
||||
String repositoryInterfaceName = "I" + domainObjectName + "Repository";
|
||||
String repositoryImplName = domainObjectName + "RepositoryImpl";
|
||||
boolean hasBLOBColumns = hasBLOBColumns(introspectedTable);
|
||||
boolean hasBLOBColumns = ElementTools.hasBLOBColumns(introspectedTable);
|
||||
|
||||
// 生成Repository接口(核心修改:手动添加所有方法,不再继承父接口)
|
||||
Interface repositoryInterface = generateRepositoryInterface(repositoryInterfaceName, domainObjectName, exampleClassName);
|
||||
@@ -156,9 +157,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
repositoryInterface.addImportedType(new FullyQualifiedJavaType(modelPackage + "." + exampleClassName));
|
||||
repositoryInterface.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
||||
|
||||
// 定义方法参数名(首字母小写)
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
|
||||
// 1. trashById
|
||||
Method trashByIdMethod = new Method("trashById");
|
||||
trashByIdMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -172,7 +170,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method trashAllMethod = new Method("trashAll");
|
||||
trashAllMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
trashAllMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
trashAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
trashAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
trashAllMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
trashAllMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(trashAllMethod);
|
||||
@@ -191,7 +189,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method deleteAllMethod = new Method("deleteAll");
|
||||
deleteAllMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
deleteAllMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
deleteAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
deleteAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
deleteAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "release"));
|
||||
deleteAllMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
deleteAllMethod.setAbstract(true);
|
||||
@@ -210,7 +208,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method recoverAllMethod = new Method("recoverAll");
|
||||
recoverAllMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
recoverAllMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
recoverAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
recoverAllMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
recoverAllMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
recoverAllMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(recoverAllMethod);
|
||||
@@ -246,7 +244,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method findValidOneMethod = new Method("findValidOne");
|
||||
findValidOneMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
findValidOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
findValidOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
findValidOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
findValidOneMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
findValidOneMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(findValidOneMethod);
|
||||
@@ -255,7 +253,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method findTrashOneMethod = new Method("findTrashOne");
|
||||
findTrashOneMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
findTrashOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
findTrashOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
findTrashOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
findTrashOneMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
findTrashOneMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(findTrashOneMethod);
|
||||
@@ -264,7 +262,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method getValidListMethod = new Method("getValidList");
|
||||
getValidListMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
getValidListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">"));
|
||||
getValidListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
getValidListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
getValidListMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
getValidListMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(getValidListMethod);
|
||||
@@ -273,7 +271,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method getTrashListMethod = new Method("getTrashList");
|
||||
getTrashListMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
getTrashListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">"));
|
||||
getTrashListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
getTrashListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
getTrashListMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
getTrashListMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(getTrashListMethod);
|
||||
@@ -282,7 +280,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method countByValidMethod = new Method("countByValid");
|
||||
countByValidMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
countByValidMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||
countByValidMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
countByValidMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
countByValidMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
countByValidMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(countByValidMethod);
|
||||
@@ -291,7 +289,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
Method countByTrashMethod = new Method("countByTrash");
|
||||
countByTrashMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
countByTrashMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||
countByTrashMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
countByTrashMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
countByTrashMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
countByTrashMethod.setAbstract(true);
|
||||
repositoryInterface.addMethod(countByTrashMethod);
|
||||
@@ -373,22 +371,21 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "release"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("if (release) {");
|
||||
method.addBodyLine(" return " + mapperFieldName + ".deleteByExample(" + exampleParamName + ");");
|
||||
method.addBodyLine("return " + mapperFieldName + ".deleteByExample(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsDelete(1);");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", " + exampleParamName + ");");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -398,12 +395,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine("criteria.andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
@@ -411,7 +407,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(1);");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setDeleteToken(" + lowerFirst(modelClassName) + ".getGuid() + \"\");");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", " + exampleParamName + ");");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -421,12 +417,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType("int"));
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine("criteria.andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
@@ -434,7 +429,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(0);");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setDeleteToken(\"VALID\");");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", " + exampleParamName + ");");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -480,15 +475,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasBLOBColumns(IntrospectedTable introspectedTable) {
|
||||
for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
|
||||
if (column.isBLOBColumn()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void addImportPackages(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperClassName, String interfaceName) {
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(snowflakeUtilClass));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
|
||||
@@ -724,13 +710,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
String exampleParamName = exampleClassName.substring(0, 1).toLowerCase() + exampleClassName.substring(1);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine(exampleParamName + ".usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getValidList(" + exampleParamName + ");");
|
||||
method.addBodyLine("if (dataList != null && dataList.size() > 0) {");
|
||||
method.addBodyLine("example.usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getValidList(example);");
|
||||
method.addBodyLine("if (dataList != null && !dataList.isEmpty()) {");
|
||||
method.addBodyLine("return dataList.get(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return null;");
|
||||
@@ -743,13 +728,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
String exampleParamName = exampleClassName.substring(0, 1).toLowerCase() + exampleClassName.substring(1);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine(exampleParamName + ".usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getTrashList(" + exampleParamName + ");");
|
||||
method.addBodyLine("if (dataList != null && dataList.size() > 0) {");
|
||||
method.addBodyLine("example.usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getTrashList(example);");
|
||||
method.addBodyLine("if (dataList != null && !dataList.isEmpty()) {");
|
||||
method.addBodyLine("return dataList.get(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return null;");
|
||||
@@ -772,18 +756,29 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||
method.addBodyLine(" if (primaryKeyList == null || primaryKeyList.size() == 0) {");
|
||||
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
||||
method.addBodyLine("return new ArrayList<>();");
|
||||
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("}");
|
||||
|
||||
String selectMethod = hasBLOBColumns ? "selectByExampleWithBLOBs" : "selectByExample";
|
||||
method.addBodyLine("return " + mapperFieldName + "." + selectMethod + "(example);");
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -802,18 +797,29 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||
method.addBodyLine(" if (primaryKeyList == null || primaryKeyList.size() == 0) {");
|
||||
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
||||
method.addBodyLine("return new ArrayList<>();");
|
||||
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("}");
|
||||
|
||||
String selectMethod = hasBLOBColumns ? "selectByExampleWithBLOBs" : "selectByExample";
|
||||
method.addBodyLine("return " + mapperFieldName + "." + selectMethod + "(example);");
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -826,8 +832,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
|
||||
@@ -843,8 +848,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.iqudoo.framework.mybatis;
|
||||
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import org.mybatis.generator.api.GeneratedJavaFile;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.JavaFormatter;
|
||||
@@ -18,7 +19,7 @@ import java.util.Properties;
|
||||
/**
|
||||
* MyBatis Generator 1.4.1 适配版:视图表专用 RepoView 生成插件
|
||||
*/
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
@SuppressWarnings({"DuplicatedCode", "SpellCheckingInspection"})
|
||||
public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 视图Repo包配置(可通过配置文件自定义)
|
||||
@@ -90,6 +91,8 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
String repoInterfaceName = "I" + domainObjectName + "Repo";
|
||||
String repoImplName = domainObjectName + "RepoImpl";
|
||||
|
||||
boolean hasBLOBColumns = ElementTools.hasBLOBColumns(introspectedTable);
|
||||
|
||||
// 2. 生成视图Repo接口(核心修改:移除继承,手动添加方法)
|
||||
Interface repoInterface = generateRepoViewInterface(repoInterfaceName, domainObjectName, exampleClassName);
|
||||
GeneratedJavaFile interfaceFile = new GeneratedJavaFile(
|
||||
@@ -106,7 +109,8 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
repoInterfaceName,
|
||||
domainObjectName,
|
||||
exampleClassName,
|
||||
mapperClassName
|
||||
mapperClassName,
|
||||
hasBLOBColumns
|
||||
);
|
||||
GeneratedJavaFile implFile = new GeneratedJavaFile(
|
||||
repoImpl,
|
||||
@@ -151,14 +155,11 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
repoInterface.addImportedType(new FullyQualifiedJavaType(modelPackage + "." + exampleClassName));
|
||||
repoInterface.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
||||
|
||||
// 定义方法参数名(首字母小写)
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
|
||||
// 1. 添加findOne方法
|
||||
Method findOneMethod = new Method("findOne");
|
||||
findOneMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
findOneMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
findOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
findOneMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
findOneMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
findOneMethod.setAbstract(true);
|
||||
repoInterface.addMethod(findOneMethod);
|
||||
@@ -167,7 +168,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
Method getListMethod = new Method("getList");
|
||||
getListMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
getListMethod.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">"));
|
||||
getListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
getListMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
getListMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
getListMethod.setAbstract(true);
|
||||
repoInterface.addMethod(getListMethod);
|
||||
@@ -176,7 +177,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
Method countMethod = new Method("count");
|
||||
countMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||
countMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||
countMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
countMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
countMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
countMethod.setAbstract(true);
|
||||
repoInterface.addMethod(countMethod);
|
||||
@@ -192,7 +193,9 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
String interfaceName,
|
||||
String modelClassName,
|
||||
String exampleClassName,
|
||||
String mapperClassName) {
|
||||
String mapperClassName,
|
||||
boolean hasBLOBColumns
|
||||
) {
|
||||
|
||||
TopLevelClass implClass = new TopLevelClass(domainRepoviewPackage + "." + implClassName);
|
||||
implClass.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -216,7 +219,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
// 生成findOne方法
|
||||
generateFindOneMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
// 生成getList方法
|
||||
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||
// 生成count方法
|
||||
generateCountMethod(implClass, exampleClassName, mapperFieldName);
|
||||
|
||||
@@ -249,14 +252,13 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||
// 参数名匹配示例(首字母小写)
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine(exampleParamName + ".usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getList(" + exampleParamName + ");");
|
||||
method.addBodyLine("if (dataList != null && dataList.size() > 0) {");
|
||||
method.addBodyLine("example.usePage(1, 1);");
|
||||
method.addBodyLine("List<" + modelClassName + "> dataList = getList(example);");
|
||||
method.addBodyLine("if (dataList != null && !dataList.isEmpty()) {");
|
||||
method.addBodyLine("return dataList.get(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return null;");
|
||||
@@ -267,19 +269,22 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
/**
|
||||
* 生成getList方法
|
||||
*/
|
||||
private void generateGetListMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||
private void generateGetListMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName, boolean hasBLOBColumns) {
|
||||
Method method = new Method("getList");
|
||||
method.addAnnotation("@Override");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType("List<" + modelClassName + ">"));
|
||||
// 参数名匹配示例(首字母小写)
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(" + exampleParamName + ");");
|
||||
|
||||
if (hasBLOBColumns) {
|
||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
} else {
|
||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
||||
}
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
|
||||
@@ -292,12 +297,11 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(new FullyQualifiedJavaType("long"));
|
||||
// 参数名匹配示例(首字母小写)
|
||||
String exampleParamName = lowerFirst(exampleClassName);
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), exampleParamName));
|
||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(" + exampleParamName + ");");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -355,14 +359,4 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
return str.substring(0, 1).toLowerCase() + str.substring(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串首字母大写(备用)
|
||||
*/
|
||||
private String upperFirst(String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,261 +0,0 @@
|
||||
package com.iqudoo.framework.mybatis.abstracts;
|
||||
|
||||
import com.iqudoo.framework.mybatis.hook.ISelectSelectivePluginHook;
|
||||
import com.iqudoo.framework.mybatis.utils.FormatTools;
|
||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractWithLimitPlugin extends PluginAdapter implements ISelectSelectivePluginHook {
|
||||
|
||||
private final static int DEFAULT_START_PAGE = 1;
|
||||
private final static int DEFAULT_IGNORE_PAGE_SIZE = 10000;
|
||||
private int startPage = 1;
|
||||
private int ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
||||
|
||||
@Override
|
||||
public boolean validate(List<String> list) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
super.initialized(introspectedTable);
|
||||
this.startPage = DEFAULT_START_PAGE;
|
||||
String ignorePageSizeProperty = this.properties.getProperty("ignorePageSize");
|
||||
if (ignorePageSizeProperty != null && ignorePageSizeProperty.trim().length() > 0) {
|
||||
try {
|
||||
this.ignorePageSize = Integer.parseInt(ignorePageSizeProperty.trim());
|
||||
} catch (NumberFormatException ignored) {
|
||||
this.ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
||||
}
|
||||
} else {
|
||||
this.ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
|
||||
// 添加 minPageNum、defaultPageSize、maxPageSize、ignorePageSize 字段
|
||||
Field maxPageSizeField = ElementTools.generateField(
|
||||
"maxPageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"100"
|
||||
);
|
||||
topLevelClass.addField(maxPageSizeField);
|
||||
|
||||
Field ignorePageSizeField = ElementTools.generateField(
|
||||
"ignorePageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
this.ignorePageSize + ""
|
||||
);
|
||||
topLevelClass.addField(ignorePageSizeField);
|
||||
|
||||
Field defaultPageSizeField = ElementTools.generateField(
|
||||
"defaultPageSize",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"20"
|
||||
);
|
||||
topLevelClass.addField(defaultPageSizeField);
|
||||
|
||||
Field minPageNumField = ElementTools.generateField(
|
||||
"minPageNum",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
startPage + ""
|
||||
);
|
||||
topLevelClass.addField(minPageNumField);
|
||||
|
||||
// 添加offset和rows字段
|
||||
Field offsetField = ElementTools.generateField(
|
||||
"offset",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"null"
|
||||
);
|
||||
topLevelClass.addField(offsetField);
|
||||
|
||||
Field rowsField = ElementTools.generateField(
|
||||
"rows",
|
||||
JavaVisibility.PROTECTED,
|
||||
integerWrapper,
|
||||
"null"
|
||||
);
|
||||
topLevelClass.addField(rowsField);
|
||||
|
||||
// 增加getter && setter 方法
|
||||
Method mSetMaxPageSize = ElementTools.generateSetterMethod(maxPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMaxPageSize);
|
||||
|
||||
Method mGetMaxPageSize = ElementTools.generateGetterMethod(maxPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetMaxPageSize);
|
||||
|
||||
Method mSetIgnorePageSize = ElementTools.generateSetterMethod(ignorePageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetIgnorePageSize);
|
||||
|
||||
Method mGetIgnorePageSize = ElementTools.generateGetterMethod(ignorePageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetIgnorePageSize);
|
||||
|
||||
Method mSetDefaultPageSize = ElementTools.generateSetterMethod(defaultPageSizeField);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetDefaultPageSize);
|
||||
|
||||
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 setLimit = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit = ElementTools.generateMethodBody(
|
||||
setLimit,
|
||||
"this.offset = null;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit);
|
||||
|
||||
Method setLimit2 = ElementTools.generateMethod(
|
||||
"limit",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "offset"),
|
||||
new Parameter(integerWrapper, "rows")
|
||||
);
|
||||
setLimit2 = ElementTools.generateMethodBody(
|
||||
setLimit2,
|
||||
"this.offset = offset;",
|
||||
"this.rows = rows;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit2);
|
||||
|
||||
Method usePage = ElementTools.generateMethod(
|
||||
"usePage",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType(),
|
||||
new Parameter(integerWrapper, "pageNum"),
|
||||
new Parameter(integerWrapper, "pageSize")
|
||||
);
|
||||
usePage = ElementTools.generateMethodBody(
|
||||
usePage,
|
||||
"pageSize = pageSize == null || pageSize <= 0 ? this.defaultPageSize : pageSize;",
|
||||
"pageNum = pageNum == null || pageNum < this.minPageNum ? this.minPageNum : 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.rows = cPageSize;",
|
||||
"return this;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, usePage);
|
||||
|
||||
// 计算获取当前页码
|
||||
Method getPageNum = ElementTools.generateMethod(
|
||||
"getPageNum",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getPageNum = ElementTools.generateMethodBody(
|
||||
getPageNum,
|
||||
"if (this.rows == null || this.offset == null || this.rows == 0) {",
|
||||
"return this.minPageNum;",
|
||||
"}",
|
||||
"return this.offset / this.rows + this.minPageNum;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum);
|
||||
// 计算获取当前每页数量
|
||||
Method getPageSize = ElementTools.generateMethod(
|
||||
"getPageSize",
|
||||
JavaVisibility.PUBLIC,
|
||||
integerWrapper
|
||||
);
|
||||
getPageSize = ElementTools.generateMethodBody(
|
||||
getPageSize,
|
||||
"if (this.rows == null) {",
|
||||
"return this.ignorePageSize;",
|
||||
"}",
|
||||
"return this.rows;"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize);
|
||||
|
||||
// !!! clear 方法增加 offset 和 rows的清理
|
||||
List<Method> methodList = topLevelClass.getMethods();
|
||||
for (Method method : methodList) {
|
||||
if (method.getName().equals("clear")) {
|
||||
method.addBodyLine("rows = null;");
|
||||
method.addBodyLine("offset = null;");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectAllElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectAllElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element);
|
||||
return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateLimitElement(XmlElement element) {
|
||||
XmlElement ifLimitNotNullElement = new XmlElement("if");
|
||||
ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null"));
|
||||
// rows
|
||||
XmlElement ifOffsetNotNullElement = new XmlElement("if");
|
||||
ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
|
||||
ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${rows}"));
|
||||
ifLimitNotNullElement.addElement(ifOffsetNotNullElement);
|
||||
// offset
|
||||
XmlElement ifOffsetNullElement = new XmlElement("if");
|
||||
ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
|
||||
ifOffsetNullElement.addElement(new TextElement("limit ${rows}"));
|
||||
ifLimitNotNullElement.addElement(ifOffsetNullElement);
|
||||
element.addElement(ifLimitNotNullElement);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.iqudoo.framework.mybatis.hook;
|
||||
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
|
||||
public interface ISelectSelectivePluginHook {
|
||||
|
||||
void generateLimitElement(XmlElement element);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.iqudoo.framework.mybatis.utils;
|
||||
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
|
||||
@@ -8,6 +9,21 @@ import static org.mybatis.generator.internal.util.messages.Messages.getString;
|
||||
@SuppressWarnings("unused")
|
||||
public class ElementTools {
|
||||
|
||||
/**
|
||||
* 检测数据表是否含有BLOB列数据
|
||||
*
|
||||
* @param introspectedTable introspectedTable
|
||||
* @return 是/否
|
||||
*/
|
||||
public static boolean hasBLOBColumns(IntrospectedTable introspectedTable) {
|
||||
for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
|
||||
if (column.isBLOBColumn()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成静态常量
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user