• idea .groovy脚本生成实体类脚本模板


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

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

    packageName = "com.sample;"
    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 + ".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 com.baomidou.mybatisplus.annotation.IdType;"
    out.println "import com.baomidou.mybatisplus.annotation.TableId;"
    out.println "import com.baomidou.mybatisplus.annotation.TableName;"
    out.println "import io.swagger.annotations.ApiModel;"
    out.println "import io.swagger.annotations.ApiModelProperty;"
    out.println "import lombok.Data;"
    out.println "import lombok.EqualsAndHashCode;"
    out.println ""
    out.println "@Data";
    out.println "@EqualsAndHashCode(callSuper = false)"
    out.println "@TableName("${table.getName()}")";
    out.println "@ApiModel(value = "${table.getComment()}")"
    out.println "public class $className {"
    out.println ""
    out.println ""
    //字段
    fields.each() {
    out.println "";
    // 输出注释
    if (isNotEmpty(it.commoent)) {
    out.println " /**"
    out.println " * ${it.commoent.toString()}"
    out.println " */"
    }
    if (it.annos != "") out.println " ${it.annos}"
    out.println " private ${it.type} ${it.name};"
    }
    out.println ""
    //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
    fields += [[
    sourceName: col.getName(),
    name : javaName(col.getName(), false),
    type : typeStr,
    annos : "@ApiModelProperty(value = "${getUft8Str(isNotEmpty(col.getComment()) ? col.getComment() : "")}")",
    commoent : ""]]
    }
    }

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

    def getUft8Str(str) {
    // StringBuffer output = new StringBuffer();
    // if (isNotEmpty(str)) {
    // for (int i = 0; i < str.length(); i++) {
    // output.append("\u" + Integer.toString(str[i], 16) + ";");
    // }
    // }
    // return output.toString();
    return str;
    }

    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]
    }
  • 相关阅读:
    Java Socket编程(三)发送和接收深入
    Java温故知新 集合类
    DataTable到实体类的转换 中庸
    经典设计模式
    门面模式
    java经典反射机制(1)
    装饰模式
    单子模式
    sql经典题目(1)
    如何学习struts框架?
  • 原文地址:https://www.cnblogs.com/cyh1282656849/p/14121399.html
Copyright © 2020-2023  润新知