IDEA利用Groovy自动生成实体类

1.使用IDEA连接数据库

2.选择要生成数据表实体类目标对象,点击图示选项

在这里插入图片描述

3.选择生成实体类目标包下

如果自动生成实体类对象内容不满足需求现状,在Scratches and Consoles下Extensions/Database Tools and SQL/schema目录下找到Generate POJOs.groovy脚本文件,并修改其内容

在这里插入图片描述

Generate POJOs.groovy脚本内容简要介绍:

packageName = "com.xx.xx.io.dto;"
typeMapping = [
  (~/(?i)int/)                      : "Long",
  (~/(?i)float|double|decimal|real/): "double",
  (~/(?i)datetime|timestamp/)       : "LocalDateTime",
  (~/(?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) }
}

def generate(out, className, fields) {
  out.println "package $packageName"
  out.println ""
  out.println "import java.time.LocalDateTime;"
  out.println "import lombok.Data;"
  out.println "import lombok.Data;"
  out.println "import io.swagger.annotations.ApiModel;"
  out.println "import io.swagger.annotations.ApiModelProperty;"
    /**
     * @program: xxxx
     * @description: xxxx
     * @author: xxxx
     * @create: 2022-07-25 10:11
     **/
  out.println ""
  out.println ""
  out.println "/**"
  out.println "* @program: nz_backstage"
  out.println "* @description: ${fields[0].tableCommoent}"
  out.println "* @author: xierongbin"
  out.println "* @create: "+new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())
  out.println "**/"
  out.println "@Data"
  out.println "@ApiModel(value = \"${className}数据传输对象\",description = \"${fields[0].tableCommoent}数据传输对象\")"
  out.println "public class $className {"
  out.println ""
  fields.each() {
    if (it.annos != "") out.println "  ${it.annos}"
    out.println("/**")
    out.println(" *")
    out.println(" * ${it.commoent}")
    out.println(" *")
    out.println(" */")
    out.println "  @ApiModelProperty(value = \"${it.commoent}\")"
    out.println "  private ${it.type} ${it.name};"
    out.println ""
  }
  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,
                 commoent: col.getComment(),
                 tableCommoent:table.getComment(),
                 annos: ""]]
  }
}

def javaName(str, capitalize) {
  def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
    .collect { Case.LOWER.apply(it).capitalize() }
    .join("")
    .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
    .replace("xxx", "DTO")
  capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
packageName :指定实体类文件包名
typeMapping :指定字段数据类型映射实体类数据类型
col.getName():获取当前字段名
col.getComment():获取当前字段注释
table.getComment():获取数据表注释
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值