码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-02 11:54:08      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:column   tsp   code   real   alt   direct   sele   image   ack   

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 "/**\n" +
" * @Author CYH \n" +
"**/"
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 生成实体类

标签:column   tsp   code   real   alt   direct   sele   image   ack   

原文地址:https://www.cnblogs.com/cyh1282656849/p/11969499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!