验证中...
语言: Groovy
分类: 常用工具包
最后更新于 2018-03-24 17:40
gistfile1.txt
原始数据 复制代码
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.ylwx.mall.entity"
typeMapping = [
(~/(?i)int/) : "Int?",
(~/(?i)float|double|decimal|real/): "Double?",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp?",
(~/(?i)date/) : "java.sql.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String?"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
new File(dir, className + ".kt").withPrintWriter { out -> generate(out, className, fields) }
}
def generate(out, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import javax.persistence.Entity"
out.println "import javax.persistence.Id"
out.println "import javax.persistence.GeneratedValue"
out.println "import javax.persistence.GenerationType"
out.println ""
out.println "@Entity"
out.println "class $className {"
out.println ""
out.println " @Id"
out.println " @GeneratedValue(strategy = GenerationType.IDENTITY)"
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " val ${it.name}: ${it.type} = null"
}
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,
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]
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助