数据库
首页 > 数据库> > idea数据库自动生产实体类

idea数据库自动生产实体类

作者:互联网

 

 

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

/*
 * Available context bindings:
 *   SELECTION   Iterable

packageName = "com.hongkun.bean.hkt;"
typeMapping = [
        (~/(?i)int/)                      : "Integer",
        (~/(?i)float|double|decimal|real/): "double",
        (~/(?i)datetime|timestamp|date/)  : "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 classConment = table.getComment()
  def fields = calcFields(table)
  def dateStr = new Date().toLocaleString();
  new File(dir, className + ".java").withPrintWriter("UTF-8") { out -> generate(out, className, classConment, fields,dateStr) }
}

def generate(out, className, classConment, fields,dateStr) {
  out.println "package $packageName"
  out.println ""
  out.println "import io.swagger.annotations.ApiModel;"
  out.println "import io.swagger.annotations.ApiModelProperty;"
  out.println "import lombok.Data;"
  out.println ""
  out.println "import java.io.Serializable;"
  out.println "import java.util.Date;"
  out.println ""
  out.println "/**"
  out.println " * Description: ${classConment}"
  out.println " *"
  out.println " * @author fengxiaoyang"
  out.println " * @date ${dateStr}"
  out.println " */"
  out.println "@Data"
  out.println "@ApiModel(\"${classConment}\")"
  out.println "public class $className implements Serializable {"
  out.println ""
//    out.println "\tprivate static final long serialVersionUID =  " + Math.abs(new Random().nextLong()) + "L;"
//    out.println ""
  fields.each() {
    //  if (isNotEmpty(it.comment)) out.println "\t/** ${it.comment} */"
    if (it.annos != "") out.println "  ${it.annos}"
    out.println "\tprivate ${it.type} ${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
    def comment = col.getComment()
    fields += [[
                       name   : fieldName(col.getName(), false),
                       type   : typeStr,
                       comment: comment,
                       annos  : "\t@ApiModelProperty(value = \"" + comment + "\" )"]]
  }
}

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
}
/**
 * 我的字段会给字典字段和外键字段增加标识头,这里转属性时去掉
 * @param str
 * @param capitalize
 * @return
 */
def fieldName(str, capitalize) {
  //去除开头的标识
  if (str.startsWith("dict_")) str = str.substring(5)
  if (str.startsWith("fk_")) str = str.substring(3)
  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]
}


标签:实体类,idea,自动生产,str,println,import,capitalize,def,out
来源: https://blog.51cto.com/u_15193673/2847859