idea groovyスクリプトを使用してデータベースエンティティを自動的に生成
26511 ワード
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.Date
/*
* Available context bindings:
* SELECTION Iterable
* PROJECT project
* FILES files helper
*/
// , ;
packageName = "com.demo.api.tesla.entity;"
// , , tinyint ,
typeMapping = [
(~/(?i)bigint/) : "Long",
(~/(?i)int/) : "Integer",
(~/(?i)tinyint/) : "Boolean",
(~/(?i)float|double|decimal|real/): "BigDecimal",
(~/(?i)time|datetime|timestamp/) : "Date",
(~/(?i)date/) : "LocalDate",
(~/(?i)/) : "String"
]
//
importMap = [
"BigDecimal" : "java.math.BigDecimal",
"LocalDate" : "java.time.LocalDate",
"LocalDateTime": "java.time.LocalDateTime",
"Date": "java.util.Date",
]
// , , HashSet
importList = []
// ,
userTimeList = [
"example"
]
//
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true) + "DO"
def writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "utf-8"))
def fields = calcFields(table)
writer.withPrintWriter { out -> generate(out, className, fields, table) }
}
// ,
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println ""
//
out.println "import lombok.Data;"
out.println "import com.demo.basic.annotation.Column;"
out.println "import com.demo.basic.annotation.Table;"
//
importList.unique().each() { pkg ->
out.println "import " + pkg + ";"
}
out.println ""
//
out.println "/**"
// ,
if (isNotEmpty(table.getComment())) {
out.println " * " + table.getComment()
}
out.println " *"
out.println " * @author wulongtao"
out.println " * @since " + LocalDate.now()
out.println " */"
//
out.println "@Data"
out.println "@Table(\"${table.getName()}\")"
out.println "public class $className {"
out.println ""
boolean isId = true
// ,
fields.each() {
//
if (isNotEmpty(it.comment)) {
out.println "\t/**"
out.println "\t * ${it.comment}"
out.println "\t */"
}
// , ,
if (isId) {
out.println "\t@Column(isPK = true, name = \"${it.realName}\")"
isId = false
} else {
out.println "\t@Column(name = \"${it.realName}\")"
}
out.println "\tprivate ${it.type} ${it.name};"
out.println ""
}
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
// BaseTimeUserIdPO ,
if (!userTimeList.contains(col.getName())) {
// , list
if (importMap.containsKey(typeStr)) {
importList.add(importMap.get(typeStr))
}
fields += [[
realName: col.getName(),
name : javaName(col.getName(), false),
type : typeStr,
dbType : col.getDataType(),
comment : col.getComment()
]]
} else {
fields += []
}
}
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}