IDEA自动生成实体类,groovy脚本配置

这是我自己的版本,在系统原有基础上
1.增加了swagger关于model的注解
2.增加正文的字段注释
3.我的数据库字段中,字典字段会加“dict_”标识头,逻辑外键字段会加“fk_”标识头,转实体类属性时统一去掉

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

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

packageName = "com.ttit.tzzd.entity;"
typeMapping = [
        (~/(?i)int/)                      : "long",
        (~/(?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)
    new File(dir, className + ".java").withPrintWriter("UTF-8") { out -> generate(out, className, classConment, fields) }
}

def generate(out, className, classConment, fields) {
    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 xiaoxie"
    out.println " * Date: 2019/5/2110:22"
    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(name = \"" + 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]
}

你可能感兴趣的:(JAVA,IDEA,SQL,groovy)