wzc.jpa.entity.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.lang.String
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.sephiroth;"
fileBefor = ".java"
typeMapping = [
(~/(?i)int|integer/) : "Integer",
(~/(?i)number\(\*\)/) : "Integer",
(~/(?i)number\([0-9]*,[0-9]\)|float|double|decimal|real/): "Double",
(~/(?i)timestamp/) : "java.sql.Timestamp",
(~/(?i)datetime|date/) : "java.util.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable && (it.getKind() == ObjectKind.TABLE || it.getKind() == ObjectKind.VIEW) }.each { generate(it, dir) }
}
def generate(out, className, fields, table) {
def key = fields[0]
def length = fields.size()-1
out.println """
package com.docusign.jpa.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import java.io.Serializable;
@Data
@Entity
public class ${className} implements Serializable {
"""
out.println ""
def gzcol = ""
for (i in 0..length) {
def it = fields[i]
if (continerBaseDO(it)) {
continue
}
if(i>0) {
gzcol += ","
}
gzcol+= "${it.type} ${it.name}"
if (it.annos != "") out.println " ${it.annos}"
out.println """ /**
* ${it.commoent}
*/"""
out.println " @Column(name = \"${it.colname}\")"
out.println " private ${it.type} ${it.name};"
}
out.println "}"
}
// region 共通方法
def generate(table, dir) {
// def className = javaName(table.getName(), true)
def className = getClassNmae(table)
def fields = calcFields(table)
// 根据目录生成 package
packageName = getPackageName(dir)
new File(dir, className + fileBefor).withPrintWriter { out -> generate(out, className, fields, table) }
}
def continerBaseDO(it) {
return (it.name == "enableFlag"|| it.name == "createdDate"||it.name == "createdBy" ||it.name == "updatedDate" ||it.name == "updatedBy" )
}
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 += [[
colname: col.getName(),
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment(),
annos: ""]]
}
}
/**
* 获取名字
* @param table
* @return
*/
def getClassNmae(table) {
def tableName = table.getName()
// return javaName(tableName.replaceAll("^CMS_V_?", "").replaceAll("^CMS[_A-Z]{4}?", ""),true)
if(table.getKind() == ObjectKind.TABLE){
return javaName(tableName,true)
} else {
return javaName(tableName,true)
}
}
/**
* 获取java名字
* @param str
* @param capitalize
* @return
*/
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]
}
/**
* 获取包名称
* @param dir 实体类所在目录
* @return
*/
def getPackageName(dir) {
return dir.toString().replaceAll("/", ".").replaceAll("\\\\", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
// endregion
网友评论