微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

java自定义entity模板

java aide_941 2℃ 0评论
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.LocalDate

/*
 * Available context bindings:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = "com.songyi.domain.entity;"
typeMapping = [
  (~/(?i)bigint/)                   : "Long",
  (~/(?i)tinyint/)                  : "Boolean",
  (~/(?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 && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}

// 导入路径列表,下面引用的时候会去重,也可以直接声明成一个 HashSet
//importList = [
//]

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 ""
  out.println "import lombok.Data;"
  out.println "import java.io.Serializable;"
  out.println "import javax.persistence.*;"
  out.println "import com.alibaba.fastjson.annotation.JSONField;"
  out.println "import org.hibernate.annotations.DynamicUpdate;"
  out.println "import org.hibernate.annotations.DynamicInsert;"

//  // 去重后导入列表
//  importList.unique().each() { pkg ->
//    out.println "import " + pkg + ";"
//  }

  out.println ""
  out.println ""
  out.println "/**"
  if (isNotEmpty(table.getComment())) {
    out.println " * " + table.getComment()
  }
  out.println " *"
  out.println " * @author weisongyi<1219204@qq.com>"
  out.println " * @date " + LocalDate.now()
  out.println " */"
  out.println "@Entity"
  out.println "@Data"
  out.println "@DynamicInsert"
  out.println "@DynamicUpdate"
  out.println "@Table(name = \"${table.getName()}\")"
  out.println "public class $className implements Serializable{"
  out.println ""
  fields.each() {
    // 输出注释
    if (isNotEmpty(it.comment)) {
      out.println "\t/**"
      out.println "\t * ${it.comment}"
      out.println "\t */"
    }
    if (it.annos != "") out.println "  ${it.annos}"
    if (it.name == "id") {
      out.println "\t@Id"
      out.println "\t@GeneratedValue(generator = \"JDBC\")"
    }else {

      if(it.type.contains("Timestamp")){
        out.println "\t@JSONField(format = \"yyyy-MM-dd HH:mm:ss\")"
      }
      out.println "\t@Column(name = \"" + it.colname + "\")"
    }

    out.println "\tprivate ${it.type} ${it.name};"
    out.println ""
  }
  out.println ""

//  //有了lombok ,去掉了get、set方法
//  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
//    if (importMap.containsKey(typeStr)) {
//      importList.add(importMap.get(typeStr))
//    }

    fields += [[
                 name : javaName(col.getName(), false),
                 type : typeStr,
                 colname: col.getName(), //重要
                 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
}

转载请注明:SuperIT » java自定义entity模板

喜欢 (0)or分享 (0)

您必须 登录 才能发表评论!