init commit
This commit is contained in:
@@ -19,6 +19,8 @@ import java.util.Properties;
|
||||
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 固定配置项
|
||||
private String snowflakeUtilClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil";
|
||||
private String snowflakeUtilGenId = "SnowflakeUtil.nextId()";
|
||||
private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository";
|
||||
private String domainRepositoryPackage = "com.iqudoo.platform.application.domain.repository";
|
||||
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
||||
@@ -45,6 +47,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
public void setProperties(Properties properties) {
|
||||
super.setProperties(properties);
|
||||
// 读取自定义配置
|
||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilClass"))) {
|
||||
snowflakeUtilClass = properties.getProperty("snowflakeUtilClass");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilGenId"))) {
|
||||
snowflakeUtilGenId = properties.getProperty("snowflakeUtilGenId");
|
||||
}
|
||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepositoryPackage"))) {
|
||||
facadeRepositoryPackage = properties.getProperty("facadeRepositoryPackage");
|
||||
}
|
||||
@@ -331,14 +339,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType superInterface = new FullyQualifiedJavaType(facadeRepositoryPackage + "." + interfaceName);
|
||||
implClass.addSuperInterface(superInterface);
|
||||
|
||||
// 修复Logger字段:添加static final修饰符
|
||||
Field loggerField = new Field("LOGGER", new FullyQualifiedJavaType("org.slf4j.Logger"));
|
||||
loggerField.setVisibility(JavaVisibility.PRIVATE);
|
||||
loggerField.setStatic(true);
|
||||
loggerField.setFinal(true);
|
||||
loggerField.setInitializationString("Tape.getLogger(" + implClassName + ".class)");
|
||||
implClass.addField(loggerField);
|
||||
|
||||
String mapperFieldName = lowerFirst(mapperClassName);
|
||||
Field mapperField = new Field(mapperFieldName, new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
|
||||
mapperField.setVisibility(JavaVisibility.PRIVATE);
|
||||
@@ -383,10 +383,10 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine(" return " + mapperFieldName + ".deleteByExample(" + exampleParamName + ");");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsDelete(DatabaseCommonDataIsDeleteEnum.DELETED.getValue());");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsDelete(1);");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", " + exampleParamName + ");");
|
||||
|
||||
@@ -404,11 +404,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine(" criteria.andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(DatabaseCommonDataIsHiddenEnum.HIDDEN.getValue());");
|
||||
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 + ");");
|
||||
@@ -427,11 +427,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
// 方法体
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : " + exampleParamName + ".getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.HIDDEN.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0);");
|
||||
method.addBodyLine(" criteria.andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
|
||||
method.addBodyLine(lowerFirst(modelClassName) + ".setIsHidden(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
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 + ");");
|
||||
@@ -490,16 +490,11 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
}
|
||||
|
||||
private void addImportPackages(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperClassName, String interfaceName) {
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("com.iqudoo.framework.tape.Tape"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("com.iqudoo.framework.tape.modules.utils.SnowflakeUtil"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("com.iqudoo.framework.tape.modules.crud.DatabaseCommonDataIsDeleteEnum"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("com.iqudoo.framework.tape.modules.crud.DatabaseCommonDataIsHiddenEnum"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("com.iqudoo.framework.tape.modules.crud.DatabaseErrorConstants"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(snowflakeUtilClass));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(modelPackage + "." + modelClassName));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(modelPackage + "." + exampleClassName));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType(facadeRepositoryPackage + "." + interfaceName));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.Logger"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
|
||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.ArrayList"));
|
||||
@@ -566,7 +561,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("if (record.getGuid() != null) {");
|
||||
method.addBodyLine(" aDo.setGuid(record.getGuid());");
|
||||
method.addBodyLine("} else {");
|
||||
method.addBodyLine(" Long guid = SnowflakeUtil.nextId();");
|
||||
method.addBodyLine(" Long guid = " + snowflakeUtilGenId + ";");
|
||||
method.addBodyLine(" aDo.setGuid(guid);");
|
||||
method.addBodyLine("}");
|
||||
|
||||
@@ -583,8 +578,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("aDo." + setterMethod + "(record." + getterMethod + "());");
|
||||
}
|
||||
|
||||
method.addBodyLine("aDo.setIsDelete(DatabaseCommonDataIsDeleteEnum.NONE.getValue());");
|
||||
method.addBodyLine("aDo.setIsHidden(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
method.addBodyLine("aDo.setIsDelete(0);");
|
||||
method.addBodyLine("aDo.setIsHidden(0);");
|
||||
method.addBodyLine("aDo.setDeleteToken(\"VALID\");");
|
||||
method.addBodyLine("aDo.setDataVersion(1);");
|
||||
method.addBodyLine("aDo.setCreateTime(new Date());");
|
||||
@@ -595,13 +590,12 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine(" return aDo;");
|
||||
method.addBodyLine("}");
|
||||
|
||||
method.addBodyLine("// 更新当前数据版本号和GUID");
|
||||
method.addBodyLine("// optimistic locking with data version and guid");
|
||||
method.addBodyLine("record.setGuid(aDo.getGuid());");
|
||||
method.addBodyLine("record.setDataVersion(aDo.getDataVersion());");
|
||||
method.addBodyLine("record.setCreateTime(aDo.getCreateTime());");
|
||||
method.addBodyLine("record.setUpdateTime(aDo.getUpdateTime());");
|
||||
method.addBodyLine("LOGGER.error(\"Database write failed, " + modelClassName + ": {}\", aDo);");
|
||||
method.addBodyLine("throw DatabaseErrorConstants.DATABASE_WRITE_ERROR;");
|
||||
method.addBodyLine("throw new Throwable(\"Database write failed, " + modelClassName + "\");");
|
||||
|
||||
implClass.addMethod(method);
|
||||
}
|
||||
@@ -617,7 +611,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
|
||||
method.addBodyLine(modelClassName + " aDo = findValidById(record.getGuid());");
|
||||
method.addBodyLine("if (aDo == null) {");
|
||||
method.addBodyLine(" throw DatabaseErrorConstants.DATABASE_RECORD_NOT_FOUND;");
|
||||
method.addBodyLine(" throw new Throwable(\"Database record not found, " + modelClassName + " guid:\" + record.getGuid());");
|
||||
method.addBodyLine("}");
|
||||
|
||||
for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
|
||||
@@ -645,7 +639,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);");
|
||||
method.addBodyLine("aDo.setDataVersion(aDo.getDataVersion() + 1);");
|
||||
method.addBodyLine("aDo.setUpdateTime(new Date());");
|
||||
method.addBodyLine("// 更新当前数据版本号");
|
||||
method.addBodyLine("// update data version");
|
||||
method.addBodyLine("record.setDataVersion(aDo.getDataVersion());");
|
||||
method.addBodyLine("record.setUpdateTime(aDo.getUpdateTime());");
|
||||
|
||||
@@ -671,7 +665,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("if (release) {");
|
||||
method.addBodyLine(" return " + mapperFieldName + ".deleteByPrimaryKey(aDo.getGuid());");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("aDo.setIsDelete(DatabaseCommonDataIsDeleteEnum.DELETED.getValue());");
|
||||
method.addBodyLine("aDo.setIsDelete(1);");
|
||||
method.addBodyLine("aDo.setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByPrimaryKey(aDo);");
|
||||
|
||||
@@ -690,7 +684,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("if (aDo == null) {");
|
||||
method.addBodyLine(" return 0;");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("aDo.setIsHidden(DatabaseCommonDataIsHiddenEnum.HIDDEN.getValue());");
|
||||
method.addBodyLine("aDo.setIsHidden(1);");
|
||||
method.addBodyLine("aDo.setDeleteToken(aDo.getGuid() + \"\");");
|
||||
method.addBodyLine("aDo.setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByPrimaryKey(aDo);");
|
||||
@@ -710,10 +704,10 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addBodyLine("if (aDo == null) {");
|
||||
method.addBodyLine(" return 0;");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("if (aDo.getIsDelete() == DatabaseCommonDataIsDeleteEnum.DELETED.getValue()) {");
|
||||
method.addBodyLine("if (aDo.getIsDelete() == 1) {");
|
||||
method.addBodyLine(" return 0;");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("aDo.setIsHidden(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
method.addBodyLine("aDo.setIsHidden(0);");
|
||||
method.addBodyLine("aDo.setDeleteToken(\"VALID\");");
|
||||
method.addBodyLine("aDo.setUpdateTime(new Date());");
|
||||
method.addBodyLine("return " + mapperFieldName + ".updateByPrimaryKey(aDo);");
|
||||
@@ -769,8 +763,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue())");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||
method.addBodyLine(" List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||
@@ -799,8 +793,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue())");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.HIDDEN.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||
method.addBodyLine(" List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||
@@ -828,8 +822,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue())");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.NONE.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(0);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
|
||||
@@ -845,8 +839,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||
|
||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(DatabaseCommonDataIsDeleteEnum.NONE.getValue())");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(DatabaseCommonDataIsHiddenEnum.HIDDEN.getValue());");
|
||||
method.addBodyLine(" criteria.andIsDeleteEqualTo(0)");
|
||||
method.addBodyLine(" .andIsHiddenEqualTo(1);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user