org.mybatis.generator.api.plus.MybatisPluginController Maven / Gradle / Ivy
The newest version!
package org.mybatis.generator.api.plus;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author wangfupeng
*/
public class MybatisPluginController extends PluginAdapter {
private FullyQualifiedJavaType slf4jLogger;
private FullyQualifiedJavaType slf4jLoggerFactory;
private FullyQualifiedJavaType autowired;
private FullyQualifiedJavaType mediaType;
private FullyQualifiedJavaType pathVariable;
private FullyQualifiedJavaType requestBody;
private FullyQualifiedJavaType requestHeader;
private FullyQualifiedJavaType requestMapping;
private FullyQualifiedJavaType postMapping;
private FullyQualifiedJavaType getMapping;
private FullyQualifiedJavaType requestMethod;
private FullyQualifiedJavaType requestParam;
private FullyQualifiedJavaType restController;
private FullyQualifiedJavaType authUser;
private FullyQualifiedJavaType logging;
private FullyQualifiedJavaType listResponse;
private FullyQualifiedJavaType simpleResponse;
private FullyQualifiedJavaType alphaListResponse;
private FullyQualifiedJavaType alphaSimpleResponse;
private FullyQualifiedJavaType api;
private FullyQualifiedJavaType apiOperation;
private FullyQualifiedJavaType apiParam;
private FullyQualifiedJavaType controllerType;
private FullyQualifiedJavaType serviceType;
private FullyQualifiedJavaType distinctServiceType;
private FullyQualifiedJavaType modelType;
private FullyQualifiedJavaType modelExampleType;
private FullyQualifiedJavaType requestType;
private String basePackage;
private String aspectPackage;
private String servicePackage;
private String controllerPackage;
private String authAopClassName;
private String project;
private String modelUrl;
private String enableGenerateHtml;
private String htmlPath;
private String responseType;
private String supportFrontPage;
private String supportBackPage;
private FullyQualifiedJavaType stringType;
private FullyQualifiedJavaType integerType;
private FullyQualifiedJavaType booleanType;
private FullyQualifiedJavaType byteType;
private FullyQualifiedJavaType doubleType;
private FullyQualifiedJavaType floatType;
private FullyQualifiedJavaType longType;
private FullyQualifiedJavaType shortType;
private FullyQualifiedJavaType bigDecimalType;
private FullyQualifiedJavaType dateType;
private FullyQualifiedJavaType authAopType;
private FullyQualifiedJavaType javaUtilListType;
private FullyQualifiedJavaType javaUtilMapType;
private FullyQualifiedJavaType javaUtilHashMapType;
private String createDistinctController;
/**
* 所有的方法
*/
private List methods;
/**
* 是否添加注解
*/
public MybatisPluginController() {
super();
methods = new ArrayList();
slf4jLogger = new FullyQualifiedJavaType("org.slf4j.Logger");
slf4jLoggerFactory = new FullyQualifiedJavaType("org.slf4j.LoggerFactory");
autowired = new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired");
mediaType = new FullyQualifiedJavaType("org.springframework.http.MediaType");
pathVariable = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.PathVariable");
requestBody = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestBody");
requestMapping = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMapping");
postMapping = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.PostMapping");
getMapping = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.GetMapping");
requestMethod = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMethod");
requestParam = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestParam");
requestHeader = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestHeader");
restController = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RestController");
api = new FullyQualifiedJavaType("io.swagger.annotations.Api");
apiOperation = new FullyQualifiedJavaType("io.swagger.annotations.ApiOperation");
apiParam = new FullyQualifiedJavaType("io.swagger.annotations.ApiParam");
stringType = new FullyQualifiedJavaType("java.lang.String");
integerType = new FullyQualifiedJavaType("java.lang.Integer");
booleanType = new FullyQualifiedJavaType("java.lang.Boolean");
byteType = new FullyQualifiedJavaType("java.lang.Byte");
doubleType = new FullyQualifiedJavaType("java.lang.Double");
floatType = new FullyQualifiedJavaType("java.lang.Float");
longType = new FullyQualifiedJavaType("java.lang.Long");
shortType = new FullyQualifiedJavaType("java.lang.Short");
bigDecimalType = new FullyQualifiedJavaType("java.lang.BigDecimal");
dateType = new FullyQualifiedJavaType("java.util.Date");
requestType = new FullyQualifiedJavaType("javax.servlet.http.HttpServletRequest");
this.javaUtilListType = new FullyQualifiedJavaType("java.util.List");
this.javaUtilMapType = new FullyQualifiedJavaType("java.util.Map");
this.javaUtilHashMapType = new FullyQualifiedJavaType("java.util.HashMap");
}
/**
* 读取配置文件
*/
@Override
public boolean validate(List warnings) {
this.modelUrl = context.getJavaModelGeneratorConfiguration().getTargetPackage();
this.basePackage = properties.getProperty("basePackage");
this.servicePackage = properties.getProperty("servicePackage");
this.project = properties.getProperty("targetProject");
this.controllerPackage = properties.getProperty("controllerPackage");
this.authAopClassName = properties.getProperty("authAopClassName");
if (authAopClassName == null) {
authAopClassName = "AuthUser";
}
this.aspectPackage = properties.getProperty("aspectPackage");
this.enableGenerateHtml = properties.getProperty("enableGenerateHtml");
this.htmlPath = properties.getProperty("htmlPath");
this.responseType = properties.getProperty("responseType");
this.supportFrontPage = properties.getProperty("supportFrontPage").toLowerCase();
this.supportBackPage = properties.getProperty("supportBackPage").toLowerCase();
this.createDistinctController = properties.getProperty("createDistinctController");
this.authUser = new FullyQualifiedJavaType(aspectPackage + ".AuthUser");
this.logging = new FullyQualifiedJavaType("com.gitee.fufu669.aspect.Logging");
this.simpleResponse = new FullyQualifiedJavaType("com.gitee.fufu669.response.SimpleResponse");
this.listResponse = new FullyQualifiedJavaType("com.gitee.fufu669.response.ListResponse");
this.alphaSimpleResponse = new FullyQualifiedJavaType("com.gitee.fufu669.response.AlphaSimpleResponse");
this.alphaListResponse = new FullyQualifiedJavaType("com.gitee.fufu669.response.AlphaListResponse");
return true;
}
@Override
public List contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
List files = new ArrayList<>();
String table = introspectedTable.getBaseRecordType();
String tableName = table.replaceAll(this.modelUrl + ".", "");
if (tableName.equals("Admin")) {
MybatisUtilLoginController.writeLoginController(basePackage, project, servicePackage, responseType);
}
serviceType = new FullyQualifiedJavaType(servicePackage + "." + tableName + "Service");
// distinctServiceType = new FullyQualifiedJavaType(servicePackage + ".distinct.Distinct" + tableName + "Service");
modelType = new FullyQualifiedJavaType(modelUrl + "." + tableName);
modelExampleType = new FullyQualifiedJavaType(modelUrl + "." + tableName + "Example");
controllerType = new FullyQualifiedJavaType(controllerPackage + ".Common" + tableName + "Controller");
authAopType = new FullyQualifiedJavaType(aspectPackage + "." + authAopClassName);
TopLevelClass topLevelClass = new TopLevelClass(controllerType);
addImport(topLevelClass);
addLogger(topLevelClass);
addService(topLevelClass, introspectedTable, tableName, files);
return files;
}
/**
* import must class 导入必要的class
*/
private void addImport(TopLevelClass topLevelClass) {
// addImportedType是往一个set里面加数据,所以可以重复添加。
// 导入必须的类
topLevelClass.addImportedType(slf4jLogger);
topLevelClass.addImportedType(slf4jLoggerFactory);
topLevelClass.addImportedType(autowired);
topLevelClass.addImportedType(mediaType);
topLevelClass.addImportedType(pathVariable);
topLevelClass.addImportedType(requestBody);
topLevelClass.addImportedType(requestHeader);
topLevelClass.addImportedType(requestMapping);
topLevelClass.addImportedType(getMapping);
topLevelClass.addImportedType(postMapping);
topLevelClass.addImportedType(requestMethod);
topLevelClass.addImportedType(requestParam);
topLevelClass.addImportedType(restController);
topLevelClass.addImportedType(authUser);
topLevelClass.addImportedType(logging);
topLevelClass.addImportedType(api);
topLevelClass.addImportedType(apiOperation);
topLevelClass.addImportedType(apiParam);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
topLevelClass.addImportedType(alphaSimpleResponse);
topLevelClass.addImportedType(alphaListResponse);
} else {
topLevelClass.addImportedType(simpleResponse);
topLevelClass.addImportedType(listResponse);
}
topLevelClass.addImportedType(stringType);
topLevelClass.addImportedType(integerType);
topLevelClass.addImportedType(serviceType);
topLevelClass.addImportedType(modelType);
topLevelClass.addImportedType(modelExampleType);
topLevelClass.addImportedType(controllerType);
topLevelClass.addImportedType(requestType);
topLevelClass.addImportedType(javaUtilListType);
topLevelClass.addImportedType(javaUtilMapType);
topLevelClass.addImportedType(javaUtilHashMapType);
topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.ArrayList"));
topLevelClass.addImportedType(authAopType);
}
/**
* public static final Logger logger =
* LoggerFactory.getLogger(UserLabelReturnFieldController.class);
*/
private void addLogger(TopLevelClass topLevelClass) {
Field field = new Field();
field.setFinal(true);
field.setInitializationString("LoggerFactory.getLogger(" + topLevelClass.getType().getShortName() + ".class)");
field.setName("logger");
field.setStatic(true);
field.setType(new FullyQualifiedJavaType("Logger"));
field.setVisibility(JavaVisibility.PUBLIC);
topLevelClass.addField(field);
}
/**
* add TopClass and All Method
*/
protected void addService(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName,
List files) {
topLevelClass.setVisibility(JavaVisibility.PUBLIC);
// set service
topLevelClass.addAnnotation("@SuppressWarnings({\"unchecked\",\"rawtypes\"})");
topLevelClass.addAnnotation("@Api(description = \"脚手架自动生成的通用的增删改查列表" + introspectedTable.getRemarks() + "接口\")");
topLevelClass.addAnnotation("@RestController");
topLevelClass.addAnnotation("@RequestMapping(value = \"/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable)
+ "\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)");
topLevelClass.addSuperInterface(logging);
/** add import mapper */
addField(topLevelClass);
/** add method */
topLevelClass.addMethod(createModelMethod(introspectedTable, tableName));
String pathForPrimaryKeyColumns = MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable);
if (!"".equals(pathForPrimaryKeyColumns)) {
topLevelClass.addMethod(deleteByPrimaryKeyMethod(introspectedTable, tableName));
topLevelClass.addMethod(deleteSetStatusByPrimaryKeyMethod(introspectedTable, tableName));
if (MybatisUtilCommon.getExistNonePrimaryKeyColumns(introspectedTable)) {
topLevelClass.addMethod(updateMethod(introspectedTable, tableName));
topLevelClass.addMethod(selectByPrimaryKeyMethod(introspectedTable, tableName));
if (supportFrontPage.equals("yes")) {
topLevelClass.addMethod(selectWithoutSensitiveByPrimaryKeyMethod(introspectedTable, tableName));
topLevelClass.addMethod(selectWithoutAuthUserWithoutSensitiveByPrimaryKeyMethod(introspectedTable, tableName));
}
topLevelClass.addMethod(selectByPrimaryKeyMockMethod(introspectedTable, tableName));
}
}
topLevelClass.addMethod(list(introspectedTable, tableName));
topLevelClass.addMethod(listMockWithoutAuthUser(introspectedTable, tableName));
topLevelClass.addMethod(listWithoutCache(introspectedTable, tableName));
topLevelClass.addMethod(listWithMock(introspectedTable, tableName));
if (supportBackPage.equals("yes")) {
topLevelClass.addMethod(query(introspectedTable, tableName));
topLevelClass.addMethod(queryWithoutCache(introspectedTable, tableName));
}
if (supportFrontPage.equals("yes")) {
List introspectedColumns = introspectedTable.getAllColumns();
for (int i = 0; i < introspectedColumns.size(); i++) {
IntrospectedColumn introspectedColumn = introspectedColumns.get(i);
if (introspectedColumn.getActualColumnName().endsWith("_id")) {
topLevelClass.addMethod(listQueryWithCacheFetchLater(introspectedTable, tableName, MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedColumn.getJavaProperty()),
introspectedColumn.getRemarks().split(":")[0], introspectedColumn.getActualColumnName()));
topLevelClass.addMethod(listQueryReturnVoWithCacheFetchLater(introspectedTable, tableName, MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedColumn.getJavaProperty()),
introspectedColumn.getRemarks().split(":")[0], introspectedColumn.getActualColumnName(), topLevelClass));
topLevelClass.addMethod(listQueryWithoutAuthUserAndSensitiveReturnVoWithCacheFetchLater(introspectedTable, tableName, MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedColumn.getJavaProperty()),
introspectedColumn.getRemarks().split(":")[0], introspectedColumn.getActualColumnName(), topLevelClass));
}
}
}
GeneratedJavaFile file = new GeneratedJavaFile(topLevelClass, project, "UTF-8", context.getJavaFormatter());
files.add(file);
if ("true".equals(createDistinctController)) {
MybatisUtilDistinctController.writeDistinctController(basePackage, aspectPackage, project, topLevelClass, introspectedTable, tableName, servicePackage, controllerPackage, responseType);
}
}
/**
* @Autowired private UserLabelReturnFieldMapper userLabelReturnFieldMapper;
*/
protected void addField(TopLevelClass topLevelClass) {
Field field = new Field();
field.setName(getServiceShort());
field.setType(serviceType);
field.setVisibility(JavaVisibility.PRIVATE);
field.addAnnotation("@Autowired");
topLevelClass.addField(field);
}
protected Method createModelMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"增加" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@PostMapping(value = \"/create\")");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.setName("create");
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
method.addParameter(new Parameter(modelType, MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName), "@RequestBody"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回已创建结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".create(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "));");
} else {
method.addBodyLine("//返回已创建结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".create(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "));");
}
return method;
}
private String getServiceShort() {
return MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName());
}
protected Method selectWithoutAuthUserWithoutSensitiveByPrimaryKeyMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@ApiOperation(\"无鉴权脱敏查询" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/get/without/auth/user/and/sensitive" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("getWithoutAuthUserAndSensitive");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//自动缓存查看数据");
method.addBodyLine(introspectedTable.getFullyQualifiedTable().getDomainObjectName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + " = " + getServiceShort() + ".getWithCacheFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + ");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + ");");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + ");");
}
return method;
}
protected Method selectWithoutSensitiveByPrimaryKeyMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"脱敏查询" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/get/without/sensitive" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("getWithoutSensitive");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//自动缓存查看数据");
method.addBodyLine(introspectedTable.getFullyQualifiedTable().getDomainObjectName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + " = " + getServiceShort() + ".getWithCacheFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + ");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + ");");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + ");");
}
return method;
}
protected Method selectByPrimaryKeyMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"查询" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/get" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("get");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".getWithCacheFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".getWithCacheFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
}
return method;
}
protected Method selectByPrimaryKeyMockMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"快速查询" + introspectedTable.getRemarks() + "缓存\")");
method.addAnnotation("@GetMapping(value = \"/mock" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("mock");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".getCacheMockFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".getCacheMockFetchLater(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
}
return method;
}
private Method updateMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"更改" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@PostMapping(value = \"/update\")");
method.setName("update");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
method.addParameter(new Parameter(modelType, MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName), "@RequestBody"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".update(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "));");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".update(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "));");
}
return method;
}
private Method deleteSetStatusByPrimaryKeyMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"设置删除状态" + introspectedTable.getRemarks() + ",传入id, 返回data : 成功 : 1, 失败 : 0\")");
method.addAnnotation("@GetMapping(value = \"/delete/set/status" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("deleteSetStatus");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".deleteSetStatus(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".deleteSetStatus(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
}
return method;
}
private Method deleteByPrimaryKeyMethod(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"直接删除" + introspectedTable.getRemarks() + ",传入id, 返回data : 成功 : 1, 失败 : 0\")");
method.addAnnotation("@GetMapping(value = \"/delete" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\")");
method.setName("delete");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaSimpleResponse);
} else {
method.setReturnType(simpleResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
MybatisUtilCommon.addParameterWithAnnotationForPrimaryKeyColumnsInMethodSelectByPrimaryKey(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaSimpleResponse.success(" + getServiceShort() + ".delete(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return SimpleResponse.success(" + getServiceShort() + ".delete(" + MybatisUtilCommon.getParameterValueForPrimaryKeyColumns(introspectedTable) + "));");
}
return method;
}
private Method listWithMock(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"查询缓存列表,返回缓存数据,然后去后台查询进缓存,后面再次查询返回缓存数据" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list/with/mock\")");
method.setName("listWithMock");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
MybatisUtilCommon.writeStatusEqualTo(method, statuses, modelExampleType);
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) +
".countByExampleCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count,page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count,page, limit);");
}
return method;
}
private Method listWithoutCache(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"没有缓存查询列表" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list/without/cache\")");
method.setName("listWithoutCache");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
MybatisUtilCommon.writeStatusEqualTo(method, statuses, modelExampleType);
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) +
".countByExampleWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count,page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count,page, limit);");
}
return method;
}
private Method list(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"有缓存查询列表" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list\")");
method.setName("list");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
MybatisUtilCommon.writeStatusEqualTo(method, statuses, modelExampleType);
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
}
return method;
}
private Method listMockWithoutAuthUser(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@ApiOperation(\"没有验证token,直接查询缓存列表,返回缓存数据,然后去后台查询进缓存,后面再次查询返回缓存数据" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list/mock/without/auth/user\")");
method.setName("listMockWithoutAuthUser");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
MybatisUtilCommon.writeStatusEqualTo(method, statuses, modelExampleType);
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listCacheMockFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
}
return method;
}
private Method query(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"带条件查询列表" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/query\")");
method.setName("query");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
MybatisUtilCommon.addParameterForColumnsWithMaxMinPrefixSuffixLike(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//创建查询标准");
method.addBodyLine(
modelExampleType.getShortName() + ".Criteria criteria = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".createCriteria();");
method.addBodyLine("//组装查询标准");
MybatisUtilCommon.addCriteriaForColumnsInMethod(method, introspectedTable);
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("if(page<1){page=1;}");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
}
return method;
}
private Method queryWithoutCache(IntrospectedTable introspectedTable, String tableName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"带条件查询列表" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/query/without/cache\")");
method.setName("queryWithoutCache");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
MybatisUtilCommon.addParameterForColumnsWithMaxMinPrefixSuffixLike(method, introspectedTable);
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//创建查询标准");
method.addBodyLine(
modelExampleType.getShortName() + ".Criteria criteria = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".createCriteria();");
method.addBodyLine("//组装查询标准");
MybatisUtilCommon.addCriteriaForColumnsInMethod(method, introspectedTable);
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine("if(page<1){page=1;}");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithoutCache(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
}
return method;
}
private Method listQueryWithCacheFetchLater(IntrospectedTable introspectedTable, String tableName, String javaProperty, String remark, String actualColumnName) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"有缓存查询特定查询条件的列表" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list/query/" + actualColumnName.replaceAll("_", "/") + "\")");
method.setName("listQuery" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(javaProperty));
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(longType, javaProperty, "@ApiParam(value = \""
+ remark + " \") @RequestParam(value = \"" + javaProperty + "\", required = true)"));
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//创建查询标准");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
method.addBodyLine("//创建查询标准");
method.addBodyLine(modelExampleType.getShortName() + ".Criteria criteria = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".createCriteria();");
method.addBodyLine("//组装查询标准");
if (statuses.size() == 1) {
method.addBodyLine("if(status!=null){");
method.addBodyLine("criteria.and" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(statuses.get(0)) + "EqualTo(" + statuses.get(0) + ");");
method.addBodyLine("}");
}
method.addBodyLine("criteria.and" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(javaProperty) + "EqualTo(" + javaProperty + ");");
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("return AlphaListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
} else {
method.addBodyLine("return ListResponse.success(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")," +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
",page, limit);");
}
return method;
}
private Method listQueryReturnVoWithCacheFetchLater(IntrospectedTable introspectedTable, String tableName, String javaProperty, String remark, String actualColumnName, TopLevelClass topLevelClass) {
Method method = new Method();
method.addAnnotation("@" + authAopClassName);
method.addAnnotation("@ApiOperation(\"有缓存查询特定查询条件的列表,并返回特定的值。" + introspectedTable.getRemarks() + "\")");
method.addAnnotation("@GetMapping(value = \"/list/query/vo/" + actualColumnName.replaceAll("_", "/") + "\")");
method.setName("listQueryReturnVo" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(javaProperty));
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.setReturnType(alphaListResponse);
} else {
method.setReturnType(listResponse);
}
method.addParameter(new Parameter(stringType, "token", "@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false)"));
List introspectedColumns = introspectedTable.getAllColumns();
List statuses = new ArrayList<>();
List statusDescriptions = new ArrayList<>();
MybatisUtilCommon.getStatusesAndStatusDescriptions(introspectedColumns, statuses, statusDescriptions);
if (statuses.size() == 1) {
method.addParameter(new Parameter(integerType, "status", "@ApiParam(value = \""
+ statusDescriptions.get(0) + "\") @RequestParam(value = \"status\", required = false) "));
}
method.addParameter(new Parameter(longType, javaProperty, "@ApiParam(value = \""
+ remark + " \") @RequestParam(value = \"" + javaProperty + "\", required = true)"));
method.addParameter(new Parameter(integerType, "page", "@ApiParam(value = \""
+ " 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") "));
method.addParameter(new Parameter(integerType, "limit", "@ApiParam(value = \""
+ " 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\")"));
method.addParameter(new Parameter(stringType, "orderBy", "@ApiParam(value = \""
+ " 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\")"));
method.addParameter(new Parameter(requestType, "request", ""));
method.setVisibility(JavaVisibility.PUBLIC);
method.addBodyLine("//新申明一个查询实例");
method.addBodyLine(
modelExampleType.getShortName() + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName())
+ " = new " + modelExampleType.getShortName() + "();");
method.addBodyLine("//设置查找个数限制");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setLimit(limit);");
method.addBodyLine("//设置查找初始位置");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOffset((page-1)*limit);");
method.addBodyLine("//设置排序方式");
method.addBodyLine(MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".setOrderByClause(orderBy);");
method.addBodyLine("//创建查询标准");
method.addBodyLine(modelExampleType.getShortName() + ".Criteria criteria = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ".createCriteria();");
method.addBodyLine("//组装查询标准");
if (statuses.size() == 1) {
method.addBodyLine("if(status!=null){");
method.addBodyLine("criteria.and" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(statuses.get(0)) + "EqualTo(" + statuses.get(0) + ");");
method.addBodyLine("}");
}
method.addBodyLine("criteria.and" + MybatisUtilCommon.toUpperCaseAtFirstCharacter(javaProperty) + "EqualTo(" + javaProperty + ");");
method.addBodyLine("//获取总数");
method.addBodyLine("Integer " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
" = Integer.valueOf(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".countByExampleWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ")+\"\");");
String otherActualColumnName = "";
String otherJavaProperty = "";
for (int i = 0; i < introspectedColumns.size(); i++) {
IntrospectedColumn introspectedColumn = introspectedColumns.get(i);
if (introspectedColumn.getActualColumnName().endsWith("_id")) {
//遍历找到另一个_id
if (!introspectedColumn.getActualColumnName().equals(actualColumnName)) {
otherJavaProperty = introspectedColumn.getJavaProperty();
otherActualColumnName = introspectedColumn.getActualColumnName();
}
}
}
String listName = MybatisUtilCommon.toLowerCaseAtFirstCharacter(introspectedTable.getFullyQualifiedTable().getDomainObjectName()) + "s";
method.addBodyLine("//获取查询列表");
method.addBodyLine("List<" + introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "> " + listName + " = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + ".listWithCacheFetchLater(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(modelExampleType.getShortName()) + ");");
if ("".equals(otherActualColumnName)) {
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
method.addBodyLine("//返回结果");
method.addBodyLine("return AlphaListResponse.success(" + listName + ", " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
", page, limit);");
} else {
method.addBodyLine("//返回结果");
method.addBodyLine("return ListResponse.success(" + listName + ", " +
MybatisUtilCommon.toLowerCaseAtFirstCharacter(serviceType.getShortName()) + "Count" +
", page, limit);");
}
} else {
method.addBodyLine("//申明一个由map组成的list");
method.addBodyLine("List