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} 添加索引成功") } } } }