idea 通过 Generate POJOs.groovy 生成实体类

Posted cyh1282656849

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea 通过 Generate POJOs.groovy 生成实体类相关的知识,希望对你有一定的参考价值。

Generate POJOs.groovy脚本:


import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil


packageName = ""
typeMapping = [
(~/(?i)tinyint|smallint|mediumint/) : "Integer",
(~/(?i)bool|bit/) : "Boolean",
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "BigDecimal",
(~/(?i)datetime|timestamp/) : "Date",
(~/(?i)date/) : "Date",
(~/(?i)time/) : "Date",
(~/(?i)/) : "String",
(~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream"
]

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)
packageName = getPackageName(dir)
new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className,table, fields) }
}

def generate(out, className,table, fields) {

Set<String> types = new HashSet<String>()
fields.each() {
types.add(it.type)
}

out.println "package $packageName"
out.println ""
out.println ""
out.println "import antu.com.annotations.Column;"
out.println "import antu.com.annotations.PrimaryKey;"
out.println "import antu.com.annotations.TableName;"
if (types.contains("Date")) {
out.println "import java.util.Date;"
}

if (types.contains("InputStream")) {
out.println "import java.io.InputStream;"
}

if (types.contains("BigDecimal")) {
out.println "import java.math.BigDecimal;"
}

out.println ""
out.println "/** " +
" * @Author CYH " +
"**/"
out.println "@TableName("${table.toString().replaceAll("table:","")}")"
out.println "public class $className {"
out.println ""
fields.each() {
out.println ""
if (it.annos != "") out.println " ${it.annos}"
if (it.comment != "") out.println " /*** ${it.comment} */"
out.println " @Column("${it.name.toUpperCase()}")"
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 void set${it.name.capitalize()}(${it.type} ${it.name}) {"
out.println " this.${it.name} = ${it.name};"
out.println " }"
out.println ""
}
out.println "}"
}

def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}

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 += [[
column : col.getName(),
comment: col.getComment(),
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]
}

 

 

在idea右侧Database工具栏中新建数据库连接后选中表如图操作即可生成实体类

 

 技术图片

 

以上是关于idea 通过 Generate POJOs.groovy 生成实体类的主要内容,如果未能解决你的问题,请参考以下文章

Intellij idea Maven-generate完整教程

IDEA工具java开发之 代码生成Generate

idea maven mvn archetype:generate 速度缓慢问题(转)

为啥我在idea中右键的generate,里面的不全,只有空参的,没有set和get方法,也没有带参的构造方法?

新建maven项目时,mvn archetype:generate 速度缓慢

IntelliJ IDEA使用技巧一览表