生成实体类
程序员文章站
2022-06-14 23:03:02
...
IDEA 工具生成Java实体类
转载 https://blog.csdn.net/likun557/article/details/111771739
https://blog.csdn.net/yang5726685/article/details/100655455
文件名:lombok.groovy
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.xxxx.dao;" // 此处指定包路径,也就对应实体类中的package com.topex.admin.entity;
typeMapping = [ // 此处指定对应的类型映射,如下:数据库中bigint对应生成java的Long,int|tinyint生成Integer...
(~/(?i)bigint/) : "Long",
(~/(?i)int|tinyint/) : "Integer",
(~/(?i)float|double|decimal|real/): "BigDecimal",
(~/(?i)date/) : "LocalDateTime",
(~/(?i)datetime|timestamp/) : "LocalDateTime",
(~/(?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 PrintWriter(new OutputStreamWriter(new FileOutputStream( new File(dir, className + ".java")), "utf-8")).withPrintWriter { out -> generate(out, className, fields) }
}
def generate(out, className, fields) { // 从这里开始,拼实体类的具体逻辑代码
out.println "package $packageName"
out.println ""
out.println ""
out.println "import com.baomidou.mybatisplus.annotation.IdType;"
out.println "import com.baomidou.mybatisplus.annotation.TableId;"
out.println "import lombok.Data;" // 因为我使用了lombok插件,使用到了Data注解,所以在引包时加了这一行
out.println "import lombok.EqualsAndHashCode;"
out.println "import lombok.experimental.Accessors;"
//out.println "import io.swagger.annotations.ApiModelProperty;" // 同上,使用了swagger文档,所以引入到需要的注解
//out.println "import javax.persistence.Id;" // tk.mybatis插件需用时需要@id注解,所以引入,不需要就去掉
out.println "import java.io.Serializable;"
out.println "import java.time.LocalDateTime;"
out.println "import java.math.BigDecimal;"
out.println ""
out.println "@Data"
out.println "@EqualsAndHashCode(callSuper = false)"
out.println "@Accessors(chain = true)"
out.println "public class $className implements Serializable {"
out.println ""
int i = 0
fields.each() { // 遍历字段,按下面的规则生成
// 输出注释,这里唯一的是id特殊判断了一下,如果判断it.name == id, 则多添加一行@Id
if (it.name == "id") {
if (!isNotEmpty(it.commoent)) {
out.println "\t/**"
out.println "\t * 主键id"
out.println "\t */"
//out.println "\[email protected](value = \"主键id\", position = ${i})"
}
//out.println "\[email protected]"
}
if (isNotEmpty(it.commoent)) {
out.println "\t/**"
out.println "\t * ${it.commoent}"
out.println "\t */"
//out.println "\[email protected](value = \"${it.commoent}\", position = ${i})"
}
if (it.name == "id"){
out.println "\[email protected](value = \"id\", type = IdType.AUTO)"
}
if (it.annos != "") out.println " ${it.annos}"
out.println "\tprivate ${it.type} ${it.name};"
out.println ""
i++
}
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,
commoent: col.getComment(),
annos: ""]]
}
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
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]
}
上一篇: PHP操作Imagick库打印数字矩阵