POJO-jpa-lombok-Entity.groovy

import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil

import java.text.SimpleDateFormat

packageName = "com.example.demo.pojo.entity;"
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 + "Entity.java").withPrintWriter("utf-8") { out -> generate(out,table, className, fields) }
}

def generate(out,table, className, fields) {
  def tableName = table.getName();
  out.println "package $packageName"
  out.println ""
  out.println "import lombok.Data;"
  out.println "import java.io.Serializable;"
  out.println "import javax.persistence.*;"
  out.println "import com.fasterxml.jackson.annotation.JsonFormat;"
  out.println "import org.hibernate.annotations.CreationTimestamp;"
  out.println "import org.hibernate.annotations.UpdateTimestamp;"
  out.println "import org.hibernate.annotations.Type;"
  out.println "import lombok.extern.slf4j.Slf4j;"
  out.println ""
  out.println "/**"
  out.println " * @ClassName ${className}Entity"
  out.println " * @Description Automatic script generation"
  out.println " * @Author"
  out.println " * @Date ${new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())}"
  out.println " * @Version 1.0"
  out.println " */"
  out.println "@Data"
  out.println "@Slf4j"
  out.println "@Entity"
  out.println "@Table(name = \"$tableName\")"
  out.println "public class ${className}Entity  implements Serializable{"
  out.println ""
  fields.each() {
    if (it.annos != "") {
      out.println "  ${it.annos}"
    }

    if (isNotEmpty(it.commoent)){
      out.println "\t/**"
      out.println "\t * ${it.commoent}"
      out.println "\t **/"
    }
    if ("id".equalsIgnoreCase(it.srcName)){
      out.println "\t@Id"
      out.println "\t@GeneratedValue(strategy = GenerationType.IDENTITY)"
    }

    out.println "\t@Column(name = \"${it.srcName}\")"

    if ("create_time".equalsIgnoreCase(it.srcName)){
      out.println "\t@CreationTimestamp"
      out.println "\t@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")"
      out.println "\tprivate java.sql.Timestamp createTime;\r"
      return
    }

    if ("update_time".equalsIgnoreCase(it.srcName)){
      out.println "\t@UpdateTimestamp"
      out.println "\t@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")"
      out.println "\tprivate java.sql.Timestamp updateTime;\r"
      return
    }

    if ("state".equalsIgnoreCase(it.srcName)){
      out.println "\t@Type(type = \"org.hibernate.type.NumericBooleanType\")"
      out.println "\tprivate Boolean state;\r"
      return
    }

    out.println "\tprivate ${it.type} ${it.name};\r"
  }
  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),
                       srcName : col.getName(),
                       type : typeStr,
                       commoent  : col.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}[_]]/, "_")
  capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

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

你可能感兴趣的:(脚本)