Last active
July 31, 2018 08:21
-
-
Save abop/9200fabcd96ec8d6854fb458a49be653 to your computer and use it in GitHub Desktop.
Intellij IDEA 中从数据库结构生成 POJO 的脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.database.model.DasTable | |
import com.intellij.database.model.ObjectKind | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
//packageName = "com.sample;" | |
fileNameSuffix = "DO" | |
typeMapping = [ | |
(~/(?i)int/) : "Long", | |
(~/(?i)float|double|real/): "Double", | |
(~/(?i)decimal/): "Decimal", | |
(~/(?i)datetime|timestamp/) : "LocalDateTime", | |
(~/(?i)date/) : "LocalDate", | |
(~/(?i)time/) : "LocalTime", | |
(~/(?i)/) : "String" | |
] | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) } | |
} | |
def generate(table, dir) { | |
def packageName = dirToPackage(dir) | |
def className = javaName(table.getName(), true) + fileNameSuffix | |
def fields = calcFields(table) | |
new File(dir, className + ".java").withPrintWriter { out -> generate(out, packageName, className, fields) } | |
} | |
static def generate(out, packageName, className, fields) { | |
out.println "package $packageName;" | |
out.println "" | |
out.println "import java.time.*;" | |
out.println "" | |
out.println "" | |
out.println "public class $className {" | |
out.println "" | |
fields.each() { | |
if (it.annos != "") out.println " ${it.annos}" | |
if (it.comment != null) { | |
out.println " /**" | |
out.println " * ${it.comment}" | |
out.println " */" | |
} | |
out.println " private ${it.type} ${it.name};" | |
} | |
out.println "" | |
fields.each() { | |
out.println "" | |
out.println " public ${it.type} get${it.name.capitalize()}() {" | |
out.println " return ${it.name};" | |
out.println " }" | |
out.println "" | |
out.println " public ${className} set${it.name.capitalize()}(${it.type} ${it.name}) {" | |
out.println " this.${it.name} = ${it.name};" | |
out.println " return this;" | |
out.println " }" | |
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 | |
fields += [[ | |
name : javaName(col.getName(), false), | |
type : typeStr, | |
comment: col.getComment(), | |
annos: ""]] | |
} | |
} | |
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] | |
} | |
static def dirToPackage(dir) { | |
def path = dir.getPath() | |
path.substring(path.indexOf("/com/") + 1).replaceAll(/[\/]/, ".") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
?