findAndById

This commit is contained in:
Jeremy Liang
2026-02-04 04:47:08 +08:00
parent 3ec21f6f35
commit ce1514fd39
4 changed files with 33 additions and 34 deletions

View File

@@ -31,7 +31,7 @@
- `deleteAll({Example} example, boolean release)` - 删除(批量,支持物理删除)
- `recoverById(long id)` - 从回收站恢复(单个)
- `recoverAll({Example} example)` - 从回收站恢复(批量)
- `findNoWhereById(long id)` - 查找(不区分有效/回收站)
- `findAnyById(long id)` - 查找(不区分有效/回收站)
- `findValidById(long id)` - 查找有效记录(单个)
- `findTrashById(long id)` - 查找回收站记录(单个)
- `findValidOne({Example} example)` - 查找有效记录(单个,支持条件)

View File

@@ -215,14 +215,14 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
recoverAllMethod.setAbstract(true);
repositoryInterface.addMethod(recoverAllMethod);
// 7. findNoWhereById
Method findNoWhereByIdMethod = new Method("findNoWhereById");
findNoWhereByIdMethod.setVisibility(JavaVisibility.PUBLIC);
findNoWhereByIdMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
findNoWhereByIdMethod.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id"));
findNoWhereByIdMethod.addException(new FullyQualifiedJavaType("Throwable"));
findNoWhereByIdMethod.setAbstract(true);
repositoryInterface.addMethod(findNoWhereByIdMethod);
// 7. findAnyById
Method findAnyByIdMethod = new Method("findAnyById");
findAnyByIdMethod.setVisibility(JavaVisibility.PUBLIC);
findAnyByIdMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
findAnyByIdMethod.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id"));
findAnyByIdMethod.addException(new FullyQualifiedJavaType("Throwable"));
findAnyByIdMethod.setAbstract(true);
repositoryInterface.addMethod(findAnyByIdMethod);
// 8. findValidById
Method findValidByIdMethod = new Method("findValidById");
@@ -346,7 +346,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addField(mapperField);
// 原有方法生成逻辑(无修改)
generateFindNoWhereByIdMethod(implClass, modelClassName, exampleClassName);
generateFindAnyByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName);
generateFindValidByIdMethod(implClass, modelClassName, exampleClassName);
generateFindTrashByIdMethod(implClass, modelClassName, exampleClassName);
generateInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable);
@@ -439,23 +439,23 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateJavaFileToDisk(Interface intf, String packageName) {
private void generateJavaFileToDisk(Interface anInterface, String packageName) {
try {
String packagePath = packageName.replace('.', File.separatorChar);
File targetDir = new File(targetProject, packagePath);
if (!targetDir.exists()) {
boolean mkdirs = targetDir.mkdirs();
if (!mkdirs) {
throw new RuntimeException("创建目录失败:" + targetDir.getAbsolutePath());
throw new RuntimeException("Created dir: " + targetDir.getAbsolutePath());
}
}
String className = intf.getType().getShortName();
String className = anInterface.getType().getShortName();
File javaFile = new File(targetDir, className + ".java");
String content = javaFormatter.getFormattedContent(intf);
String content = javaFormatter.getFormattedContent(anInterface);
java.nio.file.Files.write(javaFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
System.out.println("成功生成接口文件:" + javaFile.getAbsolutePath());
System.out.println("Created: " + javaFile.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("生成Repository接口失败" + intf.getType().getShortName(), e);
throw new RuntimeException("Create failure: " + anInterface.getType().getShortName(), e);
}
}
@@ -466,7 +466,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
if (!targetDir.exists()) {
boolean mkdirs = targetDir.mkdirs();
if (!mkdirs) {
throw new RuntimeException("创建目录失败:" + targetDir.getAbsolutePath());
throw new RuntimeException("Created dir: " + targetDir.getAbsolutePath());
}
}
@@ -474,9 +474,9 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
File javaFile = new File(targetDir, className + ".java");
String content = javaFormatter.getFormattedContent(clazz);
java.nio.file.Files.write(javaFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
System.out.println("成功生成实现类文件:" + javaFile.getAbsolutePath());
System.out.println("Created: " + javaFile.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("生成Repository实现类失败" + clazz.getType().getShortName(), e);
throw new RuntimeException("Create failure: " + clazz.getType().getShortName(), e);
}
}
@@ -502,17 +502,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
}
private void generateFindNoWhereByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName) {
Method method = new Method("findNoWhereById");
private void generateFindAnyByIdMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName, String exampleClassName) {
Method method = new Method("findAnyById");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(new FullyQualifiedJavaType(modelClassName));
method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id"));
method.addException(new FullyQualifiedJavaType("Throwable"));
method.addBodyLine(modelClassName + " aDo = findValidById(id);");
method.addBodyLine("if (aDo == null) {");
method.addBodyLine(" aDo = findTrashById(id);");
method.addBodyLine(modelClassName + " aDo = " + mapperFieldName + ".selectByPrimaryKey(id);");
method.addBodyLine("if (aDo != null && aDo.getIsDelete() == 1) {");
method.addBodyLine(" return null;");
method.addBodyLine("}");
method.addBodyLine("return aDo;");

View File

@@ -305,23 +305,23 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
/**
* 手动将Java文件写入磁盘
*/
private void generateJavaFileToDisk(Interface intf, String packageName) {
private void generateJavaFileToDisk(Interface anInterface, String packageName) {
try {
String packagePath = packageName.replace('.', File.separatorChar);
File targetDir = new File(targetProject, packagePath);
if (!targetDir.exists()) {
boolean mkdirs = targetDir.mkdirs();
if (!mkdirs) {
throw new RuntimeException("创建目录失败:" + targetDir.getAbsolutePath());
throw new RuntimeException("Created dir: " + targetDir.getAbsolutePath());
}
}
String className = intf.getType().getShortName();
String className = anInterface.getType().getShortName();
File javaFile = new File(targetDir, className + ".java");
String content = javaFormatter.getFormattedContent(intf);
String content = javaFormatter.getFormattedContent(anInterface);
java.nio.file.Files.write(javaFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
System.out.println("成功生成视图Repo接口" + javaFile.getAbsolutePath());
System.out.println("Created: " + javaFile.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("生成视图Repo接口失败" + intf.getType().getShortName(), e);
throw new RuntimeException("Create failure: " + anInterface.getType().getShortName(), e);
}
}
@@ -332,16 +332,16 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
if (!targetDir.exists()) {
boolean mkdirs = targetDir.mkdirs();
if (!mkdirs) {
throw new RuntimeException("创建目录失败:" + targetDir.getAbsolutePath());
throw new RuntimeException("Created dir: " + targetDir.getAbsolutePath());
}
}
String className = clazz.getType().getShortName();
File javaFile = new File(targetDir, className + ".java");
String content = javaFormatter.getFormattedContent(clazz);
java.nio.file.Files.write(javaFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
System.out.println("成功生成视图Repo实现类" + javaFile.getAbsolutePath());
System.out.println("Created: " + javaFile.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("生成视图Repo实现类失败" + clazz.getType().getShortName(), e);
throw new RuntimeException("Create failure: " + clazz.getType().getShortName(), e);
}
}