首先绑定一个数据库
填写名称,连接的数据库、用户名、密码等信息。
编写脚本
选择最后一项,会自动生成一个脚本文件,打开并修改内容:
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "net.beautifulbox.mall.pojo;"
typeMapping = [
(~/(?i)int/) : "long",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp/) : "Date",
(~/(?i)date/) : "Date",
(~/(?i)time/) : "Date",
(~/(?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 tableName = table.getName()
def fields = calcFields(table)
def targetClassName = className.substring(2, className.size())
new File(dir, targetClassName + ".java").withPrintWriter { out -> generate(out, tableName, targetClassName, fields) }
}
def generate(out, tableName, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import com.fasterxml.jackson.annotation.JsonIgnore;"
out.println "import com.fasterxml.jackson.annotation.JsonProperty;"
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Id;"
out.println "import javax.persistence.Table;"
out.println "import java.util.Date;";
out.println ""
out.println "@Entity";
out.println "@Table( name =\"${tableName}\" )";
out.println "public class $className {"
out.println ""
fields.each() {
if ('id'.equals(Case.LOWER.apply(it.name))) {
it.annos = '\t@Id'
} else if ('order'.equals(Case.LOWER.apply(it.name))) {
it.annos = "\t@Column(name = \"`" + it.name + "`\" )"
} else {
it.annos = "\t@Column(name = \"" + it.name + "\" )"
}
if (it.annos != "") out.println " ${it.annos}"
out.println " private ${it.type} ${javaName(it.name, false)};"
}
out.println ""
fields.each() {
out.println ""
if ('password'.equals(Case.LOWER.apply(it.name)) || 'create_time'.equals(Case.LOWER.apply(it.name))
|| 'last_login_time'.equals(Case.LOWER.apply(it.name)) || 'reg_ip'.equals(Case.LOWER.apply(it.name))
|| 'login_ip'.equals(Case.LOWER.apply(it.name)) || 'update_time'.equals(Case.LOWER.apply(it.name))) {
out.println "@JsonIgnore"
}
out.println " public ${it.type} get${javaName(it.name, true)}() {"
out.println " return ${javaName(it.name, false)};"
out.println " }"
out.println ""
if ('password'.equals(Case.LOWER.apply(it.name)) || 'create_time'.equals(Case.LOWER.apply(it.name))
|| 'last_login_time'.equals(Case.LOWER.apply(it.name)) || 'reg_ip'.equals(Case.LOWER.apply(it.name))
|| 'login_ip'.equals(Case.LOWER.apply(it.name)) || 'update_time'.equals(Case.LOWER.apply(it.name))) {
out.println "@JsonProperty"
}
out.println " public void set${javaName(it.name, true)}(${it.type} ${javaName(it.name, false)}) {"
out.println " this.${javaName(it.name, false)} = ${javaName(it.name, false)};"
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),
name : col.getName(),
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]
}
然后选择你需要生成实体类的表,然后右键,
选择第二项,选择位置存放你的实体类。这样就会生成你的实体类了。比起一个个手写快非常多。