使用idea的database连接数据库后,在表上右键可以自动生成entity文件,非常方便
默认生成的entity只要属性与getter setter,可以通过修改Generate POJOs.groovy
文件,来自动生成需要的@Column(name = "ID")
这样的注解
下面是截取的已经【修改过】得Generate POJOs.groovy
文件, 类似python语法,其中的修改是,先在calcFields
函数加入filed: col.getName(),
然后在generate
函数加入out.println " @Column(name = \"${it.filed}\")"
def generate(out, className, fields) {
out.println "package $packageName"
out.println ""
out.println ""
out.println "public class $className {"
out.println ""
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " @Column(name = \"${it.filed}\")"
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 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,
filed: col.getName(),
annos: ""]]
}
}
最后会直接生成带有@Column(name = "ID")
注解的entity,免去手写,也可以自己添加需要的
下面是我修改后的【完整】文件内容
修改的部分
- package名称
- 导入的类名
- 使用lombok插件与jpa注解,使用lombok【注释】了生成getter与setter,jpa的@Table,@Column注解
- 数据库对应类型的java类,数据库datetime转为util.date
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 = "com.bank3d.bank3d_admin.entity;"
typeMapping = [
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)datetime|timestamp/) : "Date",
(~/(?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) + "E"
def fields = calcFields(table)
new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields, table) }
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println ""
out.println "import lombok.Data;"
out.println "import lombok.experimental.Accessors;"
out.println "import org.hibernate.annotations.DynamicInsert;"
out.println "import org.hibernate.annotations.DynamicUpdate;"
out.println ""
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println "import java.util.Date;"
out.println ""
out.println "@Entity"
out.println "@Table(name = \"${table.getName()}\")"
out.println "@Data"
out.println "@DynamicUpdate"
out.println "@DynamicInsert"
out.println "@Accessors(chain = true)"
out.println "public class $className {"
out.println ""
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " @Column(name = \"${it.filed}\")"
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 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,
filed: col.getName(),
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]
}