大道始,损有余而补不足
由于业务增加,需要新增表,项目框架又是Hibernate,如果手写表感觉到在做无用功,而且很容易出错,这里就巴拉巴拉了前辈们的代码和博客,参考着写了个逆向生成实体类方法,生成Mybaties实体工具请参考博主分类Mybaties系列文章
逆向生成实体类的前提是你已经通过idea连接了oracle数据库,博主已经写好了一篇,仅供参考,传送门:idea连接远程Oracle数据库生成实体或者使用操作sql
详情请仔细查看代码中的注释,我已经整理了一遍,仅供参考,如有异议请联系博主,并及时更改
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.time.LocalDateTime
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
* update by yitianRen 20200324
*/
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)
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 "/**\n" +
" * \n" +
" * <p>@Date: " + LocalDateTime.now().toString() + ".</p>\n" +
" *\n" +
" * @author yitianRen\n" +
" */"
out.println ""
out.println "import lombok.Getter;"
out.println "import lombok.Setter;"
out.println "import javax.persistence.*;"
out.println ""
out.println "@Entity"
out.println "@Getter"//输出注解Get
out.println "@Setter"//输出注解Set
out.println "@Table ( name =\"" + table.getName() + "\" )"
out.println "public class $className {"
out.println ""
fields.each() {
out.println ""
// 输出注释 这里和下面的 comm是一一对应的
if (isNotEmpty(it.commoent)) {//定义非空校验,it.commoent != ""手写无效
out.println "\t/**"
out.println "\t * ${it.commoent}"
out.println "\t */"
}
if (it.annos != "") out.println " ${it.annos}"
out.println " private ${it.type} ${it.name};"
}
out.println ""
//输出get set方法
/*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
def comm = [
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment(),
annos: "@Column(name = \"" + col.getName() + "\" )"
]
//对于表中主键自定义注解
if ("pk".equals(Case.LOWER.apply(col.getName()))){
comm.annos = "\t@Id\n"
//自增主键需要
comm.annos += "@Column(name = \"" + col.getName() + "\")"
}
fields += [comm]//字段对照
}
}
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
}
参考:
idea 通过 Generate POJOs.groovy 生成实体类