mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-07 01:18:23 +08:00
增加插入式注解@GenerateMilvusMapper,给实体类自动生成对应的 Mapper和属性Constants
This commit is contained in:
parent
3178dede4f
commit
59cb185ff0
@ -30,6 +30,18 @@
|
|||||||
<version>${mica-auto.vaersion}</version>
|
<version>${mica-auto.vaersion}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup</groupId>
|
||||||
|
<artifactId>javapoet</artifactId>
|
||||||
|
<version>${javapoet.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auto.service</groupId>
|
||||||
|
<artifactId>auto-service</artifactId>
|
||||||
|
<version>${auto.service.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
package org.dromara.milvus.plus.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
public @interface GenerateMilvusMapper {
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,106 @@
|
|||||||
|
package org.dromara.milvus.plus.processor;
|
||||||
|
|
||||||
|
import com.google.auto.service.AutoService;
|
||||||
|
import com.squareup.javapoet.*;
|
||||||
|
import org.dromara.milvus.plus.annotation.MilvusField;
|
||||||
|
|
||||||
|
import javax.annotation.processing.*;
|
||||||
|
import javax.lang.model.SourceVersion;
|
||||||
|
import javax.lang.model.element.*;
|
||||||
|
import javax.lang.model.util.Elements;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@SupportedAnnotationTypes("org.dromara.milvus.plus.annotation.GenerateMilvusMapper")
|
||||||
|
@SupportedSourceVersion(SourceVersion.RELEASE_8)
|
||||||
|
@AutoService(Processor.class)
|
||||||
|
public class GenerateMilvusMapperProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
|
for (TypeElement annotation : annotations) {
|
||||||
|
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
|
||||||
|
if (element.getKind() == ElementKind.CLASS) {
|
||||||
|
TypeElement typeElement = (TypeElement) element;
|
||||||
|
generateMilvusMapperClass(typeElement);
|
||||||
|
generateConstantsClass(typeElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateMilvusMapperClass(TypeElement typeElement) {
|
||||||
|
// 获取元素实用工具和类型实用工具
|
||||||
|
Elements elementUtils = processingEnv.getElementUtils();
|
||||||
|
// 获取实体类的包名和类名
|
||||||
|
String packageName = elementUtils.getPackageOf(typeElement).getQualifiedName().toString();
|
||||||
|
String className = typeElement.getSimpleName().toString();
|
||||||
|
String mapperClassName = className + "MilvusMapper";
|
||||||
|
// 创建泛型参数
|
||||||
|
TypeName entityTypeName = ClassName.get(packageName, className);
|
||||||
|
// 创建Mapper类的TypeSpec
|
||||||
|
TypeSpec mapperClass = TypeSpec.classBuilder(mapperClassName)
|
||||||
|
.addModifiers(Modifier.PUBLIC)
|
||||||
|
.superclass(ParameterizedTypeName.get(
|
||||||
|
ClassName.get("org.dromara.milvus.plus.mapper", "MilvusMapper"),
|
||||||
|
entityTypeName))
|
||||||
|
.addAnnotation(AnnotationSpec.builder(ClassName.get("org.springframework.stereotype", "Component")).build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 创建JavaFile
|
||||||
|
JavaFile javaFile = JavaFile.builder(packageName, mapperClass)
|
||||||
|
.addFileComment("Generated by @GenerateMilvusMapper")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 写入文件
|
||||||
|
javaFile.writeTo(processingEnv.getFiler());
|
||||||
|
} catch (IOException e) {
|
||||||
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error generating Milvus mapper class: " + e.getMessage(), typeElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void generateConstantsClass(TypeElement typeElement) {
|
||||||
|
Elements elementUtils = processingEnv.getElementUtils();
|
||||||
|
String packageName = elementUtils.getPackageOf(typeElement).getQualifiedName().toString();
|
||||||
|
String className = typeElement.getSimpleName().toString();
|
||||||
|
String constantsClassName = className + "Constants";
|
||||||
|
List<FieldSpec> constantsFields = new ArrayList<>();
|
||||||
|
// 遍历实体类的所有字段
|
||||||
|
for (Element enclosedElement : typeElement.getEnclosedElements()) {
|
||||||
|
if (enclosedElement.getKind() == ElementKind.FIELD) {
|
||||||
|
VariableElement field = (VariableElement) enclosedElement;
|
||||||
|
// 检查字段是否有 MilvusField 注解
|
||||||
|
MilvusField milvusField = field.getAnnotation(MilvusField.class);
|
||||||
|
if (milvusField != null) {
|
||||||
|
String constantName = !milvusField.name().isEmpty() ? milvusField.name() : field.getSimpleName().toString();
|
||||||
|
// 确保常量名称是大写的
|
||||||
|
String upperCaseConstantName = constantName.toUpperCase();
|
||||||
|
constantsFields.add(FieldSpec.builder(String.class, upperCaseConstantName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
|
||||||
|
.initializer("$S", constantName)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 如果有注解字段,则创建 Constants 类
|
||||||
|
if (!constantsFields.isEmpty()) {
|
||||||
|
TypeSpec constantsClass = TypeSpec.classBuilder(constantsClassName)
|
||||||
|
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
|
||||||
|
.addFields(constantsFields)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
JavaFile javaFile = JavaFile.builder(packageName, constantsClass)
|
||||||
|
.addFileComment("Generated by @GenerateMilvusMapperProcessor")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
javaFile.writeTo(processingEnv.getFiler());
|
||||||
|
} catch (IOException e) {
|
||||||
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error generating constants class: " + e.getMessage(), typeElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -42,6 +42,8 @@
|
|||||||
<spring-boot.version>2.7.13</spring-boot.version>
|
<spring-boot.version>2.7.13</spring-boot.version>
|
||||||
<mica-auto.vaersion>2.3.2</mica-auto.vaersion>
|
<mica-auto.vaersion>2.3.2</mica-auto.vaersion>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
<javapoet.version>1.13.0</javapoet.version>
|
||||||
|
<auto.service.version>1.0</auto.service.version>
|
||||||
<nexus-staging-maven-plugin.version>1.6.7</nexus-staging-maven-plugin.version>
|
<nexus-staging-maven-plugin.version>1.6.7</nexus-staging-maven-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user