• IDEA数据库生成Entity 带注释, 默认值


    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.xxx.xxx.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 tableName = table.getName()
        def className = javaName(tableName, true)
        def fields = calcFields(table)
    // 指定编码, 处理中文注释的问题
    new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields, tableName) } } def generate(out, className, fields, tableName) { out.println "package $packageName" out.println "" out.println "" out.println "import lombok.Data;" out.println "import javax.persistence.*;" out.println "import java.io.Serializable;" out.println "import java.sql.Timestamp;" out.println "" out.println "" out.println "@Data" out.println "@Entity" out.println "@Table(name="$tableName")" out.println "public class $className implements Serializable {" out.println "" fields.each() { if (it.annos != "") out.println " ${it.annos}" if (it.name == "id") { out.println " @Id" if (it.type != "String") { out.println " @GeneratedValue(strategy = GenerationType.IDENTITY)" } } if(it.name == "entry_date" || it.name == "entryDate"){ // default value out.println " private ${it.type} ${it.name} = new Timestamp(System.currentTimeMillis());" } else{ if(it.default != null){ if(it.comment != null){ out.println " private ${it.type} ${it.name} = ${it.default}; // ${it.comment}" }else{ out.println " private ${it.type} ${it.name} = ${it.default};" } }else{ if(it.comment != null){ out.println " private ${it.type} ${it.name}; // ${it.comment}" }else{ out.println " private ${it.type} ${it.name};" } } } } // 使用Data装饰器, 就不需要这个了 out.println "" // 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 += [[ name : col.getName(), // 不对字段名做任何转换 javaName(col.getName(), false), type : typeStr, comment : col.getComment(), // 注释 default : col.getDefault(), // 默认值 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] }
  • 相关阅读:
    bzoj4563: [Haoi2016]放棋子(错排+高精)
    bzoj1089 [SCOI2003]严格n元树(dp+高精)
    9.15NOIP模拟题
    洛谷 P2010 回文日期 题解
    洛谷 P1147 连续自然数和 题解
    洛谷 P1152 欢乐的跳 题解
    信息学奥赛一本通 高手训练1 统计方案数
    想学习找不到好的博客?看这里>>
    信息学奥赛一本通 高手训练1 游戏通关
    洛谷 P3398 仓鼠找sugar 题解
  • 原文地址:https://www.cnblogs.com/wancy86/p/13712517.html
Copyright © 2020-2023  润新知