IDEA自动生成实体类
2020-07-03 18:53:59 749
idea里先添加数据库
选择表,右键 ,先配置下脚本
编辑Generate POJOs.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.sample;"
typeMapping = [
(~/(?i)bigint/) : "Long",
(~/(?i)tinyint|smallint|mediumint|int/) : "Integer",
(~/(?i)bool|bit/) : "Boolean",
(~/(?i)float/) : "Float",
(~/(?i)double/) : "Double",
(~/(?i)decimal|real/) : "BigDecimal",
(~/(?i)datetime|timestamp|date|time/) : "Date",
(~/(?i)blob|binary|bfile|clob|raw|image/) : "InputStream",
(~/(?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)
def file = new File(dir, className + ".java");
file.withPrintWriter("utf-8") { out -> generate(out, table, className, fields) }
}
def generate(out, table, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import lombok.Data;"
out.println "import javax.persistence.*;"
out.println ""
out.println "/**"
out.println " * $table.comment"
out.println " * generated by Generate POJOs.groovy"
out.println " *"
out.println " * @author Test"
out.println " * @date " + getTime()
out.println " */"
out.println "@Data"
out.println "@Entity"
out.println "@Table(name = \"$table.name\")"
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 " @Column(name= \"${it.field}\")"
out.println " private ${it.type} ${it.name};"
}
out.println ""
//getter setter 方法
// 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 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 : "",
field : col.getName(),
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 getTime() {
long timeStamp = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
return sd;
}
编辑完后执行脚本,点击 上图 红框 上一栏 “Generate POJOs.groovy”
以上配置的效果
package com.sample;
public class Customer {
/*
* null
*/
@Column(name = "Id")
private Integer id;
/*
* 数据库one字段说明
*/
@Column(name = "one")
private String one;
/*
* 数据库two字段说明
*/
@Column(name = "two")
private String two;
/*
* 数据库three字段说明
*/
@Column(name = "three")
private String three;
}