IDEA JPA 视图生成实体类 groovy 脚本
注:部分代码来源于互联网
生成实体类(部分)
package C:.Users.Mxnter.Desktop;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.Id;
/**
* @Description
* @Author IDEA Auto JPA database tables generate entity Script Mxnter
* @Date 2019-07-10
*/
@Entity
@Table ( name ="MG_VIEW_DEC_RECEIVABLES" )
@Data
public class MgViewDecReceivables implements Serializable {
private static final long serialVersionUID = 8044993785748313550L;
@Id
@Column(name = "UUID" )
private String uuid;
@Column(name = "COST_NAME" )
private String costName;
@Column(name = "APPLY_ID" )
private String applyId;
Generate VIEWs.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.text.SimpleDateFormat
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.sample;"
typeMapping = [
(~/(?i)int/) : "long",
(~/(?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.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
packageName = getPackageName(dir)
def fields = calcFields(table)
new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields, table) }
}
// 获取包所在文件夹路径
def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import lombok.Data;"
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println "import java.io.Serializable;"
out.println "import javax.persistence.Id;"
out.println ""
out.println "/**\n" +
" * @Description \n" +
" * @Author IDEA Auto JPA database view generate entity Script Mxnter\n" +
" * @Date "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " \n" +
" */"
out.println ""
out.println ""
out.println "@Entity"
out.println "@Table ( name =\""+table.getName() +"\" )"
out.println "@Data"
out.println "public class $className implements Serializable {"
out.println ""
out.println ""
out.println genSerialID()
fields.each() {
out.println "\t"
if (isNotEmpty(it.commoent)) {
out.println "\t/**"
out.println "\t * ${it.commoent.toString()}"
out.println "\t */"
}
// if (it.annos != "") out.println " ${it.annos.replace("[@Id]", "")}"
out.println "${it.annos}\t"
out.println "\tprivate ${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 += [[
colName : col.getName(),
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment(),
annos: ("UUID".equals(col.getName())?"\t@Id\n":"")+"\t@Column(name = \""+col.getName()+"\" )"]]
}
}
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 isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
static String genSerialID()
{
return "\tprivate static final long serialVersionUID = "+Math.abs(new Random().nextLong())+"L;"
}
网友评论