1. 上配置文件
🐴本次使用的IDEA版本为 2021.1.2,如果有需要可以私信联系,或者: ?? 2496290990
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.time.LocalDateTime
typeMapping = [
(~/(?i)int/) : "Integer",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?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 }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
new File(dir, className + ".java").withPrintWriter("utf-8") {
out -> generate(out, className, fields, table.getName(),table.getComment(),getPackageName(dir.toString()))
}
}
def generate(out, className, fields, tableName,tableComment,dir) {
out.println "package $dir;"
out.println ""
out.println ""
out.println "/**"
out.println " * @author eleven "
out.println " * @date " + new Date().toLocaleString()
out.println " * @description $tableComment $tableName"
out.println " */"
out.println " "
out.println "@Data"
out.println "@AllArgsConstructor"
out.println "@NoArgsConstructor"
out.println "@TableName(\"$tableName\")"
out.println "public class $className {"
out.println ""
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " /**"
out.println " * ${it.comment}"
out.println " */"
out.println " private ${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
fields += [[
name : javaName(col.getName(), false),
type : typeStr,
annos: "",
comment : col.getComment()
]]
}
}
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]
}
def getPackageName(str){
def path = str.split("java");
path[1].replaceAll(/[\\]/,".")
.substring(1)
}
2. 替换自己本地的配置文件
点击右侧的 database ,选中其中一个表,右键调出菜单,选中Script Extension 然后选中 Go to Script Directory
将自己的配置文件替换成我的,如果没有的话就创建一个 .groovy 后缀的文件 生成实体的话,在刚才的那个菜单点击 Generate POJOs.groovy 就可以了
|