自定义Generate POJOs.groovy

Intellij IDEA 通过数据库表逆向生成带注释的实体类文件(Generate POJOs.groovy)

生成JPA实体文件,生成的代码如下:

import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@Table ( name ="image" )
@Data
@ApiModel
public class Image  implements Serializable{

private static final long serialVersionUID = 1L;

  @Id
  @Column(name = "id" )
  @ApiModelProperty( value="id")
  private String id;

  @Column(name = "entity_id" )
  @ApiModelProperty( value="实体id-可关联作业单")
  private String entity_id;

  @Column(name = "image_name" )
  @ApiModelProperty( value="图片名称")
  private String image_name;

  @Column(name = "image_path" )
  @ApiModelProperty( value="图片路径")
  private String image_path;

  @Column(name = "create_time" )
  @ApiModelProperty( value="创建时间")
  private java.util.Date create_time;

  @Column(name = "update_time" )
  @ApiModelProperty( value="修改时间")
  private java.util.Date update_time;

}

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.time.LocalDateTime
import java.time.format.DateTimeFormatter

/*
 * Available context bindings:
 *   SELECTION   Iterable
 *   PROJECT     project
 *   FILES       files helper
 *   update by yitianRen 20200324
 */

packageName = "com.nimble.ydtb.api.entity;"
typeMapping = [
        (~/(?i)int/)                      : "Integer",  //数据库类型和Jave类型映射关系
        (~/(?i)float|double|decimal|real/): "Double",
        (~/(?i)bool|boolean/)             : "Boolean",
        (~/(?i)datetime|timestamp/)       : "java.util.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)//更改实体生成规则
  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" +
          " * 

@Date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ".

\n" + " *\n" + " * @author linqh03\n" + " */" out.println "" out.println "import lombok.Data;" out.println "import javax.persistence.*;" out.println "import java.io.Serializable;" out.println "import io.swagger.annotations.ApiModel;" out.println "import io.swagger.annotations.ApiModelProperty;" out.println "" out.println "@Table ( name =\"" + table.getName() + "\" )" out.println "@Data" out.println "@ApiModel" out.println "public class $className implements Serializable{" out.println "" out.println "private static final long serialVersionUID = 1L;" fields.each() { out.println "" // 输出注释 这里和下面的 comm是一一对应的 /* if (isNotEmpty(it.commoent)) {//定义非空校验,it.commoent != ""手写无效 out.println "// ${it.commoent}" }*/ if (it.name == "id") out.println " @Id" if (it.annos != "") out.println " ${it.annos}" out.println " @ApiModelProperty( value=\"${it.commoent}\")" 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 def comm = [ name : col.getName(), 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 }

你可能感兴趣的:(java开发)