IDEA-快速生成实体类

时间:2024-03-23 21:19:33

自定义配置生成带注释注解的实体类
一、 连接数据库
从右侧点击Database打开数据库工具窗口,
或者从菜单View→Tool Windows→Database打开数据库工具窗口。
点击Database工具窗口左上角添加按钮“+”IDEA-快速生成实体类,选择数据库类型,这里以MySQL为例
填写数据库连接的主机名、数据库名、用户名和密码。
点击Test Connection按钮进行测试数据库连接是否正常。

二、 编辑Generate POJOs.groovy
在数据库工具窗口右键=》Scripted Extensions=》Go to Scripts Directory
打开Generate POJOs.groovy所在位置,也可以新建自定义配置。
IDEA-快速生成实体类
IDEA-快速生成实体类
IDEA-快速生成实体类
我的Generate POJOs.groovy配置
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.util.Date
import java.text.SimpleDateFormat

/*

  • Available context bindings:
  • SELECTION Iterable
  • PROJECT project
  • FILES files helper
    */

packageName = “”
typeMapping = [
(~/(?i)tinyint|bool|bit/) : “Boolean”,
(~/(?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)
def fields = calcFields(table)
packageName = getPackageName(dir)
// 设置编码为utf-8
new File(dir, className + “.java”).withPrintWriter(“utf-8”) { 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 “”
out.println “import com.ogawa.health.framework.core.data.BaseEntity;”
out.println “import javax.persistence.Column;”
out.println “”
out.println “import javax.persistence.Table;”
out.println “import javax.persistence.Id;”
out.println “import java.util.Date;”
out.println “”
out.println “/**”
out.println " * @author yaozhenyang"
out.println " * @fileName KaTeX parse error: Can't use function '\"' in math mode at position 267: …"@Table(name = \̲"̲{table.getName()}",schema = “db_temi”)"
out.println “@Data”
out.println “public class KaTeX parse error: Undefined control sequence: \t at position 192: … out.println "\̲t̲/**{it.commoent}*/”
}
if (it.annos != “”) out.println " KaTeX parse error: Can't use function '\"' in math mode at position 101: …@Column(name = \̲"̲{it.filed}")"
out.println " private ${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 += [[
name : javaName(col.getName(), false),
type : typeStr,
filed: col.getName(),
annos: “”,
commoent: col.getComment()
]]
}
}

def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}

def getPackageName(dir) {
return dir.toString().replaceAll("\\", “.”).replaceAll("^.*src(\.main\.java\.)?", “”) + “;”
}

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]
}

三、 生成实体类
在数据库工具窗口,选择要生成实体类的表,右键=》Scripted Extensions=》选择之前的配置文件,选择实体类的目标目录。完成。
展示:
IDEA-快速生成实体类