mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-24 23:41:21 +08:00
代码生成支持机构索引
This commit is contained in:
43
script/addIndex.groovy
Normal file
43
script/addIndex.groovy
Normal file
@@ -0,0 +1,43 @@
|
||||
import groovy.io.FileType
|
||||
|
||||
|
||||
def camelToSnake(String str) {
|
||||
|
||||
str = str[0].toLowerCase() + str[1..-1]
|
||||
if (str.endsWith("Entity")) {
|
||||
str = str.substring(0, str.length() - 6)
|
||||
}
|
||||
|
||||
return str.replaceAll("([A-Z]+)", "_\$1").toLowerCase()
|
||||
}
|
||||
|
||||
|
||||
new File("../src/main/java").eachFileRecurse(FileType.FILES) {
|
||||
|
||||
if (it.name.endsWith(".java")) {
|
||||
def content = it.text
|
||||
|
||||
|
||||
if (content.contains("extends OrgCommonEntity")) {
|
||||
|
||||
if (content.contains("@Table")) {
|
||||
|
||||
println("文件名: ${it.name} 包含table注解, 跳过")
|
||||
} else {
|
||||
|
||||
|
||||
def lines = it.readLines()
|
||||
|
||||
|
||||
def lineNo = lines.findIndexOf { it.contains("extends OrgCommonEntity") }
|
||||
|
||||
lines.add(lineNo , """
|
||||
@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_${camelToSnake(it.name.substring(0, it.name.indexOf(".java")))}_org_id", columnList = "organization_id"))
|
||||
""")
|
||||
it.text = lines.join("\n")
|
||||
|
||||
println("文件名: ${it.name} 添加索引成功")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import com.squareup.javapoet.*;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
@@ -126,6 +127,20 @@ public class Codegen {
|
||||
.addAnnotation(Entity.class)
|
||||
.build();
|
||||
|
||||
if (orgMode){
|
||||
entity =
|
||||
entity.toBuilder()
|
||||
.addAnnotation(
|
||||
AnnotationSpec.builder(Table.class)
|
||||
.addMember(
|
||||
"indexes",
|
||||
"@jakarta.persistence.Index(name =\"idx_"
|
||||
+ lModuleName
|
||||
+ "_org_id\", columnList = \"organization_id\")")
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
if (archive) {
|
||||
entity =
|
||||
entity.toBuilder()
|
||||
|
||||
Reference in New Issue
Block a user